1

I am using multimap as shown below but as shown but the issue with this that first i am adding the contents in listand then associating it with the key, can I do it in a single line itself.

Map<Integer, List<String>> multimap = new HashMap<Integer, List<String>>();
List<String> someList = new ArrayList<String>();
someList.add("abc");
someList.add("def");
someList.add("ght");
multimap.put(1, someList);

that is adding list contents where I am eneterting the key. something like

 multimap.put(1, // adding the list in a single line here itslef );
4
  • You could, but it is not necessarily pretty to look at. You could technically write your whole java program on one line. Commented Nov 13, 2013 at 15:57
  • Multimap is different from HashMap. You are using HashMap/. Commented Nov 13, 2013 at 15:58
  • Deja vu. I've seen that code somewhere. @Nambari FYI a multimap is essentially a Map with a List as a value. Commented Nov 13, 2013 at 16:04
  • @AdamArold: Agree, "I am using multimap" based on this wording I thought OP trying to use one of the multimap implementations. Commented Nov 13, 2013 at 16:06

7 Answers 7

4

It's not very pretty, but you could do something along the lines of

multimap.put(1, new ArrayList<String>(Arrays.asList("abc", "def", "ght")));
Sign up to request clarification or add additional context in comments.

4 Comments

Although not pretty, it's what the OP asked for
You don't need to construct an ArrayList from the list returned by Arrays.asList; just use the return value directly. All the map requires is some sort of List.
@TedHopp That will put an immutable List inside the map. I'd wager that the lists will get more content in the future.
@TedHopp yes, but list that is returned with asList has its limits like you cant add or remove its elements, so ArrayList may be better option here.
2

Have a try with following code:

multimap.put(1, Arrays.asList(new String[]{"abc","def","ght"}));

1 Comment

You don't need to explicitly create a String[]; Arrays.asList takes a varargs parameter, so simply listing the strings as arguments works: Arrays.asList("abc", "def", "ght").
1

You could use

List<String> someList = Arrays.asList("abc", "def", "ght");

Comments

1

You are implementing Guavas Multimap by hand, I recommend you to look at the Guava solution.

Multimap<Integer, String> multimap = HashMultimap.create();
multimap.putAll(1, Lists.newArrayList("a", "b", "c"));

This article nicely explains some of the advantages of the Guava Multimap implementation.

While we're at it, guava also provides a solution to initialize lists on one line (javadoc):

Lists.newArrayList("a", "b", "c");

Comments

0
Map<Integer, List<String>> multimap = new HashMap<Integer, List<String>>();

Then

multimap.put(1, new ArrayList<String>(Arrays.asList("abc", "def", "ght")));

Comments

0
Map<Integer, List<String>> singletonMap = Collections.singletonMap(Integer.valueOf(1), Arrays.asList("abc", "dd", "dde"));

Comments

0
multimap.put(1,new ArrayList<String>(Arrays.asList("element","element"))); 

Comments

Your Answer

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