Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8206833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:48:27+00:00 2026-06-07T08:48:27+00:00

I’d like a convenience method to take a set of parameters and return an

  • 0

I’d like a convenience method to take a set of parameters and return an array, much like Arrays.asList(T... items) will take a set of parameters and return a List<T> of those items.

It’s easy enough to write one, but does one already exist in java?

UPDATE
My bad! I didn’t realize the question was so unclear. Your questions have forced me to realize that the question isn’t quite the question I thought it was.

I have several calls like the following that place various key/values into a Map:

        put( Key.get(A.class), new Key[] { Key.get(X.class), Key.get(Y.class), Key.get(Z.class)});

… where the map is of type Map<Key<? extends Foo>,Key<? extends Foo>[]>

I was looking for a typesafe and succinct way to execute the above statement, and I thought that something like the following would work:

        put( Key.get(A.class), toArray( Key.get(X.class), Key.get(Y.class), Key.get(Z.class)));

… where toArray() is defined as something like

private static <T> T[] toArray( T... t ) {
    return t;
}

However, it turns out that this solution is not typesafe itself, and thus it’s really not much more succinct than just creating a new array manually using new. This was the first cause of my misunderstanding.

I thought that I could get typesafety by using a List instead of an array and then using Arrays.asList() to populate the values of the list, but it turns out that that’s not typesafe either. This was the second cause of my misunderstanding. I thought that Arrays.asList() would make this statement more succinct than it actually does, and thus I was looking for something that would do the same for me for arrays.

So I suppose the question is really – Is there a succinct way to get typesafety in the above situation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T08:48:30+00:00Added an answer on June 7, 2026 at 8:48 am

    Arrays already have such a shortcut syntax:

    String[] strArray = {"one", "two", "three"};
    

    In response to your update:

    As it seems like you discovered, arrays of parameterized types can never be type-safe. This is one of several limitations due to the fact that arrays and generics are like oil and water.

    A varargs method such as Arrays.asList isn’t spared from this limitation since varargs works by implicitly creating an array of the comma delimited arguments. In order to have type-safety, you’ll need to avoid any solution involving arrays, including varargs.

    First, I recommend you change your map’s type to hold Lists instead of arrays:

    Map<Key<? extends Foo>, List<Key<? extends Foo>>> map = new HashMap<>();
    

    And then build a List before putting it in the Map:

    List<Key<? extends Foo>> lst = new ArrayList<>();
    lst.add(Key.get(X.class));
    lst.add(Key.get(Y.class));
    lst.add(Key.get(Z.class));
    map.put(Key.get(A.class), lst);
    

    If you want it all in one statement, it’s going to be trickier without varargs. Guava’s ImmutableList exposes the of factory methods taking up to 12 elements before falling back to varargs. If the Lists in the map aren’t going to be modified later, you could store ImmutableList<Key<? extends Foo>> and use:

    map.put(
        Key.get(A.class),
        ImmutableList.of(Key.get(X.class), Key.get(Y.class), Key.get(Z.class))
    );
    

    In fact you could still take advantage of those factory methods even if the List needs to be modifiable by copying the returned ImmutableList:

    map.put(
        Key.get(A.class),
        Lists.newArrayList(ImmutableList.of(
            Key.get(X.class),
            Key.get(Y.class),
            Key.get(Z.class)
        ))
    );
    

    But then you’re introducing overhead just for the sake of style.


    Side note: if you do happen to be using Guava, you might look at using a Multimap instead of a Map of Lists.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.