Stream
1:把字符串列表转成大写 放到一个字符串列表中:
List<String> test = Stream.of("tomc", "javk", "Hello", "DICK").map(a -> a.toUpperCase()).collect(toList());
System.out.println(test); //输出: [TOMC, JAVK, HELLO, DICK]
2: 统计整型数组 中大于 30 的数字放到整型数组中:
List<Integer> test2 = Stream.of(20,40,60,80,45).filter(b->(b>30)).collect(Collectors.toList());
System.out.println(test2); // 输出: [40, 60, 80, 45]
3: List<String> lines = Arrays.asList("spring", "node", "mkyong"); 把几个字符串转化成ArrayList。
4: 获取列表 对象中的一个字段,放入集合set中(去重):
List<MongoMoguAttendenceDto> result = moguAttendanceDao.getMaxAttendanceTimeByStu();
Set<String> studentIds = result.stream().map(MongoMoguAttendenceDto::getMemberId).collect(Collectors.toSet());
5: 从对象list 中过滤出 符合条件的对象,并把过滤后的对象的某个字段放到 set 中。
如下: 从 Person 列表中过滤出 地址是 changsha 的 Person, 并把这些Person 的 名字放到 set<String> 中:
Person a = new Person("zhangsan",20,"wuhan");
Person b = new Person("lisi",30,"changsha");
List<Person> list = new ArrayList<>();
list.add(a); list.add(b);
String addr = "changsha";
Set<String> set = list.stream().filter(p->p.getAddress().equals(addr)).map(Person::getName).collect(Collectors.toSet());
System.out.print(set); ---输出: [lisi]
6: 对一个 List<GxyWarnEntity> warns 对象 ,用对象中的一个字段schoolId分组,保存在 Map<String, List<GxyWarnEntity>> 中:
Map<String, List<GxyWarnEntity>> pushByTen= warns.stream().collect(Collectors.groupingBy(s->s.getSchoolId())); List<GxyTeacherDto> schoolTeachers = gxyTeacherMapper.selectAllTeacherByRoleNameAndDepIds(EnumRoleType.SCHOOLMANAGER.getKey(),null,new ArrayList<>(pushByTen.keySet())); if (schoolTeachers!=null && schoolTeachers.size() > 0) { schoolTeachers.stream().forEach(d -> { if (pushByTen != null && pushByTen.get(d.getSchoolId()) != null && pushByTen.get(d.getSchoolId()).size() > 0) { List<GxyWarnEntity> allwarnTens = pushByTen.get(d.getSchoolId()).stream().filter(e -> e.getWarningType() == EnumUnsignType.UNSIGNTEN.getType()).collect(Collectors.toList()); if (allwarnTens != null && allwarnTens.size() > 0) { noticeSentRecordDao.insertWarnTen(allwarnTens, d.getSchoolId()); } } }); }
对一个 List<GxyWarnEntity> warns 对象 ,用对象中的一个字段schoolId分组,保存在 Map<String, List<GxyWarnEntity>> 中, 遍历
pushByThree 的 keySet(),
pushByThree.get(teacherId) 即可获取每个 List<GxyWarnEntity>:
Map<String, List<GxyWarnEntity>> pushByThree = warns.stream().filter(e -> StringUtils.isNotEmpty(e.getClassTeacherId())).collect(Collectors.groupingBy(s -> s.getClassTeacherId())); for (String teacherId : pushByThree.keySet()) { if (StringUtils.isNotEmpty(teacherId) && pushByThree != null && pushByThree.get(teacherId).size() > 0) { List<GxyWarnEntity> allwarnThrees = pushByThree.get(teacherId).stream().filter(e -> e.getWarningType() == EnumUnsignType.UNSIGNTHREE.getType()).collect(Collectors.toList()); if (allwarnThrees != null && allwarnThrees.size() > 0) { noticeSentRecordDao.insertWarnThree(allwarnThrees, teacherId); } } }
7: 對一個 數組: List<CreditCardBasicInfo> , 把 CreditCardBasicInfo 其中的一個字段 作爲key,該對象作爲value ,保存在map中:
List<CreditCardBasicInfo> getCreditCardInfoByCustomerId(String custId);
Map<String, CreditCardBasicInfo> cardBasicInfoMap = ccCustomerMasDao.getCreditCardInfoByCustomerId(custId)
.stream()
.filter(info -> info.getLoanAccountNumber() != null)
.collect(Collectors.toMap(CreditCardBasicInfo::getLoanAccountNumber, Function.identity()));
把上面的 map 按照key排序,取 排序后的 value 放到 list 中:
List<CreditCardBasicInfo> list = cardBasicInfoMap.entrySet().stream().sorted(Map.Entry.comparingByKey()).map(e->e.getValue()).collect(Collectors.toList());