7
HashMap <String,String... strings> hm = new HashMap<String,String... strings>();

hm.put("Zara", "Pony","Cars", "Magic");
hm.put("John", "Horse","Flying", "Loving");

How can I do that? It's not letting me.

4
  • 1
    You can't. You'll need to use a Map<String, String[]> instead. Commented Mar 29, 2018 at 7:08
  • 1
    Varargs are only for function arguments. What do you actually want in the values of the map? Commented Mar 29, 2018 at 7:09
  • 2
    I want to do this - why? Commented Mar 29, 2018 at 7:11
  • @ScaryWombat I recently did 4 question graduate position test, in which I got 0% correct. And one of the questions had something like this in the test. Commented Mar 29, 2018 at 7:33

5 Answers 5

5

The elipsis (...) operator can only be used in method signatures. You could explicitly declare and create arrays:

Map<String, String[]> hm = new HashMap<>();

hm.put("Zara", new String[]{"Pony", "Cars", "Magic"});
hm.put("John", new String[]{"Horse", "Flying", "Loving"});

If you absolutely must use varags, you can wrap the call to Map#put with your own method:

public static void main(String[] args) {
    Map<String, String[]> hm = new HashMap<>();

    addToMap(hm, "Zara", "Pony", "Cars", "Magic");
    addToMap(hm, "John", "Horse", "Flying", "Loving");
}

private static void addToMap
    (Map<String, String[]> map, String key, String... values) {
    map.put(key, values);
}
Sign up to request clarification or add additional context in comments.

Comments

3

Vararg notation is only for use with functions that allow it. It can not be used anywhere else.

You have to decide what you actually want your map values to be. Normally varargs is basically syntactic sugar for an array. However, a List or Set also makes sense here.

Either of the following would work:

  1. Array:

    HashMap <String, String[]> hm = new HashMap<>();
    hm.put("Zara", new String[] {"Pony","Cars", "Magic"});
    hm.put("John", new String[] {"Horse", "Flying", "Loving"});
    
  2. List:

    HashMap <String, List<String>> hm = new HashMap<>();
    hm.put("Zara", new ArrayList<String>());
    hm.get("Zara").add("Pony");
    hm.get("Zara").add("Cars");
    hm.get("Zara").add("Magic");
    hm.put("John", new ArrayList<String>());
    hm.get("John").add("Horse");
    hm.get("John").add("Flying");
    hm.get("John").add("Loving");
    

Comments

1

Closest you can do is use a `Map and create a method for that

public static void addToMap(Map<String, String[]> map, String key, String...values) {
    map.put(key, values);
}

Comments

0

See this question. Basically you need to do the following:

hm.put("Zara", new String[]{"Pony", "Cars", "Magic"});

Or if you want to declare it separetely:

String[] zaraValues = new String[]{"Pony", "Cars", "Magic"};
hm.put("Zara", zaraValues);

Comments

0

You might be looking for this:

HashMap<String, List<String>> hm = new HashMap<>();
hm.put("Zara", Arrays.asList("Pony", "Cars", "Magic"));
hm.put("John", Arrays.asList("Horse", "Flying", "Loving"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.