restter.blogg.se

Java hashmap example
Java hashmap example







java hashmap example
  1. JAVA HASHMAP EXAMPLE UPDATE
  2. JAVA HASHMAP EXAMPLE FREE

Note: I am a committer for Eclipse Collections Private static final ImmutableIntObjectMap IMMUTABLE_INT_OBJ_MAP2 = Private static final ImmutableIntObjectMap IMMUTABLE_INT_OBJ_MAP = Private static final MutableIntObjectMap SYNCHRONIZED_INT_OBJ_MAP = Private static final MutableIntObjectMap UNMODIFIABLE_INT_OBJ_MAP = Private static final MutableIntObjectMap MUTABLE_INT_OBJ_MAP = You can also statically initialize primitive maps with Eclipse Collections. Private static final ImmutableMap IMMUTABLE_MAP2 = Private static final ImmutableMap IMMUTABLE_MAP = Private static final MutableMap SYNCHRONIZED_MAP = Private static final MutableMap UNMODIFIABLE_MAP = Private static final MutableMap MUTABLE_MAP = With Eclipse Collections, all of the following will work: import collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue)) Private static final Map myMap = Stream.of( For this example we can make use of former as: import .* We already have two implementations of Entry in which are SimpleEntry and SimpleImmutableEntry. We can also use Map.of as suggested by Tagir in his answer here but we cannot have more than 10 entries using Map.of. Private static final Map map = Map.ofEntries( We can use Map.ofEntries, calling Map.entry( k, v ) to create each entry.

JAVA HASHMAP EXAMPLE UPDATE

Update (2016): As Tagir Valeev points out, Java 9 will finally make this clean to do using nothing but pure JDK, by adding convenience factory methods for collections: static final Map MY_MAP = Map.of( The hack with two-dimensional array and Stream API is pretty ugly if you ask me, and gets uglier if you need to create a Map whose keys and values are not the same type (like Map in the question).Īs for future of Guava in general, with regards to Java 8, Louis Wasserman said this back in 2014, and in 2016 it was announced that Guava 21 will require and properly support Java 8. If you don't want Guava dependency, consider a plain old init method. Update (2015): As for Java 8, well, I would still use the Guava approach because it is way cleaner than anything else. (If you are new to it, there are some excellent learning resources behind that link.)

JAVA HASHMAP EXAMPLE FREE

If you aren't using this library in your Java project yet, I strongly recommend trying it out! Guava has quickly become one of the most popular and useful free 3rd party libs for Java, as fellow SO users agree. (A subset of) Guava used to be called Google Collections. To learn more about the benefits of Guava's immutable collection utilities, see Immutable Collections Explained in Guava User Guide. Instead, try ImmutableMap.builder() along these lines: static final Map MY_MAP = ImmutableMap.builder() If you want the map to have more than 5 entries, you can no longer use ImmutableMap.of(). I like the Guava way of initialising a static, immutable map: static final Map MY_MAP = ImmutableMap.of(Īs you can see, it's very concise (because of the convenient factory methods in ImmutableMap).









Java hashmap example