对具有地图作为输入的列表进行排序
Sort a list having map as input
我有一个列表,作为元素的是地图我想根据地图元素值对这个列表进行排序
[{id=45964953, count=1}, {id=46009636, count=1}, {id=45936991, count=1}, {id=45984035, count=2}, {id=45951961, count=1}, {id=45399668, count=31}]
我需要根据计数值对其进行排序。可以在java
完成吗
输出应该是这样的
[ {id=45399668, count=31},{id=45984035, count=2}, {id=45964953, count=1}, {id=46009636, count=1}, {id=45936991, count=1}, {id=45951961, count=1}]
您可以创建一个实现 Comparable<Map<String,Integer>>
的 class(假设 String
和 Integer
以及您的地图的键和值)并根据您的地图比较两个地图标准。
然后您可以将列表和比较器传递给 Collections.sort()。
Java8出来有一段时间了。用这个打动你的老师:
list = list.stream()
.sorted((m1, m2) -> Integer.compare(m2.get("count"),m1.get("count")))
.collect(Collectors.toList());
下面是一些测试代码:
List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>() {{
add(new HashMap<String, Integer>() {{put("id",45964953); put("count", 1);}});
add(new HashMap<String, Integer>() {{put("id",46009636); put("count", 1);}});
add(new HashMap<String, Integer>() {{put("id",45936991); put("count", 1);}});
add(new HashMap<String, Integer>() {{put("id",45984035); put("count", 2);}});
add(new HashMap<String, Integer>() {{put("id",45951961); put("count", 1);}});
add(new HashMap<String, Integer>() {{put("id",45399668); put("count", 31);}});
}};
list = list.stream().sorted((m1, m2) -> Integer.compare(m2.get("count"), m1.get("count"))).collect(Collectors.toList());
System.out.println(list);
输出:
[{count=31, id=45399668}, {count=2, id=45984035}, {count=1, id=45964953}, {count=1, id=46009636}, {count=1, id=45936991}, {count=1, id=45951961}]
我有一个列表,作为元素的是地图我想根据地图元素值对这个列表进行排序
[{id=45964953, count=1}, {id=46009636, count=1}, {id=45936991, count=1}, {id=45984035, count=2}, {id=45951961, count=1}, {id=45399668, count=31}]
我需要根据计数值对其进行排序。可以在java
完成吗输出应该是这样的
[ {id=45399668, count=31},{id=45984035, count=2}, {id=45964953, count=1}, {id=46009636, count=1}, {id=45936991, count=1}, {id=45951961, count=1}]
您可以创建一个实现 Comparable<Map<String,Integer>>
的 class(假设 String
和 Integer
以及您的地图的键和值)并根据您的地图比较两个地图标准。
然后您可以将列表和比较器传递给 Collections.sort()。
Java8出来有一段时间了。用这个打动你的老师:
list = list.stream()
.sorted((m1, m2) -> Integer.compare(m2.get("count"),m1.get("count")))
.collect(Collectors.toList());
下面是一些测试代码:
List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>() {{
add(new HashMap<String, Integer>() {{put("id",45964953); put("count", 1);}});
add(new HashMap<String, Integer>() {{put("id",46009636); put("count", 1);}});
add(new HashMap<String, Integer>() {{put("id",45936991); put("count", 1);}});
add(new HashMap<String, Integer>() {{put("id",45984035); put("count", 2);}});
add(new HashMap<String, Integer>() {{put("id",45951961); put("count", 1);}});
add(new HashMap<String, Integer>() {{put("id",45399668); put("count", 31);}});
}};
list = list.stream().sorted((m1, m2) -> Integer.compare(m2.get("count"), m1.get("count"))).collect(Collectors.toList());
System.out.println(list);
输出:
[{count=31, id=45399668}, {count=2, id=45984035}, {count=1, id=45964953}, {count=1, id=46009636}, {count=1, id=45936991}, {count=1, id=45951961}]