2

In other words, why do Java programmers need to create a class or explicitly create a Map when they could instead just say :

{ "name":["value1", "value2"]}

like in other languages.

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
Amol Patil
  • 985
  • 2
  • 11
  • 43
  • 1
    You can basically do that with arrays. But maps and lists are (in Java) interfaces with many implementations, so you need to explicitly say what kind of map or list you are writing. Also, Java is well known for being more verbose than other languages, presumably to make it easier to learn. – khelwood Dec 17 '15 at 09:11
  • 2
    Just for sake of precision this is called Collection `Literals` not anonymous. – benzonico Dec 17 '15 at 09:24
  • More info on this question http://stackoverflow.com/questions/10705705/how-do-i-use-collection-literals-in-java-7 – benzonico Dec 17 '15 at 09:37
  • You can take the source code for the java compiler and add in the features you want if this bothers you enough... – Mad Physicist Dec 17 '15 at 14:05

3 Answers3

3

Static typing

Why do Java programmers need to create a class or explicitly create a Map?

Because Java is a statically typed language, where everything must have its own type.

Double-brace syntax

And still, the language allows you to compose a map of { "name":["value1", "value2"]} with a single statement, using double-brace initialization:

Map<String, List<String>> map = new HashMap<String, List<String>>() {{
    put("name", Arrays.asList("value1", "value2"));
}};

Map.of & List.of

You can use new convenient literal syntax of Map.of and List.of, if an unmodifiable map or list works for your situation. These convenience factory methods for collections were added to Java 9 and later.

    Map < String, List < String > > map = Map.of(
            "nameA" , List.of( "value1" , "value2" ) ,
            "nameB" , List.of( "value3" , "value4" , "value5" )
    );

map.toString(): {nameA=[value1, value2], nameB=[value3, value4, value5]}

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • 2
    The double-brace idiom is dangerous and provides little benefit in exchange for the surprises that come with anonymous classes that look like stock JRE classes. – chrylis -cautiouslyoptimistic- Dec 17 '15 at 09:21
  • 1
    Oh please no, not with the double brace hack again! I'd never heard of it until I started hanging out at SO, and I wish I'd stayed not-hearing-of-it. – yshavit Dec 17 '15 at 09:24
  • 1
    This is actually pretty accurate answer regarding the formulation of the question : this is indeed an anonymous class implementing map, so somehow an anonymous map. And then it initialize it with an initializer block. But only do that to code base you want no one to work on or if you want the next dev to curse you. – benzonico Dec 17 '15 at 09:26
  • 1
    Also, I don't buy the static typing reason. Type inference could handle that problem. You look at the common type of the keys, look at the common type of the values, and there you go. – yshavit Dec 17 '15 at 09:31
  • @kocko nothing to do with final but rather immutable. And I would use guava for this. – benzonico Dec 17 '15 at 09:32
  • @yshavit I added a section on the Java 9 commands `Map.of` & `List.of` as an alternative, if having unmodifiable map/list is acceptable. – Basil Bourque Jan 18 '20 at 20:04
2

The video is about the new collection API improvements of Java 9. But in the beginning Stuart Marks explains why Java is not as convenient as other languages when it comes down to collection creation.

https://www.youtube.com/watch?v=OJrIMv4dAek

SKerkewitz
  • 487
  • 1
  • 6
  • 16
1

It's simply a limitation of the language; inline lists and maps have been proposed but as of Java 9 still won't be included in the core language.

Groovy, on the other hand, does syntax for inline lists (ArrayList by default) and maps (LinkedHashMap by default).

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152