Java8 常用分组、去重、排序、List转Map



// 取员工姓名并去重
List<String> userNames = userList.stream().map(User::getName).distinct().collect(Collectors.toList());

List<Long> sysUserIds = userList.stream().filter(user -> user.getState() == 1).map(user -> user.getId()).collect(Collectors.toList())

// sorted按照创建时间正序排列
userList = userList.stream().sorted(Comparator.comparing(User::getCreateTime)).collect(Collectors.toList());
// sort单字段排序,根据id排序
userList.sort(Comparator.comparing(User::getId));
// sort多字段排序,根据id,年龄排序
userList.sort(Comparator.comparing(User::getId).thenComparing(User::getAge));

// 根据创建时间倒叙排序
userList = userList.stream().sorted(Comparator.comparing(User::getCreateTime).reversed()).collect(Collectors.toList());


//求和
//基本类型
int sumAge = userList.stream().mapToInt(User::getAge).sum();
//BigDecimal求和(需过滤掉bigDecimal对象为null的情况,否则可能会报空指针)
BigDecimal sumSalary = userList.stream().map(User::getSalary).filter(s -> s != null).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal sumSalary = userList.stream().map(User::getSalary).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);



List<DeptVO> deptVOList = userList.stream().filter(user -> user.getDeptId == 111L).collect(ArrayList::new, (list, object) -> {
    DeptVO vo = new deptVO();
    vo.setPhone(object.getPhone());
    vo.setSalary(object.getSalary());
    list.add(vo);
}, List::addAll);
 
// 或者
List<DeptVO> deptVOList = userList.stream().filter(user -> user.getDeptId == 111L).map(user -> {
    DeptVO vo = new deptVO();
    vo.setPhone(user.getPhone());
    vo.setSalary(user.getSalary());
    return vo;
}).collect(Collectors.toList());



/**
 * List -> Map
 * 需要注意的是:
 * toMap 如果集合对象有重复的key,会报错Duplicate key ....
 *  user1,user2的id都为1。
 *  可以用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2
 */
Map<Long, User> userMap = userList.stream().collect(Collectors.toMap(User::getId, a -> a,(k1,k2)->k1))


//声明一个List集合
List<Person> list = new ArrayList();  
        list.add(new Person("1001", "小A"));  
        list.add(new Person("1002", "小B"));  
        list.add(new Person("1003", "小C"));
        System.out.println(list);
//将list转换map
Map<String, String> map = list.stream().collect(Collectors.toMap(Person::getId,Person::getName));

Map<String, ParamCalcrate> paramCalcrateMap = paramCalcrates.stream().collect(Collectors.toMap(paramCalcrate -> paramCalcrate.getTpId(), Function.identity()));
// 需求:取手机号为13800000001的第一条数据的名字。
// 取手机号为13800000001的第一条数据,如果不存在则返回null
String name = null;
User user = userList.stream().filter(u -> "13800000001".equals(u.getPhone())).findFirst().orElse(null);
if (user != null){
    name = user.getName();
}
 
// 也可以通过Optional.isPresent判断是否存在
Optional<User> first = userList.stream().filter(u -> "13800000001".equals(u.getPhone())).findFirst();
if (first.isPresent()) {
    name= first.get().getName();
}
 
// 过滤后,通过map取名字,如果数据不存在则返回空
name = userList.stream().filter(u -> "13800000001".equals(u.getPhone())).findFirst().map(User::getName).orElse(StringUtils.EMPTY);

//排序  
 carerVehEntityList = carerVehEntityList.stream().sorted(
                    Comparator.comparing(CarerVehInOrOutEntity::getDelintSortNum, Comparator.nullsFirst(Integer::compareTo))
                    .thenComparing(CarerVehInOrOutEntity::getDelintName, Comparator.nullsFirst(String::compareTo))
                    .thenComparing(CarerVehInOrOutEntity::getLadingSn, Comparator.nullsFirst(String::compareTo))
                    .thenComparing(CarerVehInOrOutEntity::getProductSortNum, Comparator.nullsFirst(Integer::compareTo))
                    .thenComparing(CarerVehInOrOutEntity::getProductName, Comparator.nullsFirst(String::compareTo))
                    .thenComparing(CarerVehInOrOutEntity::getProductGradeSortNum, Comparator.nullsFirst(Integer::compareTo))
                    .thenComparing(CarerVehInOrOutEntity::getProductGradeName, Comparator.nullsFirst(String::compareTo))
                    .thenComparing(CarerVehInOrOutEntity::getLadingId, Comparator.nullsFirst(Long::compareTo))
                  ).collect(Collectors.toList());

你可能感兴趣的:(Java,list,数据结构)