0

In Java, if we want to pass list of values to a method that can be achieved with Arrays.asList(...) method like -

methodToCall("CA", Arrays.asList("John", "Bob", "Mike"));

How can we achieve same for HashMap in one line like above?

Alpha
  • 13,320
  • 27
  • 96
  • 163
  • https://google.github.io/guava/releases/snapshot/api/docs/com/google/common/collect/ImmutableMap.html#of() (and the overloaded versions) – Tom Oct 25 '16 at 11:32
  • 2
    Please, *please* don't think it's a good idea to use double-brace initialization (just in case anybody suggests it). – Andy Turner Oct 25 '16 at 11:32
  • You mean like `put("A","B","C")`? How would you map keys and values, especially of different types? – Thomas Oct 25 '16 at 11:33
  • Probably the better duplicate: http://stackoverflow.com/questions/507602/how-can-i-initialise-a-static-map?noredirect=1&lq=1 And yes, don't do the double-brace initializer trick: http://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization – Thilo Oct 25 '16 at 11:35
  • Check it here http://stackoverflow.com/questions/6802483/how-to-directly-initialize-a-hashmap-in-a-literal-way. It's really what you're asking for. – timon_the_destroyer Oct 25 '16 at 11:49

0 Answers0