Set<String> immutableSet = Set.of("YHOO", "AAPL", "GOOG", "MSystem.out::println);
Initialize Immutable Map in Java 9
When it comes to java.util.Map
the simple methods support upto 10 <key,value>
pairs. static <K,V> Map<K,V> of(K k1, V v1)
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2)
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3)
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3)
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6)
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7)
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8)
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9)
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10)
Here is a simple way to initialize an Immutable MapMap<String, String> immutableMap = Map.of("YHOO", "Yahoo", "AAPL", "Apple", "GOOG", "Alphabet", "MSFT", "Microsoft");
immutableMap.forEach((key, value) -> System.out.println(key + " : " + value));
Beyond that the var-args, that variation takes a variable number of arguments of "java.util.Map.entry"static <K,V> Map<K,V> ofEntries(Map.Entry<? extends K,? extends V>... entries)
Here is an example of how to use the varags variation to initialize an immutable MapMap<String, String> immutableMap2 = Map.ofEntries(Map.entry("YHOO", "Yahoo"), Map.entry("AAPL", "Apple"), Map.entry("GOOG", "Alphabet"), Map.entry("MSFT", "Microsoft"));
immutableMap2.forEach((key, value) -> System.out.println(key + " : " + value));
The Full Code to directly run these examples
package com.aoj.java9.collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Immutables {
public static void main(String[] args) {
List<String> list8 = new ArrayList<String>();
list8.add("YHOO");
list8.add("AAPL");
list8.add("GOOG");
list8.add("MSFT");
List<String> immutableList8 = Collections.unmodifiableList(list8);
//immutableList8.add("ORCL");// Will throw java.lang.UnsupportedOperationException
immutableList8.forEach(System.out::println);
List<String> immutableList = List.of("YHOO", "AAPL", "GOOG", "MSFT");
// immutableList.add("ORCL"); // Will throw java.lang.UnsupportedOperationException
immutableList.forEach(System.out::println);
Map<String, String> immutableMap = Map.of("YHOO", "Yahoo", "AAPL", "Apple", "GOOG", "Alphabet", "MSFT", "Microsoft");
immutableMap.forEach((key, value) -> System.out.println(key + " : " + value));
Map<String, String> immutableMap2 = Map.ofEntries(Map.entry("YHOO", "Yahoo"), Map.entry("AAPL", "Apple"), Map.entry("GOOG", "Alphabet"), Map.entry("MSFT", "Microsoft"));
immutableMap2.forEach((key, value) -> System.out.println(key + " : " + value));
Set<String> immutableSet = Set.of("YHOO", "AAPL", "GOOG", "MSFT");
immutableSet.forEach(System.out::println);
}
}
No comments:
Post a Comment