Stream流使用实例

2023-08-20 16:49:49哔哩哔哩

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

("a");("b");("c");("d");


(资料图)

// 列表转列表:直接toList

List<String> ans1 = ()

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

.collect(()); 

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

Map<String, String> ans2 = ()

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

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

// map转map也是在toMap做好映射关系

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

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

.collect((x -> (), x -> ())); 

// map可以改元素Entry为String

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

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

.map(x -> () + ())

.collect(()); 

("列表转列表" + ans1);

("列表转map" + ans2);

("map转map" + ans3);

("map转列表" + ans4); 

标签:

企业

小鹏汽车累计交付量突破20万台
2022-06-21
详细行程
让群众遇事不烦办事不难
2022-02-15
详细行程
跳台滑雪男子团体:中国队亮相
2022-02-15
详细行程