当前位置:   article > 正文

java8 stream的使用_java stream map key对id value为对象

java stream map key对id value为对象

list集合中拿出两个字段组成MAP

Map<Integer, String> userMap = userList.stream().collect(Collectors.toMap(User::getTjid, User::getName))

从list集合中拿出一个字段作为key,对象作为value

Map<Integer, User> userMap = userList.stream().collect(Collectors.toMap(User::getTjid, Function.identity()));

从list对象集合中提取出一个字段组成list,并且去重

Set<Integer> opttjidSet = tbTradeRecords.stream().map(TbTradeRecord::getOperatortjid).collect(Collectors.toSet());

把list通过某个属性分组

Map<Integer,List<LoanRecordInfoResp>> loanRecordGroup = loanRecords.stream().collect(Collectors.groupingBy(LoanRecordInfoResp::getTjid));

java Stream 把对象中的一个List属性 抽出来形成一个List

  1. List<String> nodeNames = questionMap.values().stream()
  2. .flatMap(s -> s.getNodeNames().stream())
  3. .collect(Collectors.toList());

有一个Map<key,value> , 有一个List<key>, 传入一个List<key>,匹配Map<key,value> 返回一个List<value>

  1. Map<String, Integer> map = new HashMap<>();
  2. map.put("one", 1);
  3. map.put("two", 2);
  4. map.put("three", 3);
  5. map.put("four", 4);
  6. List<String> keys = List.of("two", "three", "five"); // 输入的key列表
  7. List<Integer> values = keys.stream()
  8. .filter(map::containsKey) // 过滤出map中存在的key
  9. .map(map::get) // 通过map获取对应的value
  10. .collect(Collectors.toList()); // 收集到列表

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/537127
推荐阅读
相关标签
  

闽ICP备14008679号