java8 in action:第四章学习,引入流Stream,比较流与集合

流是java8 API的新成员。允许声明性方式处理数据集合。它的好处就是透明地并行处理,无需写多线程代码。

先上一个demo:

实体类:
  public class Dish {

private String name;
private boolean vegetarian;
private int calories;
private Type type;

public enum Type{Meat,Fish,Other}

public Dish(String name, boolean vegetarian, int calories, Type type) {
    super();
    this.name = name;
    this.vegetarian = vegetarian;
    this.calories = calories;
    this.type = type;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public boolean isVegetarian() {
    return vegetarian;
}
public void setVegetarian(boolean vegetarian) {
    this.vegetarian = vegetarian;
}
public int getCalories() {
    return calories;
}
public void setCalories(int calories) {
    this.calories = calories;
}
public Type getType() {
    return type;
}
public void setType(Type type) {
    this.type = type;
}
@Override
public String toString() {
    return "Dish [name=" + name + ", vegetarian=" + vegetarian + ", calories=" + calories + ", type=" + type + "]";
}

}

查询calories小于500的食物名称。

public class Test {
public static List<Dish> menu=Arrays.asList(
        new Dish("pork", false, 800, Type.Meat),
        new Dish("beef", false, 700, Dish.Type.Meat),
        new Dish("chicken", false, 400, Dish.Type.Fish),
        new Dish("french fries", true, 530, Dish.Type.Other),
        new Dish("rice", true, 350, Dish.Type.Other));

public static void main(String[] args) {
    
    List<String> lowCaloricDishesName=menu.stream()
            .filter(d -> d.getCalories()<500)
            .sorted(Comparator.comparing(Dish::getCalories))
            .map(Dish::getName)
            .collect(toList());
    
    System.out.println(lowCaloricDishesName);
}
}

记得引入的类:

import static java.util.stream.Collectors.toList;

java 8 in action该书中对流的定义:从支持数据处理操作的源生产的元素序列。

对于上面的例子:menu.stream() 得到流,数据源就是menu,它提供元素序列(访问特定值的一组有序值),filter,sorted,map,limit,collect对流进行操作。collect最后处理流返回结果。

扩展一些上面的demo,查询calories大于300的前3个食物名称。

List<String> threeNames=menu.stream()
            .filter(d -> d.getCalories()>500)
            .sorted(Comparator.comparing(Dish::getCalories))
            .map(Dish::getName)
            .limit(3)
            .collect(toList());

下面分享流的的几个特点。也是和集合不一样的地方。

只能遍历一次##

List<String> names=Arrays.asList("Tom","Jim","Joe");
    Stream<String> s=names.stream();
    s.forEach(System.out::println);
    s.forEach(System.out::println);

执行两次s.forEach(System.out::println); 报错:
   java.lang.IllegalStateException: stream has already been operated upon or closed
at java.util.stream.AbstractPipeline.sourceStageSpliterator(Unknown Source)

stream has already been operated upon or closed流已经被操作或者关闭了。这个异常很明显。

外部迭代和内部迭代##

直接看demo,查看食物名称,并且添加到集合中。

//集合处理方式
    List<String> names1=new ArrayList<>();
    for (Dish d:menu) {
        names1.add(d.getName());
    }
    
    //流处理方式
    List<String> names2=menu.stream()
                        .map(Dish::getName)
                        .collect(toList());

两者的处理方式很直观。集合使用foreach外部迭代,而流是内部迭代的方式。也就是流帮你处理了。至于foreach,涉及到并行问题了,显然在某些时候的表现是不如Stream的。

流的几个常见方法##

中间操作,也就是形成流的流水线。

filter, map ,limit,sorted,distinct 返回类型都是Stream<T>。

终端操作:
forEach,count(注意是long型),collect

   forEach使用:
  menu.forEach(System.out::println);
  menu.forEach(d -> System.out.print(d.getName()+"\t"));
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言: 讲Stream之前,先来用个小需求带入本文。毕竟代码看的最清楚。 正文: 项目某个页面有个需求,将关键词和...
    T9的第三个三角阅读 2,440评论 1 9
  • 了解Stream ​ Java8中有两个最为重要的改变,一个是Lambda表达式,另一个就是Stream AP...
    龙历旗阅读 3,369评论 3 4
  • Java8 in action 没有共享的可变数据,将方法和函数即代码传递给其他方法的能力就是我们平常所说的函数式...
    铁牛很铁阅读 1,286评论 1 2
  • Java 8的新特性可以帮助你: 1.使用Java 8可以减少冗长的代码,让代码更易于理解 2.通过方法引用和St...
    Phoenix小彬阅读 971评论 0 2
  • Streams 原文链接: Streams 原文作者: shekhargulati 译者: leege100 状态...
    忽来阅读 5,567评论 3 32