如何在hashmap中查找值的键

时间:2014-12-17 04:44:33

标签: java

我希望根据HashMap中的特定值获取密钥

在下面的程序中,我尝试查找句子中单词的最大长度。我使用HashMap将单词作为键和长度存储为值。

我试试这段代码:

import java.util.*;
public class Test3 {


    public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<String, Integer>();
        String sent="EGInnovations located in seven countries in the world";
        String[] words=sent.split(" ");
        int len=0;
        int max=0;
        int min=0;
        int temp=0;
        for(String word : words)
        {

            len=word.length();
            map.put(word, len);
        }

        Iterator it=map.entrySet().iterator();
        while(it.hasNext())
        {
            Map.Entry str=(Map.Entry)it.next();
            System.out.println(str.getKey()+" "+str.getValue());

            max=(Integer)str.getValue();
            if(max>min)
            {
                temp=max;
                min=max;
            }
        }
        System.out.println(temp);
        System.out.println(map.get(temp));
    }

}

我得到了这个输出:

seven 5
located 7
world 5
EGInnovations 13
the 3
countries 9
in 2

13    //here I got the maximum length
null  //But If I try to get the key of the value(13) It will give "null".

请帮助解决方案或让我的想法获得特定价值的关键......

2 个答案:

答案 0 :(得分:2)

您可以创建一个新地图,将length as keylist of value存储为地图中的值。

因此,无论何时传递长度,您都可以获得具有该长度的值。

以下程序为您提供a map (along with duplicate values like "in" which occurs twice),可用于获取长度的最大值和最小值及其值。

public static void main(String[] args) {
    HashMap<Integer, List<String>> map = new HashMap<Integer, List<String>>();
    String sent = "EGInnovations located in seven countries in the world";
    String[] words = sent.split(" ");
    int len = 0;
    List<String> stringList;
    for (String word : words) {
        len = word.length();

        stringList = map.get(len);
        if (stringList == null) {
            stringList = new ArrayList<String>();
            map.put(len, stringList);
        }
        stringList.add(word);
    }

    if (!map.isEmpty()) {
        System.out.println("Full length map : \n" + map); //Printing full length map
        System.out.println();
        List<Integer> lengthList = new ArrayList<Integer>(map.keySet());

        System.out.println("Minimum Length : " + Collections.min(lengthList));
        System.out.println("Minimum Length Words : " + map.get(Collections.min(lengthList)));

        System.out.println("\nMax Length : " + Collections.max(lengthList));
        System.out.println("Max Length Words : " + map.get(Collections.max(lengthList)));
    }
}

<强>更新

对于您的代码,您可以添加以下代码after while loop

//temp contains max length value

it = map.entrySet().iterator();
System.out.println("Max length : " + temp);
while(it.hasNext()) {
    Map.Entry str=(Map.Entry)it.next();

    if (str.getValue().equals(temp)) {
        System.out.println("Word : "  + str.getKey());
    }
}

答案 1 :(得分:0)

1。)使用TreeMap instead of HashMapTreeMap将根据密钥自动排序,在这种情况下,密钥的长度。排序将按升序排列。

2。)使用NavigableMap获取最后一个条目,因为最后一个条目将是最大长度。

3。)此外,您可以使用TreeMap<Integer, String> map = new TreeMap<Integer, String>();,使用相同方法创建地图并进行访问,它将按降序排序并获取第一个元素map.descendingMap().entrySet().iterator().next().getValue()

import java.util.NavigableMap;
import java.util.TreeMap;

public class MvelTest {

    public static void main(String[] args) {
        NavigableMap<Integer, String> map = new TreeMap<Integer, String>();
        String sent = "EGInnovations located in seven countries in the world";
        String[] words = sent.split(" ");
        int len = 0;

        for (String word : words) {
            len = word.length();
            map.put(len, word);
        }

         System.out.println("Full Map =>" + map);
         System.out.println("Max Length => " + map.lastEntry().getValue());
    }

}

<强>输出

Full Map =>{2=in, 3=the, 5=world, 7=located, 9=countries,13=EGInnovations} 

Max Length => EGInnovations