根据地图中的值对列表进行排序
Sort a List based on the values in a map
我正在尝试根据 Map
中的值对 List
个对象进行排序。
LinkedHashMap<String, Integer> map = new HashMap<>();
map.put("1233", 30);
map.put("1562", 5);
map.put("1243", 10);
map.put("1872", 20);
根据这张地图中的值,我想对以下列表进行排序:
l = [
{ id:1562,name:"xxx" },
{ id:1233,name:"yyy" },
{ id:1243,name:"zzz" },
{ id:1872,name:"xxx" }
]
预期输出:
l = [
{ id:1233,name:"yyy" },
{ id:1872,name:"xxx" },
{ id:1243,name:"zzz" },
{ id:1562,name:"xxx" }
]
我已经尝试过特定条件下的冒泡排序,但它花费了太多时间:
int n = l.size();
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (map.get(l.get(j) > map.get(l.get(j+1)) {
swap(j, l);
}
}
}
private void swap(int j, List<L> l) {
l temp = l.get(j);
l.set(j, l.get(j + 1));
l.set(j + 1, temp);
}
有没有更好的选择,或者其他数据结构?
您可以使用排序方法和比较器在一行中完成,但不确定它是否对您的用例足够有效。
import java.util.Comparator;
.....
l.sort(Comparator.comparing(f -> map.get(f.getId()),Comparator.reverseOrder()));
据我了解,您想根据我的 map[=31] 返回的值对 条目列表 List<Map.Entry<Integer, String>>
进行排序=] Map<String, Integer>
每个 key 降序.
为此,您需要定义一个 Comparator,它将根据输入键比较列表元素:
Comparator<Map.Entry<Integer, String>> byKeyDesc =
Comparator.<Map.Entry<Integer, String>>comparingInt(entry ->
map.get(String.valueOf(entry.getKey()))).reversed();
然后应用 list.sort()
.
public static void main(String[] args) {
Map<String, Integer> map =
Map.of( "1233", 30, "1562", 5,
"1243", 10, "1872", 20);
List<Map.Entry<Integer, String>> entryList = new ArrayList<>(
List.of(Map.entry(1562, "xxx"),
Map.entry(1233, "yyy"),
Map.entry(1243, "zzz"),
Map.entry(1872, "xxx"))
);
Comparator<Map.Entry<Integer, String>> byKeyDesc =
Comparator.<Map.Entry<Integer, String>>comparingInt(entry ->
map.get(String.valueOf(entry.getKey()))).reversed();
entryList.sort(byKeyDesc);
entryList.forEach(System.out::println);
}
输出
1233=yyy
1872=xxx
1243=zzz
1562=xxx
我正在尝试根据 Map
中的值对 List
个对象进行排序。
LinkedHashMap<String, Integer> map = new HashMap<>();
map.put("1233", 30);
map.put("1562", 5);
map.put("1243", 10);
map.put("1872", 20);
根据这张地图中的值,我想对以下列表进行排序:
l = [
{ id:1562,name:"xxx" },
{ id:1233,name:"yyy" },
{ id:1243,name:"zzz" },
{ id:1872,name:"xxx" }
]
预期输出:
l = [
{ id:1233,name:"yyy" },
{ id:1872,name:"xxx" },
{ id:1243,name:"zzz" },
{ id:1562,name:"xxx" }
]
我已经尝试过特定条件下的冒泡排序,但它花费了太多时间:
int n = l.size();
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (map.get(l.get(j) > map.get(l.get(j+1)) {
swap(j, l);
}
}
}
private void swap(int j, List<L> l) {
l temp = l.get(j);
l.set(j, l.get(j + 1));
l.set(j + 1, temp);
}
有没有更好的选择,或者其他数据结构?
您可以使用排序方法和比较器在一行中完成,但不确定它是否对您的用例足够有效。
import java.util.Comparator;
.....
l.sort(Comparator.comparing(f -> map.get(f.getId()),Comparator.reverseOrder()));
据我了解,您想根据我的 map[=31] 返回的值对 条目列表 List<Map.Entry<Integer, String>>
进行排序=] Map<String, Integer>
每个 key 降序.
为此,您需要定义一个 Comparator,它将根据输入键比较列表元素:
Comparator<Map.Entry<Integer, String>> byKeyDesc =
Comparator.<Map.Entry<Integer, String>>comparingInt(entry ->
map.get(String.valueOf(entry.getKey()))).reversed();
然后应用 list.sort()
.
public static void main(String[] args) {
Map<String, Integer> map =
Map.of( "1233", 30, "1562", 5,
"1243", 10, "1872", 20);
List<Map.Entry<Integer, String>> entryList = new ArrayList<>(
List.of(Map.entry(1562, "xxx"),
Map.entry(1233, "yyy"),
Map.entry(1243, "zzz"),
Map.entry(1872, "xxx"))
);
Comparator<Map.Entry<Integer, String>> byKeyDesc =
Comparator.<Map.Entry<Integer, String>>comparingInt(entry ->
map.get(String.valueOf(entry.getKey()))).reversed();
entryList.sort(byKeyDesc);
entryList.forEach(System.out::println);
}
输出
1233=yyy
1872=xxx
1243=zzz
1562=xxx