java8 按照list中对象的子对象的某个属性排序

public class A{

 private B b;

}



public class B{

  private int id;

}

//对于List<A> 按照B对象的id进行排序:

list.stream().sorted(Comparator.comparing(x-> x.getB().getId())).collect(Collectors.toList());

  

追加:flatMap用法,使用Java8 将集合对象中嵌套的集合扁平化 

类示例:

public class CaseRoleDTO {
     private List<CaseGoodsTypeDTO> typeList;
}

public class CaseGoodsTypeDTO {
    private List<CaseGoodsDTO> goodsList;
}

public class CaseGoodsDTO {
    //....
}

 

List<CaseRoleDTO> caseRoleDtoList = Lists.newArrayList();
List<CaseGoodsDTO> caseGoodsList = caseRoleDtoList.stream()
        .filter(x -> Collections3.isNotEmpty(x.getTypeList()))
        .flatMap(x -> x.getTypeList().stream().flatMap(v -> v.getGoodsList().stream()))
        .collect(Collectors.toList());

 

posted @ 2020-06-18 11:10  伯爵~  阅读(9000)  评论(0编辑  收藏  举报