在阅读本文章之前请了解什么叫 Lambda表达式 以及 如何使用
Stream流的使用步骤:
- // 1. 单列集合获取Stream
- ArrayList
list = new ArrayList(); - list.add("a");
- list.add("b");
- // 2. 获取Stream流
- list.stream().forEach(e-> System.out.println(e));
- // 1. 双列集合获取Stream
- HashMap
hm = new HashMap(); - hm.put("a",1);
- hm.put("b",2);
- // 2. 双列集合获取Stream需要转化为单列集合
- // 使用keyset/entryset
- hm.keySet().stream().forEach(key -> System.out.println(key));
- // 1. 数组获取Stream流
- int[] arr = {1,2,3,4,5,6,7,8};
- // 2. 数组通过Arrays获取Stream流
- Arrays.stream(arr).forEach(e-> System.out.println(e));
- // 1. 零散数据获取Stream流
- Stream.of(1234).forEach(e-> System.out.println(e));
- // 2. 零散数据获取stream通过of
- // 但是切记零散数据需要这些数据为同种数据类型
- Stream.of("A","B").forEach(e-> System.out.println(e));
2.1 过滤filter- ArrayList
arr = new ArrayList(); - Collections.addAll(arr,1,10,16,27,29,29,30,40,50);
- // 1. 获取stream流,使用filter过滤大于等于30的数字
- arr.stream().filter(e->e<30).forEach(e-> System.out.println(e));

- ArrayList
arr = new ArrayList(); - Collections.addAll(arr,1,10,16,27,29,29,30,40,50);
- // 1. 获取stream流,获取前5个元素
- arr.stream().limit(5).forEach(e-> System.out.println(e));
- ArrayList
arr = new ArrayList(); - Collections.addAll(arr,1,10,16,27,29,29,30,40,50);
- // 1. 获取stream流,跳过5个元素
- arr.stream().skip(5).forEach(e-> System.out.println(e));

- ArrayList
arr = new ArrayList(); - Collections.addAll(arr,1,10,16,27,29,29,30,40,50);
- // 1. 获取stream流,去重
- arr.stream().distinct().forEach(e-> System.out.println(e));

- ArrayList
arr1 = new ArrayList(); - ArrayList
arr2 = new ArrayList(); - Collections.addAll(arr1,1,10,16);
- Collections.addAll(arr2,2,10,26);
- // 1. 获取stream流,合并去重
- Stream.concat(arr1.stream(),arr2.stream())
- .distinct()
- .forEach(e-> System.out.println(e));

- ArrayList
arr1 = new ArrayList(); - Collections.addAll(arr1,"a-1","b-2","c-3","d-4");
- // 1. 获取stream流,并且只打印数字 使用map进行类型转化
- arr1.stream()
- .map(e->Integer.parseInt(e.split("-")[1]))
- .forEach(e-> System.out.println(e));


这个没啥好说的,上面中间方法的所有例子的终结方法都是foreach
- ArrayList
arr1 = new ArrayList(); - Collections.addAll(arr1, 1,2,3,4,5,6,7,8,9,10,11);
- long count = arr1.stream().count();
- System.out.println(count);

- ArrayList
arr1 = new ArrayList(); - Collections.addAll(arr1,"a-1","b-2","c-3","d-4");
- // 1. 获取stream流,并且存储到数组中 toArray
- // 2. 如果toArry为空参则对象为Object
- Object[] objects = arr1.stream().toArray();
- System.out.println(Arrays.toString(objects));
- // 3. 如果需要转为指定类型的数组则需要传参 参数传递为 个数->需要数组类型【个数】
- String[] strings = arr1.stream().toArray(e -> new String[e]);
- System.out.println(Arrays.toString(strings));

- ArrayList
arr1 = new ArrayList(); - Collections.addAll(arr1,"a-1","b-2","c-3","d-4","d-4");
- // 1. 获取stream流,并且存储到List集合当中
- // .collect(Collectors.toList()) 底层会自动为我们创建list集合
- List
collect = arr1.stream().collect(Collectors.toList()); - // 2. 获取stream流,并且存储到Set集合当中
- Set
collect1 = arr1.stream().collect(Collectors.toSet()); - System.out.println("list:"+collect);
- System.out.println("set:"+collect1);
通过输出我们可以看到,存储在list与set集合中的区别,set会去重

注意转化list到map的时候 不能有相同的key出现哦!!!!
- ArrayList
arr1 = new ArrayList(); - Collections.addAll(arr1,"a-1","b-2","c-3","d-4");
- // 3. 获取stream流,并且存储到map集合当中 a为键 1为值
- /*
- tomap:参数1:键的生成规则
- 参数2:值的生成规则
- */
- Map
collect2 = arr1.stream() - .collect(Collectors.toMap(
- e -> e.split("-")[0], //参数1
- e -> e.split("-")[1] //参数2
- ));
- System.out.println("map:"+collect2);