Java集合类之---(Stream数据流)

Collection接口改进

  • 普通方法
    1. forEach()输出支持:default void forEach(Consumer<? super T> action)
    1. 取得Stream数据流对象:default Stream stream()
  • 使用forEach()输出:
public class Test {
public static void main(String[] args) {
List<String> list = new ArrayList<>() ;
Collections.addAll(list,"Java","C++","Python","PHP") ;
// 方法引用
list.forEach(System.out::println) ;
}
}

Stream操作

  • 将集合数据交给Stream之后,就相当于这些数据一个一个进行处理

数据过滤

  • 满足某些条件的内容才允许做数量统计
public class Test {
public static void main(String[] args) {
List<String> list = new ArrayList<>() ;
Collections.addAll(list,"Java","C++","Python","JavaScript") ;
// 实例化Stream对象
Stream<String> stream = list.stream() ;
// 统计出这些数据中带有Java的个数
System.out.println(stream.filter((e)->e.contains("Java")).count());
}
}

收集器

  • 希望在数据过滤后得到具体数据,就可以使用收集器来完成
  • 收集器:public <R, A> R collect(Collector<? super T, A, R> collector)
public static void main(String[] args) {
List<String> list = new ArrayList<>() ;
Collections.addAll(list,"Java","C++","Python","JavaScript") ;
// 实例化Stream对象
Stream<String> stream = list.stream() ;
// 收集过滤后的数据
System.out.println(stream.filter((e)->e.contains("Java"))
.collect(Collectors.toList()));
}
  • 收集完的数据依然属于List集合,所以可以直接使用List进行接收

skip与limit方法

  • 在Stream接口中重点有两个操作方法:
    1. 设置取出最大内容:public Stream limit(long maxSize)
    1. 跳过的数据量:public Stream skip(long n)
如下例子是【跳过 0 个,取 1 个进行操作】

 public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        Collections.addAll(list,"java","C++","Python","PHP");
        //实例化对象
        Stream<String> stream = list.stream();
        List<String> resultList = stream.skip(0).limit(1).map((s)->s.toUpperCase()).collect(Collectors.toList());
        System.out.println(resultList);
    }
  • 使用了skip()、limit()方法对数据做了分页处理

MapReduce基础模型

  • MapReduce是整个Stream的核心所在;操作也是由两个阶段所组成:

    1. map():指的是针对于数据进行先期的操作处理。例如:简单的数学运算等
    1. reduce():进行数据的统计分析
  • 在Stream接口中就提供有一个map结果变为Double型的操作:

  • 统计分析: public DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper)

  • 返回的是一个DoubleStream接口对象

  • 统计方法:DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper)

class Order {
private String title;
private double price;
private int amount;
public Order(String title, double price, int amount) {
this.title = title;
this.price = price;
this.amount = amount;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
}
public class Test {
public static void main(String[] args) {
List<Order> orderList = new ArrayList<>();
orderList.add(new Order("Iphone", 8999.99, 10));
orderList.add(new Order("外星人笔记本", 12999.99, 5));
orderList.add(new Order("MacBookPro", 18999.99, 5));
orderList.add(new Order("Java从入门到放弃.txt", 9.99, 20000));
orderList.add(new Order("中性笔", 1.99, 200000));
DoubleSummaryStatistics dss = orderList.stream().mapToDouble((obj) -> obj.getPrice() *
obj.getAmount())
.summaryStatistics();
System.out.println("总量: " + dss.getCount());
System.out.println("平均值: " + dss.getAverage());
System.out.println("最大值: " + dss.getMax());
System.out.println("最小值: " + dss.getMin());
System.out.println("总和: " + dss.getSum());
}
}

猜你喜欢

转载自blog.csdn.net/WZL995/article/details/85863325
今日推荐