简体   繁体   English

使用Java 8流API创建String映射和排序列表

[英]Create a map of String and sorted list using Java 8 streams API

I have the following classes: 我有以下课程:

class Data {
        String systemId;
        String fileName;
        int x;
        int y;

        Data(String systemId, String fileName, int x, int y) {
            this.systemId = systemId;
            this.fileName = fileName;
            this.x = x;
            this.y = y;
        }
        public String getSystemId() {
            return systemId;
        }

        public void setSystemId(String systemId) {
            this.systemId = systemId;
        }

        public String getFileName() {
            return fileName;
        }

        public void setFileName(String fileName) {
            this.fileName = fileName;
        }

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }
    }


class Result {
        int x;
        int y;

        Result(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }
    }

List<Data> dataList = new ArrayList<>();
Data x1 = new Data("n1", "f1", 3, 4);
Data x2 = new Data("n1", "f1", 1, 2);
Data x3 = new Data("n1", "f1", 5, 6);
Data x4 = new Data("n1", "f2", 7, 8);
Data x5 = new Data("n2", "f1", 9, 10);
Data x6 = new Data("n2", "f2", 11, 12);
Data x7 = new Data("n3", "f1", 13, 14);
Data x8 = new Data("n4", "f1", 15, 16);
Data x9 = new Data("n1", "f1", 5, 10);
Data x10 = new Data("n1", "f1", 5, 2);

dataList.add(x1);dataList.add(x2);dataList.add(x3);dataList.add(x4);dataList.add(x5);dataList.add(x6);dataList.add(x7);dataList.add(x8);

I want to use Java streams to create a Map<String, List<Result>> out of the given input list. 我想使用Java流从给定的输入列表中创建Map<String, List<Result>> Also, the list values needs to be sorted in ascending order according to fields (x and y) 另外,列表值需要根据字段(x和y)以升序排序

I need the output map to be as follows: 我需要输出映射如下:

{"n1:f1" : [(1, 2), (3, 4), (5, 2), (5,6), (5,10)]
 "n1:f2" : [(7, 8)]
 "n2:f1" : [(9, 10)]
 "n2:f2" : [(11, 12)]
 "n3:f1" : [(13, 14)]
 "n4:f1" : [(15, 16)]
}

The key for the map is the combination of systemid and filename concatenated by colon. 映射的键是由冒号连接的systemid和文件名的组合。 The values of the list needs to be first sorted by x and then y. 列表的值首先需要按x排序,然后按y排序。

something like: 就像是:

Map<String, List<Result>> collect = dataList.stream()
            .sorted(Comparator.comparing(Data::getX).thenComparing(Data::getY))
            .collect(Collectors.groupingBy(d ->  d.getSystemId() + ":" + d.getFileName(),
                    Collectors.mapping(d -> new Result(d.getX(), d.getY()), toList())));
class Data {
        String systemId;
        String fileName;
        int x;
        int y;

        Data(String systemId, String fileName, int x, int y) {
            this.systemId = systemId;
            this.fileName = fileName;
            this.x = x;
            this.y = y;
        }
        public String getSystemId() {
            return systemId;
        }

        public void setSystemId(String systemId) {
            this.systemId = systemId;
        }

        public String getFileName() {
            return fileName;
        }

        public void setFileName(String fileName) {
            this.fileName = fileName;
        }

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }
    }


class Result {
        int x;
        int y;

        Result(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }

        @Override
        public String toString() {

        return "("+getX() +","+ getY()+")";
        }
    }

public class MainClass {
    public static void main(String[] args) {

        List<Data> dataList = new ArrayList<Data>();
        Data x1 = new Data("n1", "f1", 3, 4);
        Data x2 = new Data("n1", "f1", 1, 2);
        Data x3 = new Data("n1", "f1", 5, 6);
        Data x4 = new Data("n1", "f2", 7, 8);
        Data x5 = new Data("n2", "f1", 9, 10);
        Data x6 = new Data("n2", "f2", 11, 12);
        Data x7 = new Data("n3", "f1", 13, 14);
        Data x8 = new Data("n4", "f1", 15, 16);
        Data x9 = new Data("n1", "f1", 5, 10);
        Data x10 = new Data("n1", "f1", 5, 2);

        dataList.add(x1);
        dataList.add(x2);
        dataList.add(x3);
        dataList.add(x4);
        dataList.add(x5);
        dataList.add(x6);
        dataList.add(x7);
        dataList.add(x8);
        dataList.add(x9);
        dataList.add(x10);

        Map<String, List<Result>> collect = dataList.stream()
                .sorted(Comparator.comparing(Data::getX).thenComparing(Data::getY))
                .collect(Collectors.groupingBy(d -> d.getSystemId() + ":" + d.getFileName(),
                        Collectors.mapping(d -> new Result(d.getX(), d.getY()), Collectors.toList())));

        Map<String, List<Result>> sortedMap = new TreeMap<String, List<Result>>(collect);

        System.out.println(sortedMap);

    }
}

Output :{n1:f1=[(1,2), (3,4), (5,2), (5,6), (5,10)], n1:f2=[(7,8)], n2:f1=[(9,10)], n2:f2=[(11,12)], n3:f1=[(13,14)], n4:f1=[(15,16)]} 输出:{n1:f1 = [(1,2),(3,4),(5,2),(5,6),(5,10)],n1:f2 = [(7,8)] ,n2:f1 = [(9,10)],n2:f2 = [(11,12)],n3:f1 = [(13,14)],n4:f1 = [(15,16)]}}

Try this, It is working on my machine. 试试这个,它在我的机器上工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 转换列表时过滤出值 <SomeObject> 到地图 <String, Set<String> &gt;使用Java 8流API - Filter out values when converting List<SomeObject> to Map<String, Set<String>> using Java 8 streams API 转换清单 <E> 到地图 <String, List<String> &gt;使用Java 8流 - convert List<E> to Map<String, List<String>> using java 8 streams 创建 Map <string, list<object> &gt; 列表元素排序的地方,使用 Stream API</string,> - Create Map<String, List<Object>> where the elements of the lists are sorted, using Stream API 如何生成列表的 Map 的 Map(地图<string, map<enum, list<string> &gt;&gt;) 在 java 中使用流</string,> - How do I generate a Map of Map of List (Map<String, Map<Enum, List<String>>>) in java using streams 使用Java 8流,如何转换Map <String, Map<String, List<Person> &gt;&gt;到地图 <Integer, Map<String, Integer> &gt;? - Using Java 8 streams, how to transform Map<String, Map<String, List<Person>>> to Map<Integer, Map<String, Integer>>? 使用Java-8 Streams API将字符串列表转换为Map - Convert List of Strings into Map using Java-8 Streams API Java 流:使用不同 object 类型的多个列表创建 Map - Java Streams: Create Map Using Multiple List of different object types 删除列表中的重复项<map<string, object> &gt; 在 java 中使用流</map<string,> - Remove duplicates in List<Map<String, Object>> using Streams in java 过滤器 Map <string,list<object> &gt; 使用 Java 流</string,list<object> - Filter Map<String,List<Object>> Using Java Streams 转换 HttpServletRequest.getParameterMap() [Map<string, string[]> ] 至 Map <string, list<string> &gt; 使用 Java 流</string,></string,> - Convert HttpServletRequest.getParameterMap() [Map<String, String[]>] to Map<String, List<String>> using Java Streams
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM