What is the use of Map.ofEntries() instead of Map.of()

后端 未结 3 1234
深忆病人
深忆病人 2020-12-05 15:14

From the documentation of Map.java -

The Map.of() and Map.ofEntries() static factory methods provide a convenient way to cre

3条回答
  •  有刺的猬
    2020-12-05 16:16

    Any guesses on how would you create a Map of 26 elements?

    The primary difference between the two factory methods in Map that you already linked is that :

    Map.ofEntries

    Returns an immutable map containing keys and values extracted from the given entries (that are not bounded in count)

    From the JEP-269:Convenience Factory Methods for Collections:

    For larger numbers of entries, an API will be provided that will create a Map instance given an arbitrary number of key-value pairs:

    Map.ofEntries(Map.Entry...)
    

    While this approach is analogous to the equivalent varargs APIs for List and Set, it unfortunately requires that each key-value pair be boxed. A method for boxing keys and values, suitable for static import, will make this more convenient:

    Map.Entry entry(K k, V v)
    

    Your assumption about the method .of() from Map is somewhat incorrect probably because while this would compile with Java9:

    List values = List.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // note 11 elements here
    
    Set keys = Set.of("z", "o", "tw", "th", "fo", "fi", "si", "se", "e", "n", "te");
    

    This, on the other hand, wouldn't:

    Map map = Map.of("z", 0, "o", 1,
          "tw", 2, "th", 3, "fo", 4, "fi", 5,
          "si", 6, "se", 7, "e", 8, "n", 9, "te", 10); // this would not compile
    

    The reason for that is since there is a varargs implementation for List.of and Set.of but to create a similar API for Map both the keys and values were supposed to be boxed as stated in the JEP as well. So, the same was created using varargs of type Map.entry() as:

    Map map = Map.ofEntries(Map.entry("z",0),
           Map.entry("o",1),Map.entry("t",2)...so on);
    

    Furthermore from the documentation of Map.entry() which is also introduced Since:9 -

    Returns an immutable Map.Entry containing the given key and value. These entries are suitable for populating Map instances using the Map.ofEntries() method.

    The Entry instances created by this method have the following characteristics:

    • They disallow null keys and values. Attempts to create them using a null key or value result in NullPointerException.

    • They are immutable. Calls to Entry.setValue() on a returned Entry result in UnsupportedOperationException.

    • They are not serializable.

    • They are value-based. Callers should make no assumptions about the identity of the returned instances. This method is free to create new instances or reuse existing ones. Therefore, identity-sensitive operations on these instances (reference equality (==), identity hash code, and synchronization) are unreliable and should be avoided.

    which are similar to the characteristics of Immutable Map Static Factory Methods introduced recently.

提交回复
热议问题