亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 Java 8 中的列表創建字符串和對象的映射

從 Java 8 中的列表創建字符串和對象的映射

慕斯王 2023-02-16 16:24:21
我有以下課程: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", 1, 2);Data x2 = new Data("n1", "f1", 3, 4);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);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)按升序排序映射的鍵是由冒號連接的 systemid 和文件名的組合。我嘗試通過 systemid 和文件名的組合進行分組,但無法繼續使用該方法。
查看完整描述

1 回答

?
阿晨1998

TA貢獻2037條經驗 獲得超6個贊

您可以將groupingBy收集器與mapping下游一起使用:

Map<String, List<Result>> map = data.stream()
        .collect(Collectors.groupingBy(a -> a.getSystemId() + ":" + a.getFileName()
                , Collectors.mapping(a -> new Result(a.getX(), a.getY()),
                        Collectors.toList())));


查看完整回答
反對 回復 2023-02-16
  • 1 回答
  • 0 關注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號