Posts

Showing posts with the label Apache Commons Collections

Java - Map with Multiple Keys - Example

Today we will show you how to implement a  Map with Multiple Keys.  Solution 1: Using Java Custom Key Class Solution 2: Using Google's Guava Solution 3: Using Apache Commons Collections  Example 1: Using Java Custom MultiMapKey Class Here we created a custom MultiMap Key class, we can use MultiMap as a Key to map a value.  import java.util.HashMap; import java.util.Map; public class MultiKeyDemo { public static void main( String [] args) { // Declaring the Map Map < MultiMapKey , String > table = new HashMap <>(); // Declaring the key objects MultiMapKey key1 = new MultiMapKey( "raw1" , "col1" ); MultiMapKey key2 = new MultiMapKey( "raw1" , "col2" ); MultiMapKey key3 = new MultiMapKey( "raw1" , "col3" ); // Putting the values table.put(key1, "Java" ); table.put(key2, "Kotlin" ); table.put(key3, ...