怎么制作购物网站,建设合同施工合同示范文本,重庆百度搜索优化,廊坊cms建站模板一. Stream 1. Stream也叫Stream流#xff0c;是jdk8开始新增的一套API(java.util.stream.*)#xff0c;可以用于操作集合或者数组的数据。 2. 优势#xff1a;Stream流大量的结合了lambda的语言风格来编程#xff0c;提供了一种更加强大#xff0c;更加简洁的方式操作集合…一. Stream 1. Stream也叫Stream流是jdk8开始新增的一套API(java.util.stream.*)可以用于操作集合或者数组的数据。 2. 优势Stream流大量的结合了lambda的语言风格来编程提供了一种更加强大更加简洁的方式操作集合或者数组中的数据。 3. 使用步骤 ① 获取Stream流 ② Stream流常用的中间方法(支持链式调用) ③ Stream 流常见的终结方法 public static void main(String[] args) {//StreamListString list new ArrayListString();Collections.addAll(list, 卡莎, 卡车, 泰坦, 璐璐, 卡拉, 卡卡卡, 伊泽);System.out.println(list);//[卡莎, 卡车, 泰坦, 璐璐, 卡拉, 卡卡卡, 伊泽]//list中方法 筛选数据ListString list1 new ArrayList();for(String str : list){if (str.contains(卡) str.length() 2){list1.add(str);}}System.out.println(list1);//[卡莎, 卡车, 卡拉]//使用stream流 筛选ListString list2 list.stream().filter(s - s.contains(卡)).filter(s - s.length() 2).collect(Collectors.toList());System.out.println(list2);//v}
二. Stream的常用方法 1. 获取Stream流
方法说明Collection提供的获取Stream流default StreamE stream()获取当前集合的Stream流Arrays类提供的获取Stream流public static T StreamT stream(T[] array)获取当前数组的Stream流Stream提供的获取Stream流public staticT StreamT of(T... values)获取当前接收数据的Stream流
public static void main(String[] args) {//1. 获取List集合的Stream流ListString list new ArrayListString();Collections.addAll(list, 卡莎, 卡车, 泰坦, 璐璐, 卡拉, 卡卡卡, 伊泽);//获取Stream流StreamString stream list.stream();//2. 获取Set集合的Stream流SetString set new HashSetString();Collections.addAll(set, 大宇, 朵朵, 欢欢, 麦琪);//获取Stream流StreamString stream1 set.stream();stream1.filter(s - s.contains(欢)).forEach(System.out::println);//3. 获取Map集合的Stream流MapString, String map new HashMap();map.put(杨过, 小龙女);map.put(张无忌, 赵敏);map.put(郭靖, 黄蓉);map.put(令狐冲, 东方不败);// 获取Stream流 分开处理Set set2 map.entrySet();StreamString keys set2.stream();CollectionString values map.values();StreamString vas values.stream();//统一处理SetMap.EntryString, String entry map.entrySet();StreamMap.EntryString, String kvs entry.stream();kvs.filter(k - k.getKey().contains(张)).forEach(System.out::println);//4. 获取数组的Stream流String[] str {路马, 天天, 莱德, 落落};//public static T StreamT stream(T[] array)StreamString stream2 Arrays.stream(str);//public staticT StreamT of(T... values)StreamString stream3 Stream.of(str);
} 2. Stream流常见的中间方法 (1) 中间方法是指调用完成后返回新的Stream流可以继续使用(支持链式编程)
常用方法说明StreamT filter(Predicate? super T predicate)用于对流中的数据进行过滤StreamT sorted()对元素进行升序排序StreamT sorted(Comparator? super T comparator)对元素按照指定规则排序StreamT limit(long maxSize)获取前几个元素StreamT skip(long n)跳过前几个元素StreamT distinct()去除流中重复的元素R StreamR map(Function? super T, ? extends R mapper)对元素进行加工并返回对应的新流static T StreamT concat(Stream a, Stream b)合并a和b两个流 public class Student{private String name;private int age;private double score;public Student() {}public Student(String name, int age, double score) {this.name name;this.age age;this.score score;}public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}public double getScore() {return score;}public void setScore(double score) {this.score score;}Overridepublic String toString() {return Student{ name name \ , age age , score score };}Overridepublic boolean equals(Object o) {if (o null || getClass() ! o.getClass()) return false;Student student (Student) o;return age student.age Double.compare(score, student.score) 0 Objects.equals(name, student.name);}Overridepublic int hashCode() {return Objects.hash(name, age, score);}
}public static void main(String[] args) {ListDouble scores new ArrayListDouble();Collections.addAll(scores, 99.0, 96.0, 94.0,59.0, 66.0, 74.0);//成绩大于等于60的并排序scores.stream().filter(s - s 60.0).sorted().forEach(System.out::println);//66.0 74.0 94.0 96.0 99.0System.out.println(--------------------------------------------);ListStudent students new ArrayList();Student s1 new Student(卡莎, 18, 99.0);Student s2 new Student(泰坦, 19, 93.0);Student s3 new Student(伊泽, 16, 98.0);Student s4 new Student(璐璐, 14, 96.0);Student s5 new Student(璐璐, 14, 96.0);Collections.addAll(students, s1, s2, s3, s4, s5);//找出年龄大于等于16 且小于等于20 按照年龄降序//filter() sorted()students.stream().filter(s - s.getAge() 16 s.getAge() 20).sorted(((o1, o2) - o2.getAge() - o1.getAge())).forEach(System.out::println);System.out.println(--------------------------------------------);//找出分数最高的前三名// sorted() limit()students.stream().sorted((o1, o2) - Double.compare(o2.getScore(), o1.getScore())).limit(3).forEach(System.out::println);System.out.println(--------------------------------------------);//找出分数最低的 倒数3名//sorted() skip()students.stream().sorted((o1, o2) - Double.compare(o2.getScore(), o1.getScore())).skip(students.size() - 3).forEach(System.out::println);System.out.println(--------------------------------------------);//成绩大于等于95的 去除重复的名字// distinct() 自定义类型 如果希望内容一样认为重复 需重写 equals()和 hashCode()//filter() map() distinct()students.stream().filter(s - s.getScore() 95).map(s - s.getName()).distinct().forEach(System.out::println);students.stream().filter(s - s.getScore() 95).distinct().forEach(System.out::println);//static T StreamT concat(Stream a, Stream b) 合并a和b两个流StreamString stream Stream.of(1, 2, 3);StreamString stream2 Stream.of(4, 5, 6, 7, 8, 9);StreamString stream3 Stream.concat(stream, stream2);stream3.forEach(System.out::println);} 3. Stream流常见的终结方法 (1) 终结方法指的是调用完成后不会再返回新的Stream流了不能再继续使用Stream流了。
方法名称说明void forEach(Consumer action)对此流运算后的元素进行遍历long count()统计此流运算后的元素个数OptionalT max(Comparator? super T copmarator)获取此流运算后的最大值元素OptionalT min(Comparator? super T copmarator)获取此流运算后的最小值元素
方法名称说明R collect(Collector collector)把流处理后的结果放到一个指定的集合中Object[] toArray()把流处理后的结果放到一个指定的数组中
public static void main(String[] args) {ListStudent students new ArrayList();Student s1 new Student(卡莎, 18, 99.0);Student s2 new Student(泰坦, 19, 93.0);Student s3 new Student(伊泽, 16, 98.0);Student s4 new Student(璐璐, 14, 96.0);Student s5 new Student(璐璐, 14, 96.0);Collections.addAll(students, s1, s2, s3, s4, s5);// 计算分数超过95的有几个 .count()long l students.stream().filter(s - s.getScore() 95).count();System.out.println(l);//4//找出分数最高的 并输出 .max()Student smax students.stream().max((o1, o2) - Double.compare(o1.getScore(), o2.getScore())).get();System.out.println(smax);//找出分数最低的 并输出 .min()Student smin students.stream().min((o1, o2) - Double.compare(o1.getScore(), o2.getScore())).get();System.out.println(smin);//计算分数超过95的 并放到一个新集合中//流只能收集一次ListStudent list1 students.stream().filter(s - s.getScore() 95).collect(Collectors.toList());System.out.println(list1);SetStudent list2 students.stream().filter(s - s.getScore() 95).collect(Collectors.toSet());System.out.println(list2);//找出分数超过95的 并把名字和分数放到一个map集合中//不会自动去重 需调用distinct()去重MapString, Double map students.stream().filter(s - s.getScore() 95).distinct().collect(Collectors.toMap(m - m.getName(), m - m.getScore()));System.out.println(map);//找出分数超过95的 并把名字和分数放到一个数组Object[] arr students.stream().filter(s - s.getScore() 95).toArray();Student[] arr1 students.stream().filter(s - s.getScore() 95).toArray(len - new Student[len]);System.out.println(Arrays.toString(arr));System.out.println(Arrays.toString(arr1));
}