Java JDK8 Stream流操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tiankongzhichenglyf/article/details/82216895

一、流的初始化:
  1、Stream.of(T t);
  2、Arrays.stream(strArray);
  3、list.stream();

二、流的操作:

List<String> strList= new ArrayList<>();
strList.add("aaa");
strList.add("bbb");
strList.add("ccc");

1、forEach

strList.stream().forEach(System.out::println);

2、map(转换为大写)

List<String> newStr= strList.stream().map(String::toUpperCase).collect(Collectors.toList());

3、flatMap(根据部门id查找下级部门id)

List<String> strings = accountOrgDao.findOrgIdByAccountId(userId);
List<String> orgIds = strings.stream().flatMap(item -> orgDao.getChild(item).stream().map(Org::getId))
      .collect(Collectors.toList());

4、filter(不为空)

long count = strList.stream().filter(StringUtils::isNotBlank).count();

猜你喜欢

转载自blog.csdn.net/tiankongzhichenglyf/article/details/82216895