有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Entry语句的功能是什么?

所以我对列表非常陌生,我想知道我是否正确理解了下面的代码,更具体地说,Entry语句的功能是什么

是因为Entry语句,myList能够接受myHashMap中的条目,以便稍后使用comparator对列表进行排序吗

   List<Entry<String, Integer>> myList = new LinkedList<Entry<String, Integer>>(myHashMap.entrySet());

    Collections.sort(myList, new Comparator<Entry<String, Integer>>(){

        @Override
        public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2){

            return o1.getValue().compareTo(o2.getValue());
        }

    });

共 (1) 个答案

  1. # 1 楼答案

    Is it because of the Entry statement that myList is capable of accepting the entries from myHashMap in order to later sort the list with the comparator?

    没错

    如你所知,A HashMap的结构如下:

    {
        key1: value1,
        key2: value2,
        key3: value3,
        ...
    }
    

    每个KVP被称为Entry。换句话说,一个HashMap可以由一组Entry对象来表示。这正是entrySet方法所做的。您可以使用entrySet将所有KVP作为Entry对象获取,将它们放入一个列表中并对它们进行排序