流(Stream)中保存了对集合或者数组数据的操作,和集合类似,但是集合中保存的是数据。Stream不能保存数据
一、创建流
-
通过Collection对象的stream()或者parallelStream()
-
通过Arrays类的stream(Array[]<T>)方法
-
通过Stream接口of() iterate() generate()方法
-
通过 IntStream, LongStream, DoubleStream接口中的of(), range(), rangeClosed()方法
//1. 通过Collection对象的stream()或者parallelStream()
List<String> list = new ArrayList<>();Stream<String> stringStream = list.parallelStream();
//2.通过Arrays类的stream()方法
String[] names = {"乔峰", "虚竹", "段誉", "阿朱", "段正淳", "萧远山"};
Stream<String> stream = Arrays.stream(names);
//3.通过Stream接口of() iterate() generate()方法
// System.out.println("-----------通过Stream接口的of()--------");
Stream<Integer> integerStream = Stream.of(10, 20, 30, 40, 50, 60);Stream<Integer> iterate = Stream.iterate(0, x -> x + 2);
Stream<Integer> generate = Stream.generate(() -> new Random().nextInt(100));
//4.1. 通过 IntStream, LongStream, DoubleStream接口中的of(), range(), rangeClosed()方法
IntStream intStream = IntStream.of(10, 20, 30, 40, 50, 60, 70, 80);
二、流的操作
一个stream调用方法变成一个新的stream
-
filter(): 对元素数据进行过滤
-
limit(): 限制数据
-
skip(): 跳过几个元素
-
distinct(): 去重
-
sorted() 排序
-
map() 把流映射到另外一组数据
-
parallel() (parallelStream) 获取一个并行流
-
public class StreamDemo2 { public static void main(String[] args) { List<Employee> list = new ArrayList<>(); list.add(new Employee("张无忌", 12000.0)); list.add(new Employee("小昭", 18000.0)); list.add(new Employee("张三丰", 32000.0)); list.add(new Employee("金花婆婆", 15000.0)); list.add(new Employee("宋青书", 8000.0)); list.add(new Employee("灭绝师太", 13000.0)); list.add(new Employee("谢逊", 27000.0)); list.add(new Employee("成昆", 12800.0)); list.add(new Employee("殷天正", 32000.0)); list.add(new Employee("殷素素", 14500.0)); list.add(new Employee("张翠山", 16900.0)); list.add(new Employee("张松溪", 14200.0)); list.add(new Employee("灭绝师太", 13000.0)); list.add(new Employee("灭绝师太", 13000.0)); list.add(new Employee("灭绝师太", 13000.0)); list.add(new Employee("灭绝师太", 13000.0)); list.add(new Employee("灭绝师太", 13000.0)); // 1. filter 过滤 过滤出工资大于15000的员工 Stream<Employee> employeeStream = list.stream().filter(e -> e.getSalary() > 15000); // employeeStream.forEach(System.out::println); //2. limit 限制数据 取集合中的前两条数据 Stream<Employee> limit = list.stream().limit(2); // limit.forEach(System.out::println); //3. skip 跳过几条数据 Stream<Employee> skip = list.stream().skip(3); // skip.forEach(System.out::println); //4. distinct 去重 , 集合中的元素需要重写equals方法 //list.stream().distinct().forEach(System.out::println); //5.sorted 排序 //list.stream().distinct().sorted((o1,o2)-> Double.compare(o1.getSalary(),o2.getSalary())).forEach(System.out::println); //6. map 把流映射到另外一组数据, 提取集合中员工的名字 list.stream().map(e->e.getName()).forEach(System.out::println); //parallel (parallelStream) 获取一个并行流 采用多线程提高效率 list.stream().parallel().map(e->e.getName()).forEach(System.out::println); //练习: 找出所有姓张的,按照工资升序排序 // list.stream().filter(e->e.getName().startsWith("张")).sorted((o1,o2)->Double.compare(o1.getSalary(),o2.getSalary())) // .forEach(System.out::println); //练习2: 找出工资最低的张姓人员 // list.stream().filter(e->e.getName().startsWith("张")) // .sorted((o1,o2)-> Double.compare(o1.getSalary(),o2.getSalary())) // .limit(1).forEach(System.out::println); // } }
三、终止流
-
forEach() 遍历
-
min() 最小值
-
max() 最大值
-
count() 总数
-
reduce() 规约
-
collect() 收集
-
public class StreamDemo4 { public static void main(String[] args) { List<Employee> list = new ArrayList<>(); list.add(new Employee("张无忌", 12000.0)); list.add(new Employee("小昭", 18000.0)); list.add(new Employee("张三丰", 32000.0)); list.add(new Employee("金花婆婆", 15000.0)); list.add(new Employee("宋青书", 8000.0)); list.add(new Employee("灭绝师太", 13000.0)); list.add(new Employee("谢逊", 27000.0)); list.add(new Employee("成昆", 12800.0)); list.add(new Employee("殷天正", 32000.0)); list.add(new Employee("殷素素", 14500.0)); list.add(new Employee("张翠山", 16900.0)); list.add(new Employee("张松溪", 14200.0)); //forEach() // list.stream().filter(e-> e.getName().startsWith("张")).forEach(System.out::println); // min() max() //找出工资最低的张姓人员 Optional<Employee> min = list.stream().filter(e -> e.getName().startsWith("张")) .min((o1, o2) -> Double.compare(o1.getSalary(), o2.getSalary())); System.out.println(min.get()); //找出工资最高的员工 Optional<Employee> max = list.stream().max((o1, o2) -> Double.compare(o1.getSalary(), o2.getSalary())); System.out.println(max.get()); //count() 求总数 输出员工总数 long count = list.stream().count(); System.out.println("员工总数:" + count); System.out.println(list.size()); //reduce() 规约 输入数据后对数据产生某些影响 对数据进行深加工,在Stream中此方法主要用于数据的叠加 //求公司员工工资总和 Optional<Double> reduce = list.stream().map(e -> e.getSalary()).reduce((x, y) -> x + y); System.out.println(reduce.get()); int[] array = {1,2,3,4,5,6,7,8,9,10}; OptionalInt reduce1 = Arrays.stream(array).reduce((x, y) -> x + y); System.out.println(reduce1.getAsInt()); String[] arr = {"a","b","c","d","e"}; Optional<String> reduce2 = Arrays.stream(arr).reduce((x, y) -> x + y); System.out.println(reduce2.get()); //collect 收集 // 收集所有员工的姓名,收集到一个集合中 List<String> collect = list.stream().map(e -> e.getName()).collect(Collectors.toList()); //collect.forEach(System.out::println); Iterator<String> iterator = collect.iterator(); while(iterator.hasNext()){ String str = iterator.next(); System.out.println(str); } } }四、新时间API
-
之前的API存在的问题: 线程安全问题,设计混乱
-
本地化的日期对象
-
LocalDate
-
LocalTime
-
LocalDateTime
-
Instant 时间戳
-
ZXoneld 时区
-
DateTimeFormatter 日期格式转换
-
-
SimpleDateFormat的线程安全问题
-
public static void main(String[] args) throws ExecutionException, InterruptedException { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ExecutorService executorService = Executors.newFixedThreadPool(10); Callable<Date> callable = new Callable<Date>() { @Override public Date call() throws Exception { return simpleDateFormat.parse("2023-09-16 12:11:11"); } }; List<Future<Date>> list = new ArrayList<>(); for (int i = 0; i < 10; i++) { Future<Date> submit = executorService.submit(callable); list.add(submit); } for (Future<Date> dateFuture : list) { System.out.println(dateFuture.get()); } executorService.shutdown(); }/** * SimpleDateFormat的线程安全问题 */ public class DateTimeFormatterDemo { public static void main(String[] args) throws ExecutionException, InterruptedException { //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd"); ExecutorService pool = Executors.newFixedThreadPool(10); Callable<LocalDate> callable = new Callable<LocalDate>() { @Override public LocalDate call() throws Exception { return LocalDate.parse("2023-09-15"); } }; List<Future<LocalDate>> list = new ArrayList<>(); for (int i = 0; i < 10; i++) { Future<LocalDate> submit = pool.submit(callable); list.add(submit); } for (Future<LocalDate> dateFuture : list) { System.out.println(dateFuture.get()); } pool.shutdown(); } }