首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何基于地图优先级重置列表

如何基于地图优先级重置列表
EN

Stack Overflow用户
提问于 2022-09-26 16:25:26
回答 2查看 58关注 0票数 -1

在Java中,我使用了List和Map,它们包含与基于位置字段的查找相似的参数,并根据优先级重置列表。

我们怎么做呢?

代码语言:javascript
复制
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;
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-09-26 18:10:02

我实现了如下逻辑:

代码语言:javascript
复制
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);
票数 0
EN

Stack Overflow用户

发布于 2022-09-27 12:29:20

为了实现您想要的目标,应该有一个与其优先级相匹配的地图位置(与您实际拥有的位置相反)。就像这样:

代码语言:javascript
复制
Map<String, Integer> locationPriorities = new HashMap<>();
locationPriorities.put("E1111", 1);
locationPriorities.put("E2222", 2);
// etc...

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

代码语言:javascript
复制
Map<String, Integer> locationPriorities =
    getPriorityConfig().entrySet()
                       .stream()
                       .collect(toMap(e -> e.getValue().location, e -> e.getKey()));

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

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

代码语言:javascript
复制
List<Student> newStudents =
    students.stream()
            .sorted(comparing(s -> locationPriorities.get(s.location)))
            .collect(toList());

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

代码语言:javascript
复制
students.sort(comparing(s -> locationPriorities.get(s.location)));

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

代码语言:javascript
复制
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73857123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档