Stream流
关注做什么,而不是怎么做。
例1:对集合中的数据进行筛选:
1. Stream流中的filter方法:
(1)传统方式:
1 package cn.itcast.day_15; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 public class DemoList { 7 public static void main(String[] args) { 8 9 List<String> list = new ArrayList<>(); 10 list.add("aa"); 11 list.add("bb"); 12 list.add("abb"); 13 list.add("ccc"); 14 list.add("add"); 15 for (String s : list){ 16 System.out.println(s); 17 } 18 19 System.out.println("以a开头的字符串:"); 20 ArrayList<String> listA = new ArrayList<>(); 21 for (String s : list){ 22 if (s.startsWith("a")){ 23 listA.add(s); 24 } 25 } 26 for (String s : listA){ 27 System.out.println(s); 28 } 29 30 ArrayList<String> listB = new ArrayList<>(); 31 for (String s : listA){ 32 if (s.length() == 3){ 33 listB.add(s); 34 } 35 } 36 System.out.println("以a开头且长度为3的字符串"); 37 for (String s : listB){ 38 System.out.println(s); 39 } 40 } 41 }
(2)使用Stream流
1 package cn.itcast.day_15; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 public class Streamtest { 7 public static void main(String[] args) { 8 List<String> list = new ArrayList<>(); 9 list.add("aa"); 10 list.add("bb"); 11 list.add("abb"); 12 list.add("ccc"); 13 list.add("add"); 14 15 list.stream() 16 .filter((String name)->{ 17 return name.startsWith("a"); 18 }).filter((String name)->{ 19 return name.length() == 3; 20 }).forEach((String name)->{ 21 System.out.println(name); 22 }); 23 } 24 }
2. Stream流中的foreach方法:
例2:打印数组中的字符串
1 package cn.itcast.day_15; 2 3 import java.util.stream.Stream; 4 5 public class StreamTest01 { 6 public static void main(String[] args) { 7 Stream<String> stream = Stream.of("张三", "李四", "王五"); 8 stream.forEach((String name)->{ 9 System.out.println(name); 10 }); 11 } 12 }
例3:filter和foreach方法结合
1 package cn.itcast.day_15; 2 3 import java.util.stream.Stream; 4 5 public class StreamTest02 { 6 public static void main(String[] args) { 7 8 Stream<String> stream = Stream.of("aa", "bbb", "cccc", "afdd"); 9 stream.filter((String name)->{ 10 return name.startsWith("a"); 11 }).forEach((String name)->{ 12 System.out.println(name); 13 }); 14 } 15 }
Stream流属于管道流,只能被消费一次。
当第一个stream流消费完,数据就会转到下一个stream流。
3. 如果要将流中的元素映射到另一个流中,可以使用map方法。
例3:将String类型的字符串映射为Integer类型
1 package cn.itcast.day_15; 2 3 import java.util.stream.Stream; 4 5 public class StreamTest03 { 6 public static void main(String[] args) { 7 //获取一个stream类型的stream流 8 Stream<String> s = Stream.of("1","2","3","4"); 9 //将String类型的整数映射为Integer类型的整数 10 Stream<Integer> is = s.map((String s1) -> { 11 return Integer.parseInt(s1); 12 }); 13 14 //遍历Stream流中的数据 15 is.forEach((Integer integer)->{ 16 System.out.println(integer); 17 }); 18 } 19 }
4. Stream流中的count方法
例4:统计字符串中数字个数
1 package cn.itcast.day_15; 2 3 import java.util.ArrayList; 4 import java.util.stream.Stream; 5 6 public class StreamTest04 { 7 public static void main(String[] args) { 8 ArrayList<Integer> list = new ArrayList<>(); 9 list.add(1); 10 list.add(2); 11 list.add(3); 12 list.add(4); 13 list.add(5); 14 list.add(6); 15 Stream<Integer> stream = list.stream(); 16 long count = stream.count(); 17 System.out.println(count); 18 } 19 }
5. Stream流中的limit方法
例5:截取字符串中前三个元素
1 package cn.itcast.day_15; 2 3 import java.util.stream.Stream; 4 5 public class Streamtest05 { 6 public static void main(String[] args) { 7 String[] arr = {"aaa","bbb","cc","dfg","ad"}; 8 Stream<String> stream = Stream.of(arr); 9 Stream<String> limit = stream.limit(3); 10 limit.forEach((String name)->{ 11 System.out.println(name); 12 }); 13 } 14 }
6. Stream流中的skip方法
例6:跳过前3个字符
1 package cn.itcast.day_15; 2 3 import java.util.stream.Stream; 4 5 public class DemoTest06 { 6 public static void main(String[] args) { 7 String[] arr = {"aaa","bbb","cc","dfg","ad"}; 8 Stream<String> stream = Stream.of(arr); 9 Stream<String> stream1 = stream.skip(3); 10 stream1.forEach((String name)->{ 11 System.out.println(name); 12 }); 13 } 14 }
7. Stream流中的concat方法
例7:将两个流对象合成一个流
1 package cn.itcast.day_15; 2 3 import java.util.stream.Stream; 4 5 public class StreamTest06 { 6 public static void main(String[] args) { 7 String[] arr1 = {"aaa","bbb","cc","dfg","ad"}; 8 String[] arr2 = {"bbb","ccc","ddd","asdf"}; 9 Stream<String> stream1 = Stream.of(arr1); 10 Stream<String> stream2 = Stream.of(arr2); 11 Stream<String> concat = Stream.concat(stream1, stream2); 12 concat.forEach((String name)->{ 13 System.out.println(name); 14 }); 15 } 16 }