如何根据地图优先级重置列表



在Java中,我有List and Map,它保存类似于基于位置字段查找的参数,并根据优先级重置List。

我们该怎么做?

import lombok.AllArgsConstructor;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Demo {
private Map<Integer, DataConfig> getPriorityConfig() {
// Here 1, 2... etc are priorities
Map<Integer, DataConfig> MAP = new HashMap<>();
MAP.put(1, new DataConfig("E", "3333"));
MAP.put(2, new DataConfig("E", "1111"));
MAP.put(3, new DataConfig("E", "5555"));
MAP.put(4, new DataConfig("E", "2222"));
MAP.put(5, new DataConfig("E", "4444"));
return MAP;
}
public static void main(String[] args) {
List<Student> students = Arrays.asList(
new Student(4, "E4444", "John"),
new Student(2, "E2222", "Mike"),
new Student(1, "E1111", "Jane"),
new Student(3, "E3333", "Victoria")
);
//  Arrange List based on Priority and create new List, output should be
// here E3333 has priority-1, E1111 has priority-2, E5555 has priority-3 etc
List<Student> newStudents = Arrays.asList(
new Student(3, "E3333", "Victoria"),
new Student(1, "E1111", "Jane"),
new Student(2, "E2222", "Mike"),
new Student(4, "E4444", "John")
);
// For me its  very important to processed based on priority only
}
@AllArgsConstructor
static class DataConfig {
private String type;
private String location;
}
@AllArgsConstructor
static class Student {
private int id;
private String location;
private String name;
}
}

我实现了如下逻辑:

List<Student> students = Arrays.asList(
new Student(4, "E4444", "John"),
new Student(2, "E2222", "Mike"),
new Student(1, "E1111", "Jane"),
new Student(3, "E3333", "Victoria")
);
Map<Integer, DataConfig> priorityConfig = getPriorityConfig();
Map<String, Integer> MAP = new HashMap<>();
// location and Pri
for (Map.Entry<Integer, DataConfig> entry : priorityConfig.entrySet()) {
MAP.put(entry.getValue().type + entry.getValue().location, entry.getKey());
}
Map<Integer, Student> integerStudentMap = new HashMap<>();
for (Student s: students) {
Integer integer = MAP.get(s.getLocation());
integerStudentMap.put(integer, s);
}
Map<Integer, Student> collect = integerStudentMap.entrySet()
.stream()
.sorted(comparingByKey())
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue(), (e1, e2) -> e2));
System.out.println(collect);
List<Student> list = new ArrayList<>(collect.values());
System.out.println(list);

为了实现您想要的目标,应该有一个将位置与其优先级相匹配的地图(与实际位置相反(。类似这样的东西:

Map<String, Integer> locationPriorities = new HashMap<>();
locationPriorities.put("E1111", 1);
locationPriorities.put("E2222", 2);
// etc...

或者,如果您从其他地方检索映射,您仍然可以将其转换为您想要的格式:

Map<String, Integer> locationPriorities =
getPriorityConfig().entrySet()
.stream()
.collect(toMap(e -> e.getValue().location, e -> e.getKey()));

一旦有了上面的映射,排序就变得非常简单了。

您可以收集一个新的排序列表:

List<Student> newStudents =
students.stream()
.sorted(comparing(s -> locationPriorities.get(s.location)))
.collect(toList());

或者对现有列表进行排序:

students.sort(comparing(s -> locationPriorities.get(s.location)));

确保导入这些静态方法(或使用限定调用(:

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;

最新更新