Java 如何在hasmap中找到值的键

Java 如何在hasmap中找到值的键,java,Java,我想根据HashMap中的特定值获取一个键 在下面的程序中,我试图找出句子中单词的最大长度。我使用HashMap将单词存储为键,将单词的长度存储为值 我尝试以下代码: import java.util.*; public class Test3 { public static void main(String[] args) { HashMap<String, Integer> map=new HashMap<String, Integer>(

我想根据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));
    }

}

请帮助解决方案或给我一个想法,以获得特定值的关键

您可以创建一个新地图,该地图将
长度存储为键
并将
值列表
存储为地图中的值

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

下面的程序为您提供了一个映射(以及重复值,如“in”,出现两次),可用于获取长度的最大值和最小值及其值

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)));
    }
}
1.)使用
TreeMap而不是HashMap
TreeMap
将根据关键字自动排序,在本例中,关键字是单词的长度。排序将按升序进行

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

3.)还可以使用
TreeMap map=newtreemap()
,v使用相同的方法创建一个映射,只需访问,它将按降序排序并获得第一个元素
map.degendingMap().entrySet().iterator().next().getValue()


您需要迭代它,然后比较value和returnkey,为什么不创建一个新的var-tempKey呢。在设置temp=max的同时,您可以设置tempKey=str.getKey(),您可以用示例Naman Gala解释一下吗?@kavi看看答案,简短而简单。有没有可能从我的代码中获得输出,这是前面附带的……您需要迭代映射,然后将您的值与max进行比较,然后获得密钥。。因为您将长度存储为值
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)));
    }
}
//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());
    }
}
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