簡體   English   中英

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

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

我有以下課程:

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);

我想使用Java流從給定的輸入列表中創建Map<String, List<Result>> 另外,列表值需要根據字段(x和y)以升序排序

我需要輸出映射如下:

{"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)]
}

映射的鍵是由冒號連接的systemid和文件名的組合。 列表的值首先需要按x排序,然后按y排序。

就像是:

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);

    }
}

輸出:{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)]}}

試試這個,它在我的機器上工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM