【Java】Stream流

Stream流相当于一条流水线,把数据放上之后对数据进行各种处理,最后得到结果。

一、使用步骤

(1)得到一条Stream流,并把数据放入Stream流

(2)使用中间方法对流水线上的数据进行操作

(3)使用终结方法对流水线上的数据进行操作

二、Stream流的获取方法

(1)单列集合

①创建单列集合

②向单列集合中添加元素

③调用stream()方法获取Stream流,并遍历打印每一个元素(lambda表达式)

public class Test1 {
    //单列集合获取Stream流
    public static void main(String[] args) {
        //创建单列集合
        ArrayList<String> list = new ArrayList<>();
        //向单列集合中添加元素
        Collections.addAll(list,"a","b","c","d","e");
        //调用stream方法获取stream流,并循环打印每一个元素
        list.stream().forEach(s -> System.out.println(s));
    }
}

(2)双列集合

①创建双列集合

②向双列集合中添加元素

③调用entrySet()方法获得Set集合(单列集合,元素为键值对),再调用stream()方法获取Stream流,遍历打印Set集合中的每一个元素(键值对)

④调用keySet()方法获得Set集合(元素为键),再调用stream()方法获取Stream流,遍历打印元素

(③④是两种获取Stream流的方法)

public class Test2 {
    //双列集合获取Stream流
    public static void main(String[] args) {
        HashMap<String,Integer> hm = new HashMap<>();
        hm.put("a",1);
        hm.put("b",2);
        hm.put("c",3);
        hm.put("d",4);
        hm.put("e",5);
        //获取键值对
        hm.entrySet().stream().forEach(s-> System.out.println(s));
        //获取键
        hm.keySet().stream().forEach(s -> System.out.println(s));
    }
}

(3)数组

①创建数组并加入数据

②调用Arrays的stream()方法,并把数组传入

③遍历打印每一个元素

public class Test3 {
    //数组获得Stream流
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5};
        Arrays.stream(arr).forEach(i-> System.out.println(i));
    }
}

(4)零散元素

①用Sream.of()方法,把数据放入

②遍历打印每一个元素

public class Test4 {
    //零散数据获得Stream流
    public static void main(String[] args) {
        Stream.of(1,2,3,4,5).forEach(i -> System.out.println(i));
    }
}

注意:   ①放入的数据必须是同一数据类型

              ②这种方法可以处理零散数据,也可以处理数组,但是数组内的数据只能是引用数据类                      型,如果是基本数据类型数组会被当作是一个元素

三、Stream流的中间方法

 注意:①中间方法返回新的Stream流,原来的Stream流只能使用一次

           ②修改Stream流中的数据不会影响原来集合或数组中的数据

转换流中的数据类型

例:获取每个人的年龄,年龄和姓名之间用"-"隔开

public class Test5 {
    public static void main(String[] args) {
        //创建集合并添加元素
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list,"张三-19","李四-20","王五-100","赵六-90");
        list.stream().map(new Function<String, Integer>() {
            @Override
            public Integer apply(String s) {
                String[] split = s.split("-");//将字符串从"-"分隔开,得到一个数组
                String s1 = split[1];//年龄在下标为1的数组中
                int age = Integer.parseInt(s1);
                return age;
            }
        }).forEach(new Consumer<Integer>() {//遍历集合
            @Override
            public void accept(Integer integer) {
                System.out.println(integer);
            }
        });
    }
}

在用lambda表达式化简后

public class Test5 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list,"张三-19","李四-20","王五-100","赵六-90");
        list.stream().map(s-> Integer.parseInt(s.split("-")[1])).forEach(integer-> System.out.println(integer));
    }
}

四、Stream流中的终结方法

(count()为统计)

(1)toArray方法

public class Test6 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("张三");
        list.add("李四");
        list.add("王五");
        list.stream().toArray(new IntFunction<String[]>() {
            @Override
            public String[] apply(int value) {//value表示返回数组的长度
                return new String[value];
            }
        });
    }
}

用lambda表达式可表示为

public class Test6 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("张三");
        list.add("李四");
        list.add("王五");
        list.stream().toArray( value -> new String[value]);
    }
}

(2)collect方法

能够把流水线上的数据放入到list集合,set集合和map集合中

①list集合和set集合

public class Test7 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("张三-男-20");
        list.add("李四-男-30");
        list.add("王五-女-23");
        list.add("赵六-男-25");
        list.add("王五-女-26");
        List<String> li = list.stream().filter(s -> s.split("-")[1].equals("男")).collect(Collectors.toList());
        Set<String> set = list.stream().filter(s -> s.split("-")[1].equals("男")).collect(Collectors.toSet());
}

②map集合(把姓名作为键,年龄作为值,把性别为男的放入集合中)

public class Test7 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("张三-男-20");
        list.add("李四-男-30");
        list.add("王五-女-23");
        list.add("赵六-男-25");
        list.add("王五-女-26");
        Map<String, Integer> map = list.stream().filter(s -> s.split("-")[1].equals("男")).collect(Collectors.toMap(new Function<String, String>() {
            @Override(参数String s表示流水线上的每一个元素)
            public String apply(String s) {//第一个new Function<>的表示map中的键,第一个参数表示流水线上的数据类型,第二个参数表示键的数据类型
                return s.split("-")[0];
            }
        }, new Function<String, Integer>() {//第二个new Function<>的表示map中的值,第一个参数表示流水线上的数据类型,第二个参数表示值的数据类型
            @Override
            public Integer apply(String s) {
                return Integer.parseInt(s.split("-")[2]);
            }
        }));
    }
}

 用lambda可表示为

public class Test7 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("张三-男-20");
        list.add("李四-男-30");
        list.add("王五-女-23");
        list.add("赵六-男-25");
        list.add("王五-女-26");
        Map<String, Integer> map = list.stream().filter(s -> s.split("-")[1].equals("男")).collect(Collectors.toMap( s-> s.split("-")[0],  
                s-> Integer.parseInt(s.split("-")[2])));
    }
}

注意:如果键重复,代码会报错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值