3

i have some keys which are pointing to many values in a multimap. how can i retrieve the key basing on the value present in the multimap. Here is my code.

   package com.manoj;

import java.util.Set;

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

public class GuavaMap 
{
    public static void main(String[] args) 
    {
        Multimap regions = ArrayListMultimap.create();
        regions.put("asia", "afganistan");
        regions.put("asia", "bangladesh");
        regions.put("asia", "inida");
        regions.put("asia", "japan");
        regions.put("asia", "burma");
        regions.put("europe", "andorra");
        regions.put("europe", "austria");
        regions.put("europe", "belgium");
        regions.put("europe", "cyprus");
        regions.put("oceania","australia");
        regions.put("oceania", "fiji");
        regions.put("oceania", "nauru");
        Set<String> keys = regions.keySet();
        System.out.println("key\t\t\t"+"values\t\t\t");
        System.out.println();
        String comp = null;
        for(String key : keys)
        {
            System.out.print(key);
            System.out.println(regions.get(key));
        }
    }
}

the above code is providing me the output as follows enter image description here

i need the region name basing on the country.

Example: if i give "australia" output should be "oceania"

teobais
  • 2,820
  • 1
  • 24
  • 36
Manoj Krishna
  • 347
  • 1
  • 4
  • 12
  • As values in the `Multimap` are not required to be unique you need to decide how to handle multiple keys for the same value. Leaving that aside, it appears that you need to iterate all `.entries()`, compare the value with "australia" and return the key on match. – jensgram Dec 30 '15 at 10:22
  • look at this question, http://stackoverflow.com/questions/8066109/bidirectional-multi-valued-map-in-java – awsome Dec 30 '15 at 10:24
  • I would suggest closing the question if you already found a answer yourself – Aaditya Gavandalkar Dec 30 '15 at 10:45
  • 1
    You might be better off creating a Map of countries to regions and then creating a Multimap from inverting the Map: `Multimap regionsToCountriesMultimap = Multimaps.invertFrom(Multimaps.forMap(countriesToRegionsMap), ArrayListMultimap.create());`. This has several benefits: 1. you have a Map that you can use to get a country's region in constant time, 2. you have a Multimap you can use to get all of the countries in a region in constant time, and 3. using a Map ensures that you can't put a country in more than one region (which is possible with your current solution). – mfulton26 Dec 30 '15 at 12:22

3 Answers3

2

you can invert it

Multimap<String, String> invregions = Multimaps.invertFrom(regions , ArrayListMultimap.<String, String>create());

and call get("yourcountry");

this will give you the keys containing your country

kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
  • Interesting, missed the google library :). Though I am unable to locate method `inverse` on MultiMap regions. – Aaditya Gavandalkar Dec 30 '15 at 10:33
  • 3
    [`inverse()`](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/ImmutableMultimap.html#inverse()) is only available for [`ImmutableMultimap`](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/ImmutableMultimap.html). See [`Multimaps.invertFrom(Multimap)`](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Multimaps.html#invertFrom(com.google.common.collect.Multimap,%20M)). – mfulton26 Dec 30 '15 at 11:53
0

Firstly, I would like to suggest to not use raw types and explicitly mention Key and Value type while creating Map.

Regarding your question to retrieve key depending on value, you can iterate over all entries of the map to do the same like:

class GuavaMap 
{
    //Explicitly mentioned key and value both are Strings here
    public static Multimap<String, String> regions = ArrayListMultimap.<String, String>create();
    public static void main(String[] args) 
    {
        regions.put("asia", "afganistan");
        regions.put("asia", "bangladesh");
        regions.put("asia", "inida");
        regions.put("asia", "japan");
        regions.put("asia", "burma");
        regions.put("europe", "andorra");
        regions.put("europe", "austria");
        regions.put("europe", "belgium");
        regions.put("europe", "cyprus");
        regions.put("oceania","australia");
        regions.put("oceania", "fiji");
        regions.put("oceania", "nauru");
        Set<String> keys = regions.keySet();
        System.out.println("key\t\t\t"+"values\t\t\t");
        System.out.println();
        String comp = null;
        for(String key : keys)
        {
            System.out.print(key);
            System.out.println(regions.get(key));
        }

        //usage of below defined method
        String region = getRegion("australia");
        System.out.println("Region for australia:" + region);
    }

   // Function to get the region name i.e. key
    public static String getRegion(String country){
        for(Entry<String, String> entry : regions.entries()){
            if(entry.getValue().equals(country))
                return entry.getKey();
        }
        return "Not found";
    }
}
-2

Thanks for the reply.

i found a solution based on the list, map and hashmap

package com.manoj;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

public class NestedList 
{
    public static void main(String[] args) 
    {
        List asia = Arrays.asList("afganistan","japan","india","pakistan","singapore","sri lanka");
        List europe = Arrays.asList("albania","belarus","iceland","russia","norway","turkey");
        List middleEast = Arrays.asList("australia","new zealand","samoa","tonga","vanuatu");

        Map region = new HashMap<>();
        region.put("asia",asia);
        region.put("europe", europe);
        region.put("middleeast" ,middleEast);
        String reg = null;
        String val = null;
        for(Object key : region.keySet())
        {
            reg = key.toString();
            Iterator it = ((List) region.get(key)).iterator();
            while(it.hasNext())
            {
                val = it.next().toString();
                if(val.equalsIgnoreCase("india"))//here you have to provide the country name
                {
                    System.out.println(reg);
                }
            }
        }
    }
}
Manoj Krishna
  • 347
  • 1
  • 4
  • 12