谈起Java Stream API,我们希望能够弄明白它究竟是什么,能够用来做什么,有什么优势,并能够应用于具体场景。分别简述如下。
?
用于对集合对象进行各种便利、高效的聚合操作,或大批量数据操作
?
以往对于集合的聚合操作,需要使用Iterator遍历集合,代码繁冗;对于过滤和计算得到的中间结果,需要额外的空间进行存储
Java Stream API解决了以上问题,遍历逻辑可以精简为一行,使得代码更加简洁易读。
Java Stream提供串行和并行两种模式进行汇聚操作,能够充分利用多核处理器的优势,更方便写出高性能的并发程序且不易出错
?
StreamAPIDemo
定义商品对象并初始化购物清单如下:
?
class="java" name="code">public class StreamAPIDemo {
private List<Item> shoppingList;
@Before
public void setUp() throws Exception {
shoppingList = Lists.newArrayList();
shoppingList.add(new Item("iPhone 7", 7250L));
shoppingList.add(new Item("Rolex Watch", 28888L));
shoppingList.add(new Item("Electric Toothbrush", 388L));
shoppingList.add(new Item("Kindle Paperwhite", 1098L));
shoppingList.add(new Item("Coca Cola", 3L));
}
class Item {
String itemName;
long price;
public Item(String itemName, long price) {
this.itemName = itemName;
this.price = price;
}
}
}
?
?
// 打印购物清单
@Test
public void printShoppingList(){
shoppingList.stream().forEach(System.out::println);
}
输出:
?
?
Item{itemName='iPhone 7', price=7250}
Item{itemName='Rolex Watch', price=28888}
Item{itemName='Electric Toothbrush', price=388}
Item{itemName='Kindle Paperwhite', price=1098}
Item{itemName='Coca Cola', price=3}
?
?
?
// 统计购物清单总数与总价
@Test
public void printTotalPrice() {
long itemNum = shoppingList.stream().count();
System.out.println("Sum of items in the shopping list: " + itemNum);
long totalPrice = shoppingList.stream().collect(Collectors.summingLong(Item::getPrice));
System.out.println("Total price: " + totalPrice);
}
输出:
?
?
Sum of items in the shopping list: 5 Total price: 37627
?
?
// 过滤价格小于1000的商品
@Test
public void filterItems(){
shoppingList.stream().filter(p -> p.getPrice() >= 1000L).forEach(System.out::println);
}
// 按价格排序
@Test
public void sortItemsByPrice(){
shoppingList.stream().sorted(Comparator.comparingLong(Item::getPrice)).forEach(System.out::println);
}
?
// 所有商品价格减100 - Map
@Test
public void streamMap(){
shoppingList.stream().map(p -> new Item(p.getItemName(), p.getPrice()-100)).forEach(System.out::println);
}
?
?
// 取出如上优惠之后金额超过1000元的商品中,价格最低的商品(最便宜的奢侈品)
@Test
public void streamReduce(){
Item cheapestLuxury = shoppingList.stream().map(p -> new Item(p.getItemName(), p.getPrice()-100))
.filter(p -> p.getPrice() > 1000).reduce((a, b) -> a.getPrice()<b.getPrice()?a:b).get();
System.out.println(cheapestLuxury);
}
?Original Link
?
?
?
?
?
?
?