如何根据多图中的值检索密钥?

时间:2015-12-30 10:15:56

标签: java collections guava

我有一些键指向多图中的许多值。如何根据多图中存在的值检索密钥。这是我的代码。

   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));
        }
    }
}

上面的代码为我提供了如下输出 enter image description here

我需要基于国家的地区名称。

示例:如果我给“澳大利亚”输出应该是“大洋洲”

3 个答案:

答案 0 :(得分:2)

你可以反转它

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

并调用get(“yourcountry”);

这将为您提供包含您所在国家/地区的密钥

答案 1 :(得分:0)

首先,我建议不要使用原始类型,并在创建Map时明确提及Key和Value类型。

关于根据值检索密钥的问题,您可以迭代地图的所有entries以执行相同操作:

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 :(得分:-2)

感谢您的回复。

我找到了一个基于列表,地图和散列图的解决方案

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);
                }
            }
        }
    }
}