gpt4 book ai didi

java - 比较 Map 值并排序

转载 作者:行者123 更新时间:2023-12-02 09:48:58 25 4
gpt4 key购买 nike

这是我的课

公共(public)类人{

int id;
int salary;
Map<String,List<String>> house;
//getter and setters omitted for brevity

public static void main(String[] args) {
// TODO Auto-generated method stub

Map<String,List<String>> map =new HashMap<>();

map.put("KEY_1",Arrays.asList("3") ) ;
map.put("KEY_2",Arrays.asList("House_1") ) ;
map.put("KEY_3",Arrays.asList("House_13") ) ;



Map<String,List<String>> map2 =new HashMap<>();

map2.put("KEY_1",Arrays.asList("1") ) ;
map2.put("KEY_2",Arrays.asList("House_2") ) ;
map2.put("KEY_3",Arrays.asList("House_22") ) ;


Map<String,List<String>> map3 =new HashMap<>();

map3.put("KEY_1",Arrays.asList("2") ) ;
map3.put("KEY_2",Arrays.asList("House_3") ) ;
map3.put("KEY_3",Arrays.asList("House_33") ) ;


Person p1 = new Person(1,1000, map );

Person p2 = new Person(2,2000, map2 );

Person p3 = new Person(3,3000, map3 );


List<Person> personList = new ArrayList();
personList.add(p1);
personList.add(p2);
personList.add(p3);



for (Person p : personList)
{

for(Entry<String, List<String>> personMap : p.getHouse().entrySet() )

{

System.out.println( "value of key is -->" + personMap.getKey() + " value is " + personMap.getValue() );

}
}

}
}

我的要求是首先根据 KEY_1 值对数据进行排序,然后如果 KEY_2 的值?我如何编写比较器或为此进行比较?

当前 O/p 是

value of key is -->KEY_1 value is [3]
value of key is -->KEY_3 value is [House_13]
value of key is -->KEY_2 value is [House_1]
value of key is -->KEY_1 value is [1]
value of key is -->KEY_3 value is [House_22]
value of key is -->KEY_2 value is [House_2]
value of key is -->KEY_1 value is [2]
value of key is -->KEY_3 value is [House_33]
value of key is -->KEY_2 value is [House_3]

但我的预期输出是

value of key is -->KEY_1 value is [3] value of key is -->KEY_3 value is [House_13] value of key is -->KEY_2 value is [House_1]

value of key is -->KEY_1 value is [2] value of key is -->KEY_3 value is [House_33] value of key is -->KEY_2 value is [House_3]

value of key is -->KEY_1 value is [1] value of key is -->KEY_3 value is [House_22] value of key is -->KEY_2 value is [House_2]

即,如果具有 key_1 的对象具有最高值,则它应该排在第一位,然后我们也需要根据 KEY_2 对它进行排序。

最佳答案

您的示例不完整,请始终发布 Minimal, Reproducible Example并提供清晰的问题描述。例如,您的 getElements() 方法未包含在内,并且 messages 未定义。

但是,据我从不完整的示例中可以看出,您希望按照字符串 keys 的词汇顺序(升序)遍历映射。

为此,您必须知道 Map.entrySet() 返回类 Set 的实例,并且您使用 for-each 循环,该循环使用其迭代器和Set 的迭代器不保证条目的任何特定迭代顺序。

但是您可以使用类 SortedMap :

A Map that further provides a total ordering on its keys. The map is ordered according to the natural ordering of its keys, or by a Comparator typically provided at sorted map creation time. This order is reflected when iterating over the sorted map's collection views (returned by the entrySet, keySet and values methods). Several additional operations are provided to take advantage of the ordering. (This interface is the map analogue of SortedSet.)

如果您想根据而不是键来遍历 map ,请参阅现有答案 Sort a Map<Key, Value> by values .

关于java - 比较 Map 值并排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56456993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com