Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 它给出的key=Collections.max(birds.entrySet(),Map.Entry.comparingByValue()).getKey();_Java - Fatal编程技术网

Java 它给出的key=Collections.max(birds.entrySet(),Map.Entry.comparingByValue()).getKey();

Java 它给出的key=Collections.max(birds.entrySet(),Map.Entry.comparingByValue()).getKey();,java,Java,我不明白如果两个值相同,LOC如何给出最小的键,如-(1=8),(2=8) 它是如何给出正确答案的 static int migratoryBirds(List<Integer> arr) { Map<Integer, Integer> birds = new HashMap<>(); int key = 0; for(int i = 0; i < arr.size(); i++) { if(birds.

我不明白如果两个值相同,LOC如何给出最小的键,如-(1=8),(2=8) 它是如何给出正确答案的

static int migratoryBirds(List<Integer> arr)
{
    Map<Integer, Integer> birds = new HashMap<>();
    int key = 0;

    for(int i = 0; i < arr.size(); i++)
    {
        if(birds.containsKey(arr.get(i)))
        {
            birds.put(arr.get(i), birds.get(arr.get(i)) + 1);
        }else
        {
            birds.put(arr.get(i), 1);
        }
    }
 
    **key = Collections.max(birds.entrySet(), Map.Entry.comparingByValue()).getKey();**
    return key;
}
static int migratoryBirds(列表arr)
{
Map birds=newhashmap();
int键=0;
对于(int i=0;i
如果两个条目具有最大值,将任意选择其中一个条目。在这种情况下,1=8和2=8都是正确答案。它不一定给出正确答案。它只是碰巧给出了正确的答案。