1.遍历(forEach)
public static void main(String[] args) {
List<Person> userList = new ArrayList<>();
userList.add(new Person("段誉",25));
userList.add(new Person("萧峰",40));
userList.add(new Person("虚竹",30));
userList.add(new Person("无涯子",100));
userList.add(new Person("慕容复",35));
userList.add(new Person("云中鹤",45));
System.out.println(userList);
System.out.println("---------------");
userList.stream().forEach(u -> {
u.setName("天龙八部-" + u.getName());
});
System.out.println(userList);
}
控制台输出:
[Person(name=段誉, age=25), Person(name=萧峰, age=40), Person(name=虚竹, age=30), Person(name=无涯子, age=100), Person(name=慕容复, age=35), Person(name=云中鹤, age=45)]
---------------
[Person(name=天龙八部-段誉, age=25), Person(name=天龙八部-萧峰, age=40), Person(name=天龙八部-虚竹, age=30), Person(name=天龙八部-无涯子, age=100), Person(name=天龙八部-慕容复, age=35), Person(name=天龙八部-云中鹤, age=45)]
2.筛选(filte)
List<Person> userList = new ArrayList<>();
userList.add(new Person("段誉",25));
userList.add(new Person("萧峰",40));
userList.add(new Person("虚竹",30));
userList.add(new Person("无涯子",100));
userList.add(new Person("慕容复",35));
userList.add(new Person("云中鹤",45));
List<Person> collect =
userList.stream().filter(user -> (user.getAge() > 30 && user.getName().length() >2)).collect(Collectors.toList());
System.out.println(collect);
控制台输出:
[Person(name=无涯子, age=100), Person(name=慕容复, age=35), Person(name=云中鹤, age=45)]
3.查找(findAny/findFirst)
public static void main(String[] args) {
List<Person> userList = new ArrayList<>();
userList.add(new Person("段誉",25));
userList.add(new Person("萧峰",41));
userList.add(new Person("萧峰",42));
userList.add(new Person("萧峰",43));
userList.add(new Person("虚竹",30));
userList.add(new Person("无涯子",100));
userList.add(new Person("慕容复",35));
userList.add(new Person("云中鹤",45));
Person user = userList.stream().filter(u -> u.getName().equals("阿紫")).findAny().orElse(null);
System.out.println(user);
Person user2 = userList.stream().filter(u -> u.getName().equals("萧峰")).findAny().orElse(null);
System.out.println(user2);
Person user3 = userList.stream().filter(u -> u.getName().equals("萧峰")).findFirst().orElse(null);
System.out.println(user3);
}
控制台输出:
null
Person(name=萧峰, age=41)
Person(name=萧峰, age=41)
1 .findFirst() 方法根据命名可以大致知道是获取Optional流中的第一个元素。
2 .findAny() 方法是获取Optional 流中任意一个,存在随机性,其实里面也是获取元素中的第一个。
使用findAny()是为了更高效的性能。如果是数据较少,串行地情况下,一般会返回第一个结果,如果是并行的情况,那就不能确保是第一个。
4.转换/去重(map/flatMap/distinct)
List<Person> userList = new ArrayList<>();
userList.add(new Person("段誉",25));
userList.add(new Person("萧峰",41));
userList.add(new Person("萧峰",42));
userList.add(new Person("虚竹",30));
userList.add(new Person("无涯子",100));
userList.add(new Person("慕容复",35));
userList.add(new Person("云中鹤",45));
List<String> nameList = userList.stream().map(Person::getName).collect(Collectors.toList());
System.out.println(nameList);
List<String> nameList2 = userList.stream().map(Person::getName).distinct().collect(Collectors.toList());
System.out.println(nameList2);
控制台输出:
[段誉, 萧峰, 萧峰, 虚竹, 无涯子, 慕容复, 云中鹤]
[段誉, 萧峰, 虚竹, 无涯子, 慕容复, 云中鹤]
map的作用:是将输入一种类型,转化为另一种类型,通俗来说:就是将输入类型变成另一个类型。
flatMap的作用:是扁平化的,比如有对象List<List<Student>> userLists, 这属于套娃现象,而最终只想得到List<Student> userList对象,即将多维集合,变成一个集合,相当于压缩,俗称扁平化。
对于处理嵌套list,flatMap更合适文章来源:https://uudwc.com/A/oyA0
public static void main(String[] args) {
List<Student> studentList1 = new ArrayList<>();
studentList1.add(new Student("枯荣大师",22));
studentList1.add(new Student("游坦之",44));
List<Student> studentList2 = new ArrayList<>();
studentList2.add(new Student("天山童姥",66));
studentList2.add(new Student("苏星河",88));
List<Person> userList = new ArrayList<>();
userList.add(new Person(studentList1));
userList.add(new Person(studentList2));
List<String> collect = userList.stream()
.map(Person::getStudentList)
.flatMap(Collection::stream)
.map(Student::getName)
.collect(Collectors.toList());
System.out.println(collect);
List<String> studentList = new ArrayList<>();
userList.stream().forEach(person -> {
person.getStudentList().forEach(people -> {
studentList.add(people.getName());
});
});
System.out.println(studentList);
}
控制台输出:
[枯荣大师, 游坦之, 天山童姥, 苏星河]
[枯荣大师, 游坦之, 天山童姥, 苏星河]
5.跳过(limit/skip)
public static void main(String[] args) {
List<Person> userList = new ArrayList<>();
userList.add(new Person("段誉",25));
userList.add(new Person("萧峰",41));
userList.add(new Person("虚竹",30));
userList.add(new Person("无涯子",100));
userList.add(new Person("慕容复",35));
List<Person> collect = userList.stream().skip(1).collect(Collectors.toList());
System.out.println(collect);
List<Person> collect2 = userList.stream().limit(2).collect(Collectors.toList());
System.out.println(collect2);
List<Person> collect3 = userList.stream().skip(4).limit(3).collect(Collectors.toList());
System.out.println(collect3);
}
控制台输出:
[Person(name=萧峰, age=41), Person(name=虚竹, age=30), Person(name=无涯子, age=100), Person(name=慕容复, age=35)]
[Person(name=段誉, age=25), Person(name=萧峰, age=41)]
[Person(name=慕容复, age=35)]
6.最大值/最小值/平均值(reduce)
public static void main(String[] args) {
List<Person> userList = new ArrayList<>();
userList.add(new Person("段誉",25));
userList.add(new Person("萧峰",41));
userList.add(new Person("虚竹",30));
userList.add(new Person("无涯子",100));
userList.add(new Person("慕容复",35));
System.out.println("求和");
Integer sum1 = userList.stream().map(Person::getAge).reduce(0, Integer::sum);
System.out.println(sum1);
int sum2 = userList.stream().mapToInt(Person::getAge).sum();
System.out.println(sum2);
Integer sum3 = userList.stream().collect(Collectors.summingInt(Person::getAge));
System.out.println(sum3);
System.out.println("最大值");
Optional<Integer> max1 = userList.stream().map(Person::getAge).collect(Collectors.toList())
.stream().reduce((v1, v2) -> v1 > v2 ? v1 : v2);
System.out.println(max1.get());
Optional<Integer> max2 = userList.stream().map(Person::getAge).collect(Collectors.toList())
.stream().reduce(Integer::max);
System.out.println(max2.get());
int max3 = userList.stream().mapToInt(Person::getAge).max().getAsInt();
System.out.println(max3);
System.out.println("最小值");
Optional<Integer> min = userList.stream().map(Person::getAge).collect(Collectors.toList())
.stream().reduce(Integer::min);
System.out.println(min.get());
System.out.println("平均值");
Double averaging1 = userList.stream().collect(Collectors.averagingDouble(Person::getAge));
System.out.println(averaging1);
double averaging2 = userList.stream().mapToInt(Person::getAge).average().getAsDouble();
System.out.println(averaging2);
}
控制台输出:
求和
231
231
231
最大值
100
100
100
最小值
25
平均值
46.2
46.2
7.统计(count/counting)
public static void main(String[] args) {
List<Person> userList = new ArrayList<>();
userList.add(new Person("段誉",25));
userList.add(new Person("萧峰",41));
userList.add(new Person("虚竹",30));
userList.add(new Person("无涯子",100));
userList.add(new Person("虚竹",30));
userList.add(new Person("慕容复",35));
Long collect = userList.stream().filter(p -> p.getName() == "虚竹").collect(Collectors.counting());
System.out.println(collect);
long count = userList.stream().filter(p -> p.getAge() >= 50).count();
System.out.println(count);
}
控制台输出:
2
1
8.排序(sorted)
public static void main(String[] args) {
List<Person> userList = new ArrayList<>();
userList.add(new Person("1",25));
userList.add(new Person("2",41));
userList.add(new Person("33",30));
userList.add(new Person("11",100));
userList.add(new Person("55",30));
userList.add(new Person("22",35));
List<Person> collect =
userList.stream().sorted(Comparator.comparing(Person::getAge).reversed()).collect(Collectors.toList());
System.out.println(collect);
List<Person> collect2 =
userList.stream().sorted(Comparator.comparing(Person::getAge)).collect(Collectors.toList());
System.out.println(collect2);
List<Person> collect3 =
userList.stream().sorted(Comparator.comparing(Person::getName)).collect(Collectors.toList());
System.out.println(collect3);
List<Person> collect4 = userList.stream().sorted((e1, e2) -> {
return Integer.compare(Integer.parseInt(e1.getName()), Integer.parseInt(e2.getName()));
}).collect(Collectors.toList());
System.out.println(collect4);
}
控制台输出:
[Person(name=11, age=100), Person(name=2, age=41), Person(name=22, age=35), Person(name=33, age=30), Person(name=55, age=30), Person(name=1, age=25)]
[Person(name=1, age=25), Person(name=33, age=30), Person(name=55, age=30), Person(name=22, age=35), Person(name=2, age=41), Person(name=11, age=100)]
[Person(name=1, age=25), Person(name=11, age=100), Person(name=2, age=41), Person(name=22, age=35), Person(name=33, age=30), Person(name=55, age=30)]
[Person(name=1, age=25), Person(name=2, age=41), Person(name=11, age=100), Person(name=22, age=35), Person(name=33, age=30), Person(name=55, age=30)]
字符串排序可以自定义排序规则文章来源地址https://uudwc.com/A/oyA0