1、排序
import lombok.Data; import java.util.*; import java.util.stream.Collectors; public class StreamStudy { public static void main(String[] args) { User u1 = new User("陈一",32); User u2 = new User("陈二",23); User u3 = new User("陈三",33); List<User> users = new ArrayList<>(); users.add(u1); users.add(u2); users.add(u3); List<User> collect1 = users.stream().sorted(Comparator.comparing(User::getAge)).collect(Collectors.toList()); System.out.println(collect1); List<User> collect2 = users.stream().sorted(Comparator.comparing(User::getAge).reversed()).collect(Collectors.toList()); System.out.println(collect2); } } @Data class User { public User() {} public User(String name, int age) { this.name = name; this.age = age; } String name; int age; }
2、集合操作
import lombok.Data; import java.util.*; import java.util.stream.Collectors; public class StreamStudy { public static void main(String[] args) { User user1 = new User(5, "aa"); User user2 = new User(2, "ba"); User user3 = new User(3, "ac"); User user4 = new User(1, "aad"); User user5 = new User(4, "asd"); List<User> userList = Arrays.asList(user1, user2, user3, user4, user5); //获取所有ID集合 List<Integer> idList = userList.stream().map(User::getId).collect(Collectors.toList()); System.out.println(idList); //根据ID排序 List<User> userList1 = userList.stream().sorted(Comparator.comparingInt(User::getId)).collect(Collectors.toList()); System.out.println(userList1); //分组 Map<String, Long> map = userList.stream().collect(Collectors.groupingBy(User::getName, Collectors.counting())); System.out.println(map); //获取id大于2的 List<User> userList2 = userList.stream().filter(user -> user.getId() > 2).collect(Collectors.toList()); System.out.println(userList2); //获取最大 Integer id = userList.stream().map(User::getId).max(Integer::compareTo).get(); System.out.println(id); //获取最小 Integer id1 = userList.stream().map(User::getId).min(Integer::compareTo).get(); System.out.println(id1); //获取数量 Long id3 = userList.stream().count(); System.out.println(id3); //求和 int sum = userList.stream().mapToInt(User::getId).sum(); System.out.println(sum); //获取平均值 double avg = userList.stream().mapToInt(User::getId).average().getAsDouble(); System.out.println(avg); //如果存在获取任意一个对象 User user = userList.stream().filter(us -> us.getId() > 2).findAny().get(); System.out.println(user); //获取第一个对象 User userFirst = userList.stream().findFirst().get(); System.out.println(userFirst); //将名字全转换为大写 List<String> upName = userList.stream().map(User::getName).map(String::toUpperCase).collect(Collectors.toList()); System.out.println(upName); //获取忽略第一个并取前几条数据 List<User> users = userList.stream().skip(1).limit(2).collect(Collectors.toList()); System.out.println(users); //去重 List<User> userDis = userList.stream().distinct().collect(Collectors.toList()); System.out.println(userDis); } } @Data class User { public User(){} public User(int id, String name){ this.id = id; this.name = name; } int id; String name; }
3、并行流
将List转为Map
Map<Long, DeviceOnlineDto> onlinemap = onlinelist.parallelStream().collect(Collectors.toMap(DeviceOnlineDto::getDeviceid, e -> e));