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, "Android");

// Getting value by key
String value2 = table.get(key2);
System.out.println(value2);

// Iterate map
table.forEach((k, v) -> System.out.println("Key = "
+ k + ", Value = " + v));

// Remove one item
table.remove(key2);

}

}

class MultiMapKey {
public Object row, col;

public MultiMapKey(Object row, Object col) {
this.row = row;
this.col = col;

}

@Override
public int hashCode() {
return row.hashCode() ^ col.hashCode();

}

@Override
public boolean equals(Object myKey) {
if ((myKey instanceof MultiMapKey)) {
MultiMapKey multiMapKey = (MultiMapKey) myKey;
return this.row.equals(multiMapKey.row) &&
this.col.equals(multiMapKey.col);

} else {
return false;
}
}
}


Example 2: Using Google's Guava


Guava's Table is a collection that represents a table-like structure containing rows, columns and the associated cell values. The row and the column act as an ordered pair of keys.

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;

public class GuavaMultiKeyDemo {

public static void main(String[] args) {

// Declaring the Table
Table<String, String, String> table = HashBasedTable.create();

// Insert & Update
table.put("raw1", "col1", "Java");
table.put("raw1", "col2", "Kotlin");
table.put("raw1", "col3", "Android");

// Getting the values
String value2 = table.get("raw1", "col3");
System.out.println(value2);

}
}


Example 3: Using Apache Commons Collection


The MultiKeyMap is the most efficient way to uses multiple keys to map to a value. These provide get, containsKey, put and remove for individual keys which operate without extra object creation.

import org.apache.commons.collections4.map.MultiKeyMap;

public class ApacheMultiKeyDemo {

public static void main(String[] args) {
// Declaring the MultiKeyMap
MultiKeyMap<String, String> table = new MultiKeyMap<String, String>();

// Insert & Update
table.put("raw1", "col1", "Java");
table.put("raw1", "col2", "Kotlin");
table.put("raw1", "col3", "Android");

// Getting the values
String value2 = table.get("raw1", "col2");
System.out.println(value2);

// Contains key
Boolean containsKey = table.containsKey("raw1", "col1");
System.out.println(containsKey);

}
}

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Spring boot video streaming example-HTML5

Spring Boot + Mockito simple application with 100% code coverage

Spring Boot + OpenCSV Export Data to CSV Example

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Custom Exception Handling in Quarkus REST API

ReactJS, Spring Boot JWT Authentication Example

DataTable-Pagination example with Spring boot, jQuery and ajax

Spring Webflux File Download Example

Node JS mini projects with source code - free download