Recently, in development, I saw my colleagues use stream for data conversion of collections List<object> ->List<String>
List<object> --> Map<Long, object>, it feels very simple and convenient. I found some information on the Internet to learn
Java 8 Stream | Novice Tutorial
Record our common usage
.stream() − Creates a serial stream for a collection
1、List<Object> --> List<String>
//List<Object> --> List<String>
List<User> resultList = new ArrayList<>();
List<String> newList = resultList.stream().map(e -> e.getName()).collect(Collectors.toList());
// List<map> --> List<String>
List<String> contractIds = array.stream().map(m -> MapUtils.getString(m, "contractId").toString()).collect(Collectors.toList());
2、List<User> --> Map<Long, User>
List<User> --> Map<Long, User>
List<User> resultList = new ArrayList<>();
Map<Long, User> idToUser = resultList.stream().collect(Collectors.toMap(User::getId, (user -> user)));
3、List<Map<String, Object>> --> Map<String, Integer> countMap
List<user> --> Map<String, Integer> countMap
List<Map<String, Object>> reList = stdGoodsTempDao.queryCountByRegcardnm(paramMap);
Map<String, Integer> countMap = reList.stream().collect(Collectors.toMap(map -> (String) map.get("regcardnm"), (map -> (Integer) map.get("goodsCount"))));
// List<user> --> Map<String, Integer> countMap
Map<Integer, String> countMap = list.stream().collect(Collectors.toMap(e -> e.getId(), e -> e.getBillNum()));
4. .stream().parallel().forEach easily implements multithreading
List<StdRegcardTemp> lists=new ArrayList<>(10);
lists.stream().parallel().forEach(item -> { //具体处理 });
5. Get the piece of data with the maximum value of a certain field,
.stream().max(Comparator.comparing(Student::getRecord)).get();
Student student = stu.stream().max(Comparator.comparing(Student::getRecord)).get();
6. list<object> --> list<object> Get the return that meets the conditions
.filter( Get the data of type =1
List<Cycle> cycleStream = cycleList.stream().filter(
cycle -> 1.equals(cycle.getType())).collect(Collectors.toList());
7. list<object> --> list<Integer> Get the number that satisfies the condition, and take out the specified field
List<Integer> projectConfigIds = cycleList.stream().filter(
cycle -> "1".equals(cycle.getType()))
.map(Cycle::getId)
.collect(Collectors.toList());
8. Modify the value of the object attribute, and batch modify a certain value of the object in the list
StdSubcodeBhs = StdSubcodeBhs.stream().peek(x -> x.setSubmitState(1)).collect(Collectors.toList());
9. List deduplication
List newList = (List) list.stream().distinct().collect(Collectors.toList());
10. Field type conversion List<Integer> --> List<String>
List<Integer> declareTypes = new ArrayList();
List<String> declareTypes1 = declareTypes.stream().map(String::valueOf).collect(Collectors.toList());
----The following is a reference extension
filter use refer to this article, well written
Stream filter() filters valid data_popcorn9958's blog-CSDN blog_stream.filter