1. Overview

This article is about how to java multikey map with example. Java  Map has a key value pair where the key must be a unique Object which used to store and retrieve value Object. But we want more than two keys then we can use apache commons-collections. Let’s try to understand in which type of case we require a multi key map, Let’s assume that we want to store locale in our application where locate generally of Language name, Label name and Label’s display value. combinations of Language name and Lable name must be unique then this situation can not be easily managed in the simple key-value map, In this case, we need a multi keymap.

For the apache commons-collections, we need to add the following dependency in pom.xml:

<dependency>
           <groupId>org.apache.commons</groupId>
           <artifactId>commons-collections4</artifactId>
           <version>4.4</version>
</dependency>

2. Examples of Java MultiKeyMap

2.1 Add elements to multikey Map

Here we have created multiKeyMap and added some value, First two objects considered as keys and last object consider as value. Combination of the first two Object must be unique otherwise it will override the value object.

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

public class Demo {

    public static void main(String ... args){

        MultiKeyMap multiKeyMap=new MultiKeyMap();
        multiKeyMap.put("EN","Label_Name","Name");
        multiKeyMap.put("EN","Label_Address","Address");
        multiKeyMap.put("EN","Label_Age","Age");

        multiKeyMap.put("GJ","Label_Name","નામ");
        multiKeyMap.put("GJ","Label_Address","સરનામું");
        multiKeyMap.put("GJ","Label_Age","ઉંમર");

        multiKeyMap.put("JA","Label_Name","名");
        multiKeyMap.put("JA","Label_Address","住所");
        multiKeyMap.put("JA","Label_Age","年齢");


        System.out.println("Name label in english: " + multiKeyMap.get("EN","Label_Name"));
        System.out.println("Name label in Gujarati: " + multiKeyMap.get("GJ","Label_Name"));
        System.out.println("Name label in Japanese: " + multiKeyMap.get("JA","Label_Name"));


    }
}

2.2 Remove element from MultiKeyMap

Here are remove operations from MultiKeyMap, As we all know to remove element(s) from Map we must require key(s), Using that key(s) we can remove the elements from Map. Here is an example of the same.

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

public class Demo {

    public static void main(String ... args){

        MultiKeyMap multiKeyMap=new MultiKeyMap();
        multiKeyMap.put("EN","Label_Name","Name");
        multiKeyMap.put("EN","Label_Address","Address");
        multiKeyMap.put("EN","Label_Age","Age");

        multiKeyMap.put("GJ","Label_Name","નામ");
        multiKeyMap.put("GJ","Label_Address","સરનામું");
        multiKeyMap.put("GJ","Label_Age","ઉંમર");

        multiKeyMap.put("JA","Label_Name","名");
        multiKeyMap.put("JA","Label_Address","住所");
        multiKeyMap.put("JA","Label_Age","年齢");

        multiKeyMap.removeAll("JA");            // It will remove 3 elements - EN keys

        multiKeyMap.removeAll("GJ","Label_Name");       // It will remove 1 element - GJ - Label_Name

   }
}

2.3 MultiKeymap Iteration

As we have seen that add and remove operation, Here is Iteration for the Multimap:

public class Demo {

    public static void main(String ... args){

        MultiKeyMap<String,String> multiKeyMap=new MultiKeyMap();
        multiKeyMap.put("EN","Label_Name","Name");
        multiKeyMap.put("EN","Label_Address","Address");
        multiKeyMap.put("EN","Label_Age","Age");

        multiKeyMap.put("GJ","Label_Name","નામ");
        multiKeyMap.put("GJ","Label_Address","સરનામું");
        multiKeyMap.put("GJ","Label_Age","ઉંમર");

        multiKeyMap.put("JA","Label_Name","名");
        multiKeyMap.put("JA","Label_Address","住所");
        multiKeyMap.put("JA","Label_Age","年齢");

        
        // option 1 
        multiKeyMap.forEach((key1,key2)->System.out.println("Keys set" + key1 + " : Values "+key2));

        // option 2
        multiKeyMap.forEach((key1,key2)->System.out.println("Key" + key1.getKey(0) + " : Values "+key2));  // get key by index
       
   }
}

3. Conclusion

commons-collections4 is a very useful library for the advance level collections, Java collection framework is also powerful but our advance level requirement sometimes not able to full-fill. Here we have learned about Multiple keymap using apache commons-collections. It also provides other collections like OrderedMap, BidiMap – get value from key and get key from value, Bag – Add multiple copy of the same object in Map.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *