of metodu
Java 9 ile geliyor.
1. Immutable Map döndürür.
2. Map.of() metodu en fazlan 10 elemanlık bir Map yaratabilir. Daha büyük bir şey yaratmak istersek Map.ofEntries()
kullanılır
3. null key değer içeremez.
Açıklaması
şöyle. Bu metod ile Collections.emptyMap() benzer iş yaparlar. Fark olarak bu metod null key alamazken, diğeri
alabilir.
The Map.of() and Map.ofEntries() static factory methods provide a convenient way to create immutable maps. The Map instances created by these methods have the following characteristics:
4. aynı key değerini içermez, yoksa IllegalArgumentException fırlatılır. Açıklaması şöyle
They reject duplicate keys at creation time. Duplicate keys passed to a static factory method result in IllegalArgumentException.
Şu kod
hatalıdır.
Map<String, Integer> tempMap = Map.of(
"London", 13,
"London", 13, // !
);
ÖrnekImmutable map istemiyorsak şöyle yaparızMap<Integer, String> map = new HashMap<>( Map.of(1,"a", 2,"b", 3,"c") );
ÖrnekImmutable map istemiyorsak şöyle
yaparız.
Map<Integer, String> map = new HashMap<Integer, String>(
Map.of(1, "value1", 2, "value2", 3, "value3"));
Örnek
Şöyle
yaparız.
Map<Integer, String> map = Map.of(1, "value1", 2, "value2", 3, "value3", 4 );
Örnek
Şöyle
yaparız.
Map<String, String> srcMap = Map.of("A", "a", "B", "b", "C", "c");