数据准备:
List<String> list = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API");
public class User {
//姓名
private String name;
//年龄
private int age;
//学号
private String number;
public User() {
}
public User(String name, int age, String number) {
this.name = name;
this.age = age;
this.number = number;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", number='" + number + '\'' +
'}'+'\n';
}
User user1 = new User("Tom",15,"96968686");
User user2 = new User("Cat",24,"10086000");
User user3 = new User("Storm",25,"89898989");
User user4 = new User("HBase",26,"85858585");
User user5 = new User("Hadoop",26,"78787878");
List<User> userList = new ArrayList<>();
userList.add(user1);
userList.add(user2);
userList.add(user3);
userList.add(user4);
userList.add(user5);
- 遍历集合
//java8 之前 for (String str:list) { System.out.println(str); } System.out.println("===========邪恶的分隔符=============="); //在java8中 list.forEach(n -> System.out.println(n)); list.forEach(System.out::println);
- 集合的过滤与count
long count = list.stream() .filter(array -> array.equals("Stream API")) .count(); System.out.println(count); System.out.println("===========邪恶的分隔符=============="); List<String> collect = list.stream() .filter(array -> !array.equals("Stream API")) .collect(Collectors.toList()); collect.forEach(System.out::println);
- 分组
//List里面的对象元素,以某个属性来分组,例如,以name分组,将name相同的放在一起: Map<String, List<User>> listMap = userList.stream() .collect(Collectors .groupingBy(User::getName)); System.out.println(listMap.toString());
-
指定key-value,value是对象中的某个属性值
Map<String, String> map = userList.stream() .collect(Collectors .toMap(User::getName, User::getNumber)); map.keySet().forEach(key-> System.out.println(key+" : "+map.get(key)));
-
指定key-value,value是对象本身,User->User 是一个返回本身的lambda表达式
Map<String, User> map = userList.stream() .collect(Collectors .toMap(User::getName, User -> User)); map.keySet().forEach(key-> System.out.println(key+" : "+map.get(key)));
-
指定key-value,value是对象本身,Function.identity()是简洁写法,也是返回对象本身
Map<String, User> map = userList.stream() .collect(Collectors .toMap(User::getName, Function.identity())); map.keySet().forEach(key-> System.out.println(key+" : "+map.get(key)));
-
key 冲突的解决办法,这里选择第二个key覆盖第一个key
Map<String, User> map = userList.stream() .collect(Collectors .toMap(User::getName ,Function.identity() ,(key1, key2) -> key2)); map.keySet().forEach(key-> System.out.println(key+" : "+map.get(key)));
-
过滤Filter 从集合中过滤出来符合条件的元素:(eg:age 大于 20)
List<User> userList1 = userList.stream() .filter(user -> user.getAge() > 20) .collect(Collectors.toList()); userList1.forEach(System.out::println);
-
对集合中某个属性值求和
Integer reduce = userList.stream() .map(User::getAge) .reduce(0,Integer::sum); System.out.println(reduce);
-
对集合中某个属性值取Max or Min
Optional<User> optionalMax = userList.stream() .max(Comparator.comparing(User::getAge)); optionalMax.ifPresent(System.out::println); Optional<User> optionalMin = userList.stream() .max(Comparator.comparing(User::getAge)); optionalMin.ifPresent(System.out::println);
来源:CSDN
作者:从北码到南
链接:https://blog.csdn.net/weixin_36079865/article/details/104406159