Initialize Map in Java using Apache Commons Collections
This post will discuss various methods to initialize a map using Apache Commons Collections in Java.
1. Using MapUtils.EMPTY_MAP
Apache Commons Collections provides Collections.EMPTY_MAP that returns an immutable empty map. It throws a UnsupportedOperationException if any modification operation is performed on it.
|
1 |
Map<String, String> immutableMap = MapUtils.EMPTY_MAP; |
2. Using MapUtils.unmodifiableMap() method
Apache Commons Collections MapUtils class provides an unmodifiableMap() that returns an unmodifiable map backed by the given map.
|
1 2 |
Map<String, String> map = MapUtils.EMPTY_MAP; Map<String, String> unmodifiableMap = MapUtils.unmodifiableMap(map); |
It throws:
1. NullPointerException if the given map is null.
2. UnsupportedOperationException if any modification operation is performed on it.
3. Using MapUtils.putAll() method
Apache Commons Collections MapUtils class also provides putAll(Map<K,V> map, Object[] array) that puts all the keys and values from the specified array into the map.
If the first entry in the object array implements Map.Entry or KeyValue, then the key and value are added from that object. If the first entry in the object array is an object array itself, then it is assumed that index 0 in the subarray is the key and index 1 is the value. Otherwise, the array is treated as keys and values in alternate indices.
For example, to create a color map:
|
1 2 3 4 5 |
Map colorMap = MapUtils.putAll(new HashMap(), new String[][] { {"RED", "#FF0000"}, {"GREEN", "#00FF00"}, {"BLUE", "#0000FF"} }); |
or:
|
1 2 3 4 5 |
Map colorMap = MapUtils.putAll(new HashMap(), new String[] { "RED", "#FF0000", "GREEN", "#00FF00", "BLUE", "#0000FF" }); |
or:
|
1 2 3 4 5 |
Map colorMap = MapUtils.putAll(new HashMap(), new Map.Entry[] { new DefaultMapEntry("RED", "#FF0000"), new DefaultMapEntry("GREEN", "#00FF00"), new DefaultMapEntry("BLUE", "#0000FF") }); |
For different types of key-value pair:
|
1 2 3 4 5 |
Map<Integer, String> map = MapUtils.putAll(new HashMap<>(), new Object[][]{ {1, "#FF0000"}, {2, "#00FF00"}, {3, "#0000FF"} }); |
It throws:
1. NullPointerException if the given map is null.
2. IllegalArgumentException if subarray or entry matching is used and an entry is invalid.
3. ClassCastException if the array contents are mixed.
4. Fixed-length empty Map
Apache Commons Collections MapUtils class has fixedSizeMap() method that returns a fixed-length map backed by the specified map.
Elements may not be added or removed from the returned map, but we can change existing elements (for instance, via the Map.put(Object, Object) method). But any change is made to the underlying map will be reflected in the returned map. The map will throw an UnsupportedOperationException if any resize operation is performed on it.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import org.apache.commons.collections4.MapUtils; import java.util.HashMap; import java.util.Map; // Initialize map using Apache Commons Collections class Main { public static void main(String[] args) { Map<String, String> mutableMap = new HashMap<>(); Map<String, String> fixedLenMap = MapUtils.fixedSizeMap(mutableMap); mutableMap.put("USA", "Washington D.C."); mutableMap.put("UK", "London"); mutableMap.put("India", "Delhi"); // We cannot put a new key/value pair since the map is a fixed size. // But we can set a new value for the existing key. fixedLenMap.put("India", "New Delhi"); // {USA=Washington D.C., UK=London, India=New Delhi} System.out.println(fixedLenMap); } } |
5. Using Multimap
Multimap is a collection that maps keys to values, similar to a map, but each key may be associated with multiple values. We can use MultiMap to create a map that holds a collection of values against each key. There is only one implementation of Multimap available – MultiValueMap that uses an ArrayList by default; however, a class to instantiate or a factory returning a Collection instance may be specified.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import org.apache.commons.collections4.MultiMap; import org.apache.commons.collections4.map.MultiValueMap; class Main { // Multimap in Java using Apache Commons Collection public static void main(String[] args) { MultiMap multiMap = new MultiValueMap(); multiMap.put("USA", "NYC"); multiMap.put("USA", "Los Angeles"); multiMap.put("Asia", "Mumbai"); multiMap.put("Asia", "Beijing"); // {USA=[NYC, Los Angeles], Asia=[Mumbai, Beijing]} System.out.println(multiMap); } } |
That’s all about initializing Map in Java using Apache Commons Collections.
Related Posts:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)