Implement a Multimap in Java
This post will discuss how to implement a Multimap in Java.
A Multimap is a map that allows mapping of a single key to multiple values. Since JDK doesn’t provide any implementation of the Multimap, programmers often miss it in Java. Although Google’s Guava library and Apache Commons Collections both provide an implementation of the Multimap interface, wouldn’t it be great to implement our own Multimap class in Java, which we can customize to our style.
Well, writing a Multimap class is actually very simple in Java. Following is a simple custom implementation of the Multimap class in Java using a Map and a Collection.
|
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
import java.util.*; class MultiMap<K, V> { private Map<K, Collection<V>> map = new HashMap<>(); /** * Add the specified value with the specified key in this multimap. */ public void put(K key, V value) { if (map.get(key) == null) { map.put(key, new ArrayList<V>()); } map.get(key).add(value); } /** * Associate the specified key with the given value if not * already associated with a value */ public void putIfAbsent(K key, V value) { if (map.get(key) == null) { map.put(key, new ArrayList<>()); } // if the value is absent, insert it if (!map.get(key).contains(value)) { map.get(key).add(value); } } /** * Returns the Collection of values to which the specified key is mapped, * or null if this multimap contains no mapping for the key. */ public Collection<V> get(Object key) { return map.get(key); } /** * Returns a set view of the keys contained in this multimap. */ public Set<K> keySet() { return map.keySet(); } /** * Returns a set view of the mappings contained in this multimap. */ public Set<Map.Entry<K, Collection<V>>> entrySet() { return map.entrySet(); } /** * Returns a Collection view of Collection of the values present in * this multimap. */ public Collection<Collection<V>> values() { return map.values(); } /** * Returns true if this multimap contains a mapping for the specified key. */ public boolean containsKey(Object key) { return map.containsKey(key); } /** * Removes the mapping for the specified key from this multimap if present * and returns the Collection of previous values associated with key, or * null if there was no mapping for key. */ public Collection<V> remove(Object key) { return map.remove(key); } /** * Returns the total number of key-value mappings in this multimap. */ public int size() { int size = 0; for (Collection<V> value: map.values()) { size += value.size(); } return size; } /** * Returns true if this multimap contains no key-value mappings. */ public boolean isEmpty() { return map.isEmpty(); } /** * Removes all the mappings from this multimap. */ public void clear() { map.clear(); } /** * Removes the entry for the specified key only if it is currently * mapped to the specified value and return true if removed */ public boolean remove(K key, V value) { if (map.get(key) != null) // key exists return map.get(key).remove(value); return false; } /** * Replaces the entry for the specified key only if currently * mapped to the specified value and return true if replaced */ public boolean replace(K key, V oldValue, V newValue) { if (map.get(key) != null) { if (map.get(key).remove(oldValue)) { return map.get(key).add(newValue); } } return false; } } class Main { // Multimap implementation in Java public static void main(String[] args) { // create a multimap from past US presidents list MultiMap<String, String> multimap = new MultiMap(); multimap.put("Zachary", "Taylor"); multimap.put("John", "Adams"); multimap.put("John", "Tyler"); multimap.put("John", "Kennedy"); multimap.put("George", "Washington"); multimap.put("George", "Bush"); multimap.put("Grover", "Cleveland"); System.out.println("----- Printing Multimap using keySet -----\n"); for (String lastName: multimap.keySet()) { System.out.println(lastName + ": " + multimap.get(lastName)); } } } |
Output:
—– Printing Multimap using keySet —–
George: [Washington, Bush]
Zachary: [Taylor]
John: [Adams, Tyler, Kennedy]
Grover: [Cleveland, Cleveland]
That’s all about Multimap implementation in Java.
Also See:
Reference: HashMap (Java Platform SE 8 )
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 :)