流(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 = ate(() -> 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)-> Salary(),o2.getSalary())).forEach(System.out::println);//6. map 把流映射到另外一组数据, 提取集合中员工的名字list.stream().map(e-&Name()).forEach(System.out::println);//parallel (parallelStream) 获取一个并行流 采用多线程提高效率list.stream().parallel().map(e-&Name()).forEach(System.out::println);//练习: 找出所有姓张的,按照工资升序排序// list.stream().filter(e-&Name().startsWith("张")).sorted((o1,o2)->Salary(),o2.getSalary()))// .forEach(System.out::println);//练习2: 找出工资最低的张姓人员 // list.stream().filter(e-&Name().startsWith("张")) // .sorted((o1,o2)-> Salary(),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) -> Salary(), o2.getSalary()));System.out.());//找出工资最高的员工Optional<Employee> max = list.stream().max((o1, o2) -> Salary(), o2.getSalary()));System.out.());//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.());int[] array = {1,2,3,4,5,6,7,8,9,10};OptionalInt reduce1 = Arrays.stream(array).reduce((x, y) -> x + y);System.out.AsInt());String[] arr = {"a","b","c","d","e"};Optional<String> reduce2 = Arrays.stream(arr).reduce((x, y) -> x + y);System.out.());//collect 收集// 收集所有员工的姓名,收集到一个集合中List<String> collect = list.stream().map(e -> e.getName()).List());//collect.forEach(System.out::println);Iterator<String> iterator = collect.iterator();while(iterator.hasNext()){String str = ();System.out.println(str);}}
}
之前的API存在的问题: 线程安全问题,设计混乱
本地化的日期对象
LocalDate
LocalTime
LocalDateTime
Instant 时间戳
ZXoneld 时区
DateTimeFormatter 日期格式转换
public static void main(String[] args) throws ExecutionException, InterruptedException {SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");ExecutorService executorService = wFixedThreadPool(10);Callable<Date> callable = new Callable<Date>() {@Overridepublic 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.());}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 = wFixedThreadPool(10);Callable<LocalDate> callable = new Callable<LocalDate>() {@Overridepublic 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.());}pool.shutdown();}
}
本文发布于:2024-02-01 09:37:28,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170675145035709.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |