笔记簿
ᴄᴏᴅɪɴɢ ɪs ᴀʀᴛ
首页
关于
搜索
登录
注册
Java8 stream - 流式计算常用案例
### Java8流式计算常用案例 Stream 作为 Java 8 的一大亮点,它与 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念。它也不同于 StAX 对 XML 解析的 Stream,也不是 Amazon Kinesis 对大数据实时处理的 Stream。Java 8 中的 Stream 是对集合(Collection)对象功能的增强,它专注于对集合对象进行各种非常便利、高效的聚合操作(aggregate operation),或者大批量数据操作 (bulk data operation)。Stream API 借助于同样新出现的 Lambda 表达式,极大的提高编程效率和程序可读性。同时它提供串行和并行两种模式进行汇聚操作,并发模式能够充分利用多核处理器的优势,使用 fork/join 并行方式来拆分任务和加速处理过程。通常编写并行代码很难而且容易出错, 但使用 Stream API 无需编写一行多线程的代码,就可以很方便地写出高性能的并发程序。所以说,Java 8 中首次出现的 java.util.stream 是一个函数式语言+多核时代综合影响的产物。 #### 1.List转Map ```java /** * List -> Map * 需要注意的是: * toMap 如果集合对象有重复的key,会报错Duplicate key .... * apple1,apple12的id都为1。 * 可以用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2 */ Map
appleMap = appleList.stream() .collect( Collectors.toMap(Apple::getId, a -> a,(k1,k2)->k1) ); ``` ##### 常用方式 代码如下: ```java public Map
getIdNameMap(List
accounts) { return accounts.stream().collect( Collectors.toMap(Account::getId, Account::getUsername) ); } ``` ##### 收集成实体本身map 代码如下: ```java public Map
getIdAccountMap(List
accounts) { return accounts.stream().collect( Collectors.toMap(Account::getId, account -> account) ); } ``` **account -> account**是一个返回本身的lambda表达式,其实还可以使用Function接口中的一个默认方法代替,使整个方法更简洁优雅: ```java public Map
getIdAccountMap(List
accounts) { return accounts.stream().collect( Collectors.toMap(Account::getId, Function.identity()) ); } ``` ##### 重复key的情况 代码如下: ```java public Map
getNameAccountMap(List
accounts) { return accounts.stream().collect(Collectors.toMap(Account::getUsername, Function.identity())); } ``` 这个方法可能报错(**java.lang.IllegalStateException: Duplicate key**),因为name是有可能重复的。**toMap**有个重载方法,可以传入一个合并的函数来解决key冲突问题: ```java public Map
getNameAccountMap(List
accounts) { return accounts.stream().collect(Collectors.toMap(Account::getUsername, Function.identity(), (key1, key2) -> key2)); } ``` 这里只是简单的使用后者覆盖前者来解决key重复问题。 ##### 指定具体收集的map **toMap**还有另一个重载方法,可以指定一个Map的具体实现,来收集数据: ```java public Map
getNameAccountMap(List
accounts) { return accounts.stream().collect(Collectors.toMap(Account::getUsername, Function.identity(), (key1, key2) -> key2, LinkedHashMap::new)); } ``` #### 2. 分组 ```java Map
> collect = investDetails.stream().collect(Collectors.groupingBy(InvestDetail::getTransfId)); //List 以ID分组 Map
> Map
> groupBy = appleList.stream() .collect(Collectors.groupingBy(Apple::getId)); ``` #### 3. 过滤filter ```java // 6个月内的申请次数 int times = histories.stream() .filter(temp -> temp.isOrg_self() == false) .filter( t -> new Period( new DateTime(t.getSearched_date()), new DateTime(), PeriodType.months() ).getMonths() <= 6 ) .collect(Collectors.toList()).size(); //过滤出符合条件的数据 List
filterList = appleList.stream() .filter( a -> a.getName().equals("香蕉") ) .collect(Collectors.toList()); List
fullSmartPlans = list.stream() .filter(temp -> temp.getFinishtime() > (time)) .filter( temp -> temp.getTotalMoney().compareTo( temp.getBuyMoney() ) != 1 ) .collect(Collectors.toList()); ``` #### 4. 求和 将集合中的数据按照某个属性求和: ##### BigDecimal: ```java BigDecimal sumSuccessMoney = byInvestId.stream() .map(BorrowTenderTransfer::getSuccessMoney) .reduce(BigDecimal.ZERO, BigDecimal::add); ``` ##### Integer: ```java //计算 数量 int sum = appleList.stream().mapToInt(Apple::getNum).sum(); ``` #### 5.排序 map根据value排序 用LinkedHashMap来收集排好序的结果 ```java Map
countMap = Maps.newConcurrentMap(); // 根据value从大到小排序 return countMap.entrySet().stream() .sorted((v1, v2) -> v2.getValue() - v1.getValue()) .collect( Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1, Maps::newLinkedHashMap ) ); ```