Stream流之综合练习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.Lucky.test;
 
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
/**
 * 练习1: 数据过滤,定义一个Int类型的集合,过滤掉奇数的数据,只留下偶数
 */
public class test1 {
    public static void main(String[] args) {
        ArrayList<Integer> strInt=new ArrayList<>();
        Collections.addAll(strInt,1,2,3,4,5,6,7,8,9,10,12,15);
 
        //过滤操作
        List<Integer> collect = strInt.stream().filter(val -> val % 2 == 0)
                                .collect(Collectors.toList());
 
        //遍历数据
        Iterator<Integer> iterator = collect.iterator();
        while (iterator.hasNext()){
            System.out.print(iterator.next()+"\t");
        }
 
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.Lucky.test;
 
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
/**
 * 练习2: 创建一个ArrayList集合,添加字符串,格式是 "唯易,22"
 *        要求:保留年龄大于或等于25的人,将结果保存到Map集合中【姓名作为键,年龄作为值】
 */
public class test2 {
    public static void main(String[] args) {
        ArrayList<String> strList=new ArrayList<>();
        Collections.addAll(strList,"唯易,22","欣怡,26","维嘉,32","未央,25","Bug,42");
 
        //过滤操作
        Map<String, Integer> collect = strList.stream()
                .filter(val -> Integer.parseInt(val.split(",")[1]) >= 25)
                .collect(Collectors.toMap(k -> k.split(",")[0],
                                          v -> Integer.parseInt(v.split(",")[1])));
        System.out.println(collect);
 
        //遍历数据
        //①keySet
        Set<String> strings = collect.keySet();
        Iterator<String> iterator = strings.iterator();
        while (iterator.hasNext()){
            String name=iterator.next();
            int val=collect.get(name);
            System.out.println(name+":"+val);
        }
 
        ///②entrySet
        Set<Map.Entry<String, Integer>> entries = collect.entrySet();
        Iterator<Map.Entry<String, Integer>> iter = entries.iterator();
        while (iter.hasNext()){
            System.out.print(iter.next()+"\t");
        }
 
    }
}

  练习3材料:

package com.Lucky.test;

public class Actor {
private String name;
private int age;

public Actor() {
}

public Actor(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
return "Actor{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.Lucky.test;
 
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
/**
 * 练习3:
 * 现在有两个ArrayList集合,
 * 第一个集合中:存储6名男演员的名字和年龄。
 * 第二个集合中:存储6名女演员的名字和年龄。
 * 姓名和年龄中间用逗号隔开。比如:张三,23
 * 要求完成如下的操作:1,男演员只要名字为3个字的前两人
 *                2,女演员只要姓杨的,并且不要第一个
 *                3,把过滤后的男演员姓名和女演员姓名合并到一起
 *                4,将上一步的演员信息封装成Actor对象。
 *                5,将所有的演员对象都保存到List集合中。
 *                备注:演员类Actor,属性有:name,age
 */
public class test3 {
    public static void main(String[] args) {
        ArrayList<String> ListOne=new ArrayList<>();
        Collections.addAll(ListOne,"唯易人生,22","李四时,26","王五嚓,32","陈六狗,25","Bug,42","张三炮,23");
        ArrayList<String> ListTwo=new ArrayList<>();
        Collections.addAll(ListTwo,"小丽,22","杨欣怡,26","维嘉,32","杨未央,25","小欣,42","杨薇薇,23");
        //过滤操作
        Stream<String> one = ListOne.stream().filter(name -> name.split(",")[0].length() == 3).limit(2);
        Stream<String> two = ListTwo.stream().filter(name -> name.split(",")[0].startsWith("杨")).skip(1);
 
 
        System.out.println("---------初级做法-------------");
//        Stream<Actor> actorStream = Stream.concat(two, one)
//                .map(new Function<String, Actor>() {
//            @Override
//            public Actor apply(String o) {
//                String str = o.split(",")[0];
//                int val = Integer.parseInt(o.split(",")[1]);
//
//                return new Actor(str, val);
//            }
//        });
//        List<Actor> collect = actorStream.collect(Collectors.toList());
//        System.out.println(collect);
 
        System.out.println("---------lamda表达式做法-------------");
        Stream<Actor> Stream1 = Stream.concat(one, two).map(val -> new Actor(val.split(",")[0],
                Integer.parseInt(val.split(",")[1])));
        List<Actor> lamdaCollect = Stream1.collect(Collectors.toList());
        System.out.println(lamdaCollect);
    }
}
posted @   唯易人生  阅读(90)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示