Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

HashMap – Single Key and Multiple Values Example

DZone's Guide to

HashMap – Single Key and Multiple Values Example

Sometimes you want to store multiple values for the same hash key. The following code examples show you 3 different ways to do this.

· Java Zone ·
Free Resource

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.

Scenario

HashMap can be used to store key-value pairs.

But sometimes you may want to store multiple values for the same key.

For example:

For Key A, you want to store - Apple, Aeroplane

For Key B, you want to store - Bat, Banana

For Key C, you want to store – Cat, Car

The following code snippets will show you at least 3 different ways of storing key-value pairs with a distinction of Single Key and Multiple Values

HashMap – Single Key and Multiple Values Using List

package com.skilledmonster.examples.util.map;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * HashMap - Single Key and Multiple Values using List
 *
 * @author Jagadeesh Motamarri
 * @version 1.0
 */
public class SingleKeyMultipleValueUsingList {

    public static void main(String[] args) {

        // create map to store
        Map<String, List<String>> map = new HashMap<String, List<String>>();

        // create list one and store values
        List<String> valSetOne = new ArrayList<String>();
        valSetOne.add("Apple");
        valSetOne.add("Aeroplane");

        // create list two and store values
        List<String> valSetTwo = new ArrayList<String>();
        valSetTwo.add("Bat");
        valSetTwo.add("Banana");

        // create list three and store values
        List<String> valSetThree = new ArrayList<String>();
        valSetThree.add("Cat");
        valSetThree.add("Car");

        // put values into map
        map.put("A", valSetOne);
        map.put("B", valSetTwo);
        map.put("C", valSetThree);

        // iterate and display values
        System.out.println("Fetching Keys and corresponding [Multiple] Values n");
        for (Map.Entry<String, List<String>> entry : map.entrySet()) {
            String key = entry.getKey();
            List<String> values = entry.getValue();
            System.out.println("Key = " + key);
            System.out.println("Values = " + values + "n");
        }
    }
}




HashMap – Single Key and Multiple Values Using Google Guava Collections

package com.skilledmonster.examples.util.map;

import java.util.Set;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

/**
 * HashMap - Single Key and Multiple Values using Google Guava Collections
 *
 * @author Jagadeesh Motamarri
 * @version 1.0
 */
public class SingleKeyMultipleValueUsingGuava {

    public static void main(String[] args) {

        // create multimap to store key and values
        Multimap<String, String> multiMap = ArrayListMultimap.create();

        // put values into map for A
        multiMap.put("A", "Apple");
        multiMap.put("A", "Aeroplane");

        // put values into map for B
        multiMap.put("B", "Bat");
        multiMap.put("B", "Banana");

        // put values into map for C
        multiMap.put("C", "Cat");
        multiMap.put("C", "Car");

        // retrieve and display values
        System.out.println("Fetching Keys and corresponding [Multiple] Values n");

        // get all the set of keys
        Set<String> keys = multiMap.keySet();

        // iterate through the key set and display key and values
        for (String key : keys) {
            System.out.println("Key = " + key);
            System.out.println("Values = " + multiMap.get(key) + "n");
        }

    }
}



HashMap – Single Key and Multiple Values using Apache Commons Collection

package com.skilledmonster.examples.util.map;

import java.util.Set;

import org.apache.commons.collections.MultiMap;
import org.apache.commons.collections.map.MultiValueMap;

/**
 * HashMap - Single Key and Multiple Values using Apache Commons Collections
 *
 * @author Jagadeesh Motamarri
 * @version 1.0
 */
public class SingleKeyMultipleValueUsingApacheCollections {

    public static void main(String[] args) {

        // create multimap to store key and values
        MultiMap multiMap = new MultiValueMap();

        // put values into map for A
        multiMap.put("A", "Apple");
        multiMap.put("A", "Aeroplane");

        // put values into map for B
        multiMap.put("B", "Bat");
        multiMap.put("B", "Banana");

        // put values into map for C
        multiMap.put("C", "Cat");
        multiMap.put("C", "Car");

        // retrieve and display values
        System.out.println("Fetching Keys and corresponding [Multiple] Values n");

        // get all the set of keys
        Set<String> keys = multiMap.keySet();

        // iterate through the key set and display key and values
        for (String key : keys) {
            System.out.println("Key = " + key);
            System.out.println("Values = " + multiMap.get(key) + "n");
        }

    }
}

Output

The output for all the above 3 scenarios will be same as the one below

hashmap - single_key_multiple_values

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.

Topics:
java

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}