无码av一区二区三区无码,在线观看老湿视频福利,日韩经典三级片,成 人色 网 站 欧美大片在线观看

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Stream流使用實例

2023-08-20 17:15 作者:努力奔跑的的肥崽俊  | 我要投稿

ArrayList<String> objects = new ArrayList<>();

objects.add("a");objects.add("b");objects.add("c");objects.add("d");


// 列表轉列表:直接toList

List<String> ans1 = objects.stream()

? ? ? ?.filter(x -> x.equals("a") || x.equals("b"))

? ? ? ?.collect(Collectors.toList());?


// 列表轉map在最后toMap即可由String轉Entry

Map<String, String> ans2 = objects.stream()

? ? ? ?.filter(x -> x.equals("c") || x.equals("d"))

? ? ? ?.collect(Collectors.toMap(x -> x, x -> x + "1", (a, b) -> b));?


// map轉map也是在toMap做好映射關系

Map<String, String> ans3 = ans2.entrySet().stream()

? ? ? ?.filter(x -> x.getKey().equals("c"))

? ? ? ?.collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));?


// map可以改元素Entry為String

List<String> ans4 = ans2.entrySet().stream()

? ? ? ?.filter(x -> x.getKey().equals("d"))

? ? ? ?.map(x -> x.getKey() + x.getValue())

? ? ? ?.collect(Collectors.toList());?


System.out.println("列表轉列表" + ans1);

System.out.println("列表轉map" + ans2);

System.out.println("map轉map" + ans3);

System.out.println("map轉列表" + ans4);?

Stream流使用實例的評論 (共 條)

分享到微博請遵守國家法律
黄龙县| 龙游县| 汾西县| 双牌县| 霍林郭勒市| 望都县| 益阳市| 平利县| 睢宁县| 蕉岭县| 晴隆县| 靖安县| 连平县| 秀山| 桦南县| 浏阳市| 玉树县| 台安县| 胶南市| 肇庆市| 福贡县| 乌苏市| 故城县| 友谊县| 嘉祥县| 东莞市| 泸定县| 谢通门县| 竹北市| 肥乡县| 平顺县| 涟水县| 莆田市| 临泉县| 徐汇区| 宁安市| 佳木斯市| 鸡东县| 进贤县| 烟台市| 龙井市|