Java开发-流处理

流操作

拼接

Collectors.joining(",") 
Collectors.joining(",", "[", "]")

统计

Collectors.counting()

计数

Collectors.summingLong(String::length)

 数据源

数据源
1.如果是数组或者list<基础类型>

String[] listValue = {"i", "love", "leetcode", "i", "love", "coding", "code", "test", "auto", "shit", "auto"};
Function.identity() 数据源,一个一个循环传递"i"

Map<String, Long> mapmm=   Arrays.stream(listValue).collect(Collectors.groupingBy(Function.identity(),Collectors.summingLong(String::length)));


2.如果是List<对象>
List<对象> list=new  List<对象>();
对象::属性

流收集器操作方式

 groupingBy分组

Collectors.groupingBy(Function.identity(),Collectors.joining(","))

 Collectors.mapping拼接

String mapmm=   Arrays.stream(listValue).collect(Collectors.mapping(Function.identity(), Collectors.joining(",", "[", "]")));

Collectors.toMap转Map

stream().collect(Collectors.toMap(Person::getName, p -> p));

数据转换

Collectors.toCollection(Supplier<C> collectionFactory)

  //直接创建treeSet数据 [1, 3, 4]
TreeSet<Integer> collect3= Stream.of(1, 3, 4).collect(Collectors.toCollection(TreeSet::new));

 List<String> list = Arrays.asList("java", "python", "C++","php","java");
        //用LinkedList收集
List<String> linkedListResult = list.stream().collect(Collectors.toCollection(LinkedList::new));
        linkedListResult.forEach(System.out::println);
       
//用CopyOnWriteArrayList收集
List<String> copyOnWriteArrayListResult = list.stream().collect(Collectors.toCollection(CopyOnWriteArrayList::new));
        copyOnWriteArrayListResult.forEach(System.out::println);
       
//用TreeSet收集
TreeSet<String> treeSetResult = list.stream().collect(Collectors.toCollection(TreeSet::new));
        treeSetResult.forEach(System.out::println);

流转list

Collectors.toList()

List<String> nameCollect = platformUsers.stream().map(PlatformUser::getName).collect(Collectors.toList());

变量转list

String teamId=“”;

Collections.singletonList(teamId)

变量转map

Collections.singletonMap(key,value)

map数据过滤方式跟list过滤方式不一样

这个过滤true表示要元素

Map<Integer, Integer> dataMap = MapUtil.filter(rootMap, entry -> Constants.INT1.equals(entry.getValue()));

创建一个步长为几的流

这个方法用于生成一个顺序排列的整数流,包含起始值但不包含结束值

可以从下面的例子中发IntStream.range,可以创造一个流,来执行你需要的逻辑

 dataMap.forEach((teamId, teamList) -> IntStream.range(0, teamList.size())
              
            .filter(i -> teamList.get(i).getId().equals(teamId)).findAny().ifPresent(i -> levelMap.put(teamId, i + 1)));

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值