How can I initialise a static Map?

前端 未结 30 1036
慢半拍i
慢半拍i 2020-11-22 08:43

How would you initialise a static Map in Java?

Method one: static initialiser
Method two: instance initialiser (anonymous subclass) or some other m

相关标签:
30条回答
  • 2020-11-22 09:17

    I do not like Static initializer syntax and I'm not convinced to anonymous subclasses. Generally, I agree with all cons of using Static initializers and all cons of using anonymous subclasses that were mentioned in previus answers. On the other hand - pros presented in these posts are not enough for me. I prefer to use static initialization method:

    public class MyClass {
        private static final Map<Integer, String> myMap = prepareMap();
    
        private static Map<Integer, String> prepareMap() {
            Map<Integer, String> hashMap = new HashMap<>();
            hashMap.put(1, "one");
            hashMap.put(2, "two");
    
            return hashMap;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 09:18

    One advantage to the second method is that you can wrap it with Collections.unmodifiableMap() to guarantee that nothing is going to update the collection later:

    private static final Map<Integer, String> CONSTANT_MAP = 
        Collections.unmodifiableMap(new HashMap<Integer, String>() {{ 
            put(1, "one");
            put(2, "two");
        }});
    
     // later on...
    
     CONSTANT_MAP.put(3, "three"); // going to throw an exception!
    
    0 讨论(0)
  • 2020-11-22 09:18

    If you want unmodifiable map, finally java 9 added a cool factory method of to Map interface. Similar method is added to Set, List as well.

    Map<String, String> unmodifiableMap = Map.of("key1", "value1", "key2", "value2");

    0 讨论(0)
  • 2020-11-22 09:19

    With Eclipse Collections, all of the following will work:

    import java.util.Map;
    
    import org.eclipse.collections.api.map.ImmutableMap;
    import org.eclipse.collections.api.map.MutableMap;
    import org.eclipse.collections.impl.factory.Maps;
    
    public class StaticMapsTest
    {
        private static final Map<Integer, String> MAP =
            Maps.mutable.with(1, "one", 2, "two");
    
        private static final MutableMap<Integer, String> MUTABLE_MAP =
           Maps.mutable.with(1, "one", 2, "two");
    
    
        private static final MutableMap<Integer, String> UNMODIFIABLE_MAP =
            Maps.mutable.with(1, "one", 2, "two").asUnmodifiable();
    
    
        private static final MutableMap<Integer, String> SYNCHRONIZED_MAP =
            Maps.mutable.with(1, "one", 2, "two").asSynchronized();
    
    
        private static final ImmutableMap<Integer, String> IMMUTABLE_MAP =
            Maps.mutable.with(1, "one", 2, "two").toImmutable();
    
    
        private static final ImmutableMap<Integer, String> IMMUTABLE_MAP2 =
            Maps.immutable.with(1, "one", 2, "two");
    }
    

    You can also statically initialize primitive maps with Eclipse Collections.

    import org.eclipse.collections.api.map.primitive.ImmutableIntObjectMap;
    import org.eclipse.collections.api.map.primitive.MutableIntObjectMap;
    import org.eclipse.collections.impl.factory.primitive.IntObjectMaps;
    
    public class StaticPrimitiveMapsTest
    {
        private static final MutableIntObjectMap<String> MUTABLE_INT_OBJ_MAP =
                IntObjectMaps.mutable.<String>empty()
                        .withKeyValue(1, "one")
                        .withKeyValue(2, "two");
    
        private static final MutableIntObjectMap<String> UNMODIFIABLE_INT_OBJ_MAP =
                IntObjectMaps.mutable.<String>empty()
                        .withKeyValue(1, "one")
                        .withKeyValue(2, "two")
                        .asUnmodifiable();
    
        private static final MutableIntObjectMap<String> SYNCHRONIZED_INT_OBJ_MAP =
                IntObjectMaps.mutable.<String>empty()
                        .withKeyValue(1, "one")
                        .withKeyValue(2, "two")
                        .asSynchronized();
    
        private static final ImmutableIntObjectMap<String> IMMUTABLE_INT_OBJ_MAP =
                IntObjectMaps.mutable.<String>empty()
                        .withKeyValue(1, "one")
                        .withKeyValue(2, "two")
                        .toImmutable();
    
        private static final ImmutableIntObjectMap<String> IMMUTABLE_INT_OBJ_MAP2 =
                IntObjectMaps.immutable.<String>empty()
                        .newWithKeyValue(1, "one")
                        .newWithKeyValue(2, "two");
    } 
    

    Note: I am a committer for Eclipse Collections

    0 讨论(0)
  • 2020-11-22 09:19

    I like anonymous class, because it is easy to deal with it:

    public static final Map<?, ?> numbers = Collections.unmodifiableMap(new HashMap<Integer, String>() {
        {
            put(1, "some value");
                        //rest of code here
        }
    });
    
    0 讨论(0)
  • 2020-11-22 09:19

    As usual apache-commons has proper method MapUtils.putAll(Map, Object[]):

    For example, to create a color map:

    Map<String, String> colorMap = MapUtils.putAll(new HashMap<String, String>(), new String[][] {
         {"RED", "#FF0000"},
         {"GREEN", "#00FF00"},
         {"BLUE", "#0000FF"}
     });
    
    0 讨论(0)
提交回复
热议问题