概念
在jdk1.8中引入的新的对集合中数据的处理的--Stream流.
该工具类常常被我们用来对集合中数据的筛选,处理等等。
使用流可以大大的减少我们的代码量。
补充:
Stream 不是集合元素,它不是数据结构并不保存数据,它是有关算法和计算的,它更像一个高级版本的 Iterator。原始版本的 Iterator,用户只能显式地一个一个遍历元素并对其执行某些操作;高级版本的 Stream,用户只要给出需要对其包含的元素执行什么操作,比如 “过滤掉长度大于 10 的字符串”、“获取每个字符串的首字母”等,Stream 会隐式地在内部进行遍历,做出相应的数据转换。
Stream 就如同一个迭代器(Iterator),单向,不可往复,数据只能遍历一次,遍历过一次后即用尽了,就好比流水从面前流过,一去不复返。
而和迭代器又不同的是,Stream 可以并行化操作,迭代器只能命令式地、串行化操作。顾名思义,当使用串行方式去遍历时,每个 item 读完后再读下一个 item。而使用并行去遍历时,数据会被分成多个段,其中每一个都在不同的线程中处理,然后将结果一起输出
为什么需要Stream?
Stream 作为 Java 8 的一大亮点,它与 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念。它也不同于 StAX 对 XML 解析的 Stream,也不是 Amazon Kinesis 对大数据实时处理的 Stream。
流的使用详解:
(1) 过滤数据
//1.过滤出偶数,并且不重复的元素。distinct() 代表去重数据
List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 3, 2, 4);
numbers.stream()
.filter(i -> i % 2 == 0)
.distinct()
.forEach(System.out::println);
效果如下:
(2)遍历数据
//2.使用Stream 流遍历数据
List<String> list = Arrays.asList("soup", "soup_tang", "tang");
List<String> newList = list.stream().collect(Collectors.toList());
System.out.println(newList);
效果如下:
(3)截断数据流
//3.截断流
List<Integer> numbers = Arrays.asList(1,2,3,4,5,6);
List<Integer> newList = numbers.stream()
.filter(d -> d > 2)
.limit(4)
.collect(toList());
System.out.println(newList);
效果如下:
(4)跳过元素
//4.跳过元素 skip(1)代表跳过一个元素
List<Integer> numbers = Arrays.asList(1,2,3,4,5,6);
List<Integer> newList = numbers.stream()
.filter(d -> d > 2)
.skip(1)
.collect(toList());
System.out.println(newList);
效果如下:
(5)查找和匹配数据
allMatch的使用:
// allMatch的使用
List<Integer> list = Arrays.asList(1,2,3,4,5,6);
if(list.stream().allMatch(u -> u>=1)){
System.out.println("元素大于3");
}else{
System.out.println("元素小于3");
}
效果如下:
noneMatch的使用
//noneMatch的使用
List<Integer> list = Arrays.asList(1,2,3,4,5,6);
if(list.stream().noneMatch(u -> u>=1)){
System.out.println("元素大于3");
}else{
System.out.println("元素小于3");
}
效果如下:
(6) 使用流的Api 求和
//使用流的Api 求和
List<Integer> list = Arrays.asList(1,2,3,4,5,6);
int sum2 = list.stream().reduce(0, (a,b) -> a + b);
System.out.println("求和之后的值为:"+sum2);
效果如下:
(7)求最大值 最小值
List<Integer> list = Arrays.asList(1,2,3,4,5,6);
// 求最大值
Optional<Integer> max = list.stream().reduce(Integer::max);
System.out.println("最大值为:"+max);
// 求最小值
Optional<Integer> min = list.stream().reduce(Integer::min);
System.out.println("最小值为:"+min);
效果如下:
(8) 创建一个流
- 8.1 给值创建流
Stream stream = Stream.of("a", "b", "c");
- 8.2 由数组创建流
String [] strArray = new String[] {"a", "b", "c"};
Stream stream = Arrays.stream(strArray);
- 8.3 文件转换成流
//文件转换成流
try {
Stream<String> lines = Files.lines(Paths.get("D:/ruanjian/aa.txt"), Charset.defaultCharset());
System.out.println(lines);
} catch (IOException e) {
e.printStackTrace();
}
(9)Stream 类的排序
List<Product> productList=Arrays.asList(new Product(1,10,"牛奶"),new Product(2,20,"咖啡"),new Product(3,30,"矿泉水"));
//1.//自然排序
List<Product> productList1=productList.stream().sorted().collect(Collectors.toList());
System.out.println("1.自然排序的值为:"+productList1);
//2.倒序
List<Product> productList2=productList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
System.out.println("2.倒序:"+productList2);
//3.根据数量自然顺序
List<Product> productList3=productList.stream().sorted(Comparator.comparing(Product::getCount)).collect(Collectors.toList());
System.out.println("3.根据数量自然顺序:"+productList3);
//4.根据数量倒序
List<Product> productList4=productList.stream().sorted(Comparator.comparing(Product::getCount).reversed()).collect(Collectors.toList());
System.out.println("4.根据数量倒序:"+productList4);
productList4.forEach(product -> System.out.println("id----- "+product.getId()+" ;count---------- "+product.getCount()+";name ------------ "+product.getProductName()));
效果如下:
(10) 其中涉及的类:
package com.test.model;
import io.swagger.annotations.ApiModelProperty;
/**
* @Author tanghh
* @Date 2020/3/8 9:24
* 商品类
*/
public class Product {
@ApiModelProperty(value = "id")
private int id;
@ApiModelProperty(value = "商品数量")
private int count;
@ApiModelProperty(value = "商品名称")
private String productName;
public Product(int id, int count, String productName) {
this.id = id;
this.count = count;
this.productName = productName;
}
public Product(int count, String productName) {
this.count = count;
this.productName = productName;
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
", count=" + count +
", productName='" + productName + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
}
参考文章
https://blog.csdn.net/sunjin9418/article/details/53086565
https://blog.csdn.net/zyhlwzy/article/details/78625136
https://www.jianshu.com/p/cf9a53a95ce3
如有写的不完整的地方欢迎评论区补充