流式处理

  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
package test; import java.util.ArrayList; import java.util.IntSummaryStatistics; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Test011 { public static void main(String[] args) { Student s1 = new Student(1, 11, "111"); Student s2 = new Student(2, 22, "222"); Student s3 = new Student(3, 33, "333"); Student s5 = new Student(5, 55, "555"); Student s6 = new Student(6, 66, "666"); Student s7 = new Student(6, 66, "666"); List<Student> list = new ArrayList<Student>(); list.add(s1); list.add(s2); list.add(s3); list.add(s5); list.add(s6); list.add(s7); // filter List<Student> l1 = list.stream().filter(student -> student.mark > 50).collect(Collectors.toList()); System.out.println(l1); // filter List<Student> l3 = list.stream().filter(student -> student.mark > 50).limit(1).collect(Collectors.toList()); System.out.println(l3); // distinct List<Student> l2 = list.stream().filter(student -> student.mark > 50).distinct().collect(Collectors.toList()); System.out.println(l2); // sort List<Student> l4 = list.stream().sorted((t1, t2) -> t2.age - t1.age).distinct().collect(Collectors.toList()); System.out.println(l4); // skip List<Student> l5 = list.stream().sorted((t1, t2) -> t2.age - t1.age).distinct().skip(1).collect(Collectors.toList()); System.out.println(l5); // map List<String> l6 = list.stream().map(Student::getName).collect(Collectors.toList()); System.out.println(l6); // mapToInt int i1 = list.stream().mapToInt(Student::getAge).sum(); System.out.println(i1); // allMatch boolean b1 = list.stream().allMatch(student -> student.getAge() > 1); System.out.println(b1); // IntSummaryStatistics IntSummaryStatistics statistics = list.stream().collect(Collectors.summarizingInt(Student::getAge)); System.out.println(statistics.getMax()); // groupingBy Map<Integer, List<Student>> map = list.stream().collect(Collectors.groupingBy(Student::getMark)); System.out.println(map); } } class Student { int age; int mark; String name; public Student(int age, int mark, String name) { this.age = age; this.mark = mark; this.name = name; } 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 int getMark() { return mark; } public void setMark(int mark) { this.mark = mark; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + mark; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (age != other.age) return false; if (mark != other.mark) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } @Override public String toString() { return this.name + " " + this.age + " " + this.mark; } }

浙ICP备11005866号-12