作为 Java 8 众多新特性之一,Stream API 带来了一种新的抽象概念,它允许以 “声明式” 的方法来操作数据集合。我们先看下面的代码:
class Good { String name; // 商品名称 long price; // 价格 long sales; // 销量 public Good(String name, long price, long sales) { this.name = name; this.price = price; this.sales = sales; } public long getPrice() { return price; } public void setPrice(long price) { this.price = price; } public long getSales() { return sales; }}void process(List goods) { // // 筛选 price > 500 & sales < 200 的商品, 价格最高的 10 件商品, 价格减半(双十一来啦!) // goods.stream() .filter(c -> c.getPrice() > 500 && c.getSales() < 200) .sorted(Comparator.comparing(Good::getPrice).reversed()) .limit(10) .forEach(c -> { c.setPrice(c.getPrice() / 2); });}
想象一下,Java 8 之前的 process(List goods)
,将是怎么样的一种实现 ^_^,Stream API,就是这么简单。(当然,还借助了 Java 8 的 Lambda 表达式与方法引用)
通过本场 Chat,我们将能够了解 Stream API 的基础概念、常见的操作,并探索有关并行数据处理与性能的话题。此外,如果对于 Lambda 表达式和方法引用还没有多少了解,我们也会简要地阐述。
阅读全文: http://gitbook.cn/gitchat/activity/59eceece3b6aa74d142841ad
您还可以下载 CSDN 旗下精品原创内容社区 GitChat App ,阅读更多 GitChat 专享技术内容哦。