在Java中,我使用了List和Map,它们包含与基于位置字段的查找相似的参数,并根据优先级重置列表。
我们怎么做呢?
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;
}
}发布于 2022-09-26 18:10:02
我实现了如下逻辑:
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);发布于 2022-09-27 12:29:20
为了实现您想要的目标,应该有一个与其优先级相匹配的地图位置(与您实际拥有的位置相反)。就像这样:
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;https://stackoverflow.com/questions/73857123
复制相似问题