java8:Stream例子

1.

//在 7 中,要发现 type = grocery 的所有交易,然后返回以交易值降序排序好的交易 ID 集合
List<Transaction> groceryTransactions = new Arraylist<>();
for(Transaction t: transactions){
 if(t.getType() == Transaction.GROCERY){
 groceryTransactions.add(t);
 }
}
Collections.sort(groceryTransactions, new Comparator(){
 public int compare(Transaction t1, Transaction t2){
 return t2.getValue().compareTo(t1.getValue());
 }
});
List<Integer> transactionIds = new ArrayList<>();
for(Transaction t: groceryTransactions){
 transactionsIds.add(t.getId());
}

//java8
List<Transaction> transactionIds = transactions.parallelStream()
    .fliter(t->t.getType()==Transaction.GROCERY)
    .sorted(comparing(Transaction::getValue).reversed)
    .map(Transaction::getId)
    .collect(toList());

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页