赞
踩
从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
- List<String> nodeNames = questionMap.values().stream()
- .flatMap(s -> s.getNodeNames().stream())
- .collect(Collectors.toList());
有一个Map<key,value> , 有一个List<key>, 传入一个List<key>,匹配Map<key,value> 返回一个List<value>
- Map<String, Integer> map = new HashMap<>();
- map.put("one", 1);
- map.put("two", 2);
- map.put("three", 3);
- map.put("four", 4);
-
- List<String> keys = List.of("two", "three", "five"); // 输入的key列表
-
- List<Integer> values = keys.stream()
- .filter(map::containsKey) // 过滤出map中存在的key
- .map(map::get) // 通过map获取对应的value
- .collect(Collectors.toList()); // 收集到列表
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。