1.创建流(并行流和顺序流)
import java.util.ArrayList; import java.util.List; import java.util.stream.Stream; /** * Created by sk on 2021/1/13 */ public class Test { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("张三"); list.add("李四"); list.add("韩信"); //顺序流 Stream<String> stream = list.stream(); stream.forEach(System.out::println); System.out.println("-------------------------"); //并行流 Stream<String> stringStream = list.parallelStream(); stringStream.forEach(System.out::println); } }
2.输出
3.流基础操作
import java.util.stream.Stream; /** * Created by sk on 2021/1/13 */ public class Test1 { public static void main(String[] args) { Stream<Integer> stream = Stream.of(6, 4, 6, 7, 3, 9, 8, 10, 12, 14, 14); Stream<Integer> newStream = stream //过滤流中符合条件的某些元素 6 6 7 9 8 10 12 14 14 .filter(s -> s > 5) //去除重复元素 6 7 9 8 10 12 14 .distinct() //跳过n元素,配合limit(n)可实现分页 8 10 12 14 .skip(3) //获取n个元素 8 10 .limit(2); newStream.forEach(System.out::print); } }