集合(持续更新)

  1. 抽取对象集合的某个属性为一个新的集合
    例:
List<Car> carList;
//抽取集合所有车id
List<Long> carIdList = carList.stream().map(Car::getId).collect(Collectors.toList());

2.抽取对象集合的某个属性为一个新的集合,并去重

 List<String> list=orderList.stream().map(OrderInfo::getOrderNo).distinct().collect(Collectors.toList());

3.抽取对象集合的某条符合记录的数据

Optional<Cart> cartOptional = cartlist.stream().filter(item -> item.getProductId().equals(1L)).findFirst();
  1. 根据条件过滤集合中的数据,并抽取对象的两个属性作为map key和value
如:从集合 List<Student> student; 中剥离每个学生信息的id,name为map ,map<id,name>
 HashMap<Long, String> studentCodeInfoMap = (HashMap<Long, String>) standardCodeInfoList.stream()
                .filter(t -> t.getStudentNo() == null)
                .collect(Collectors.toMap(Student::getId, Student::getName, (k1, k2) -> k2));

map的value为对象

List<Student> studentList;
Map<Long, Staudent> staudentMap= studentList.stream().collect(Collectors.toMap(Staudent::getId, a-> a));
  1. 集合分组
List<Order> data;
Map<String, List<Order>> listMap = data.stream().collect(Collectors.groupingBy(Order::getOrderNo));
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容