C # in Java - Dictionaries?

Is it possible in Java to make a dictionary with elements already declared in it? Like below C # code:

Dictionary<string, int> d = new Dictionary<string, int>() { {"cat", 2}, {"dog", 1}, {"llama", 0}, {"iguana", -1} }; 

How to do this and what type do I use? I read that the Dictionary is out of date.

+47
java dictionary new-operator c # map
Jul 31 2018-11-11T00:
source share
5 answers

This will do what you want:

 Map<String,Integer> map = new HashMap<String, Integer>(){{ put("cat", 2); put("dog", 1); put("llama", 0); put("iguana", -1); }}; 

This statement creates an anonymous subclass of HashMap, where the only difference from the parent class is that 4 entries are added when the instance is created. This is a fairly common idiom in the Java world (although some find it inconsistent because it creates a new class definition).

Because of this dispute, with Java 9, there is a new idiom for convenient map building: a family of static Map.of methods .

Using Java 9 or higher, you can create the required map as follows:

 Map<String, Integer> map = Map.of( "cat", 2, "dog", 1, "llama", 0, "iguana", -1 ); 

With larger cards, this alternative syntax may be less error prone:

 Map<String, Integer> map = Map.ofEntries( Map.entry("cat", 2), Map.entry("dog", 1), Map.entry("llama", 0), Map.entry("iguana", -1) ); 

(This is especially good if Map.entry is statically imported rather than explicitly specified).

Besides working with Java 9+, these new approaches are not quite equivalent to the previous one:

  • They do not allow you to specify which map implementation is used.
  • They only create immutable cards
  • They do not create an anonymous subclass of Map

However, these differences should not be significant for many use cases, which makes this approach a good default for newer versions of Java.

+67
Jul 31 2018-11-11T00:
source share
 Map<String,Integer> map = new HashMap<String, Integer>(){{ put("cat", 2); put("dog", 1); put("llama", 0); put("iguana", -1); }}; 
+12
Jul 31 '11 at 0:13
source share

Bite the bullet and enter the name of the card!

  Map<String, Integer> map = new HashMap<String, Integer>(); map.put("cat", 2); map.put("dog", 1); map.put("llama", 0); map.put("iguana", -1); 

You can also do something like this, which can save some typing with a long list:

  Object[][] values = { {"cat", 2}, {"dog", 1}, {"llama", 0}, {"iguana", -1} }; for (Object[] o : values) { map.put((String) o[0], (Integer) o[1]); } 
+4
Jul 31. '11 at
source share

If you use the Guava library, you can use its ImmutableMap class either on your own (examples 1 and 2), or as an initializer for the HashMap (examples 3 and 4):

 Map<String, Integer> map1 = ImmutableMap.<String, Integer> builder() .put("cat", 2) .put("dog", 1) .put("llama", 0) .put("iguana", -1) .build(); Map<String, Integer> map2 = ImmutableMap.of( "cat", 2, "dog", 1, "llama", 0, "iguana", -1 ); Map<String, Integer> map3 = Maps.newHashMap( ImmutableMap.<String, Integer> builder() .put("cat", 2) .put("dog", 1) .put("llama", 0) .put("iguana", -1) .build() ); Map<String, Integer> map4 = Maps.newHashMap( ImmutableMap.of( "cat", 2, "dog", 1, "llama", 0, "iguana", -1) ); 
+3
Jul 31 '11 at 0:50
source share

Java7 almost introduced "collection literals" that would allow syntax usage. They will probably try to stick it in Java8. I do not know what is wrong with these people.

This can be easily achieved using some kind of wrapper API.

 Map<String,Integer> map = Maps.<String,Integer>empty() .put("cat", 2).put("dog",1)....; 

Not so bad. I would prefer something like

 map("cat", "dog", ... ) .to( 1, 2, ... ); 

Such things should have been implemented by different people, unfortunately, the standard API does not entail such things.

0
Jul 31 2018-11-11T00:
source share



All Articles