Java8新特性—Stream API总结,全程无废话,简洁明了

01.Stream API有何优势

假设有一个整形链表如下:

List<Integer> list = Arrays.asList(9, 3, 5, 6, 7, 10086, -5);

找出链表中值>=5的值,一般操作为:

 		List<Integer> res=new ArrayList<>();
        for (int i=0;i<list.size();i++){
            if(list.get(i)>=5) res.add(list.get(i));
        }

Stream API操作为:

List<Integer> res = list.stream().filter(x -> x >= 5).collect(Collectors.toList());

02.Stream API详解

2.1了解Stream

Stream 是处理集合的关键抽象概念,它可以对集合进行复杂的查找、过滤和映射数据等操作。(类似于使用 SQL 执行的数据库查询)

Stream的操作步骤

  1. 创建Stream
  2. 中间操作
  3. 终止操作

注意事项

  1. Stream 不存储元素。
  2. Stream 不会改变源对象,而是回一个持有结果的新Stream。
  3. Stream 操作会等到需要结果的时候才执行。

2.2Stream API介绍

创建Stram

1.通过集合创建流

  • 顺序流 : default Stream stream()
  • 并行流 : default Stream parallelStream()
List<Integer> list = Arrays.asList(9, 3, 5, 6, 7, 10086, -5);
Stream<Integer> stream = list.stream();//顺序流
Stream<Integer> integerStream = list.parallelStream();//并行流

2.通过数组创建

  • public static Stream stream(T[] array)
Integer[] arr={9,3,5,6,7,10086,-5};
Stream<Integer> arrStream = Arrays.stream(arr);

3.通过stream创建

  • 通过stream创建
Integer[] arr={9,3,5,6,7,10086,-5};
Stream<Integer> arrStream2 = Stream.of(arr);

中间操作

方法作用
filter(Predicate p)接收 Lambda , 从流中排除某些元素
distinct()通过流所生成元素的 hashCode() 和 equals() 去除重复元素
limit(long maxSize)获取前maxSize数量的数据
skip(long n)返回一个扔掉了前 n 个元素的流
map(Function f)接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素。
flatMap(Function f)接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流
sorted()按自然顺序排序
sorted(Comparator com)按比较器顺序排序

代码演示:

List<Integer> list = Arrays.asList(9, 3, 5, 6, 7, 10086, -5,9);

//找到list中全部>5的数据
list.stream().filter(x->x>5).forEach(x -> System.out.print(x+" "));//9 6 7 10086 9

//找到list中全部>5的数据并且去重
list.stream().filter(x->x>5).distinct().forEach(x -> System.out.print(x+" "));//9 6 7 10086 9

//找到list中全部>5的数据并且去掉第一个数据
list.stream().filter(x->x>5).skip(1).forEach(x -> System.out.print(x+" "));//6 7 10086 9

//找到list中全部>5的数据并且只要一条数据
list.stream().filter(x->x>5).limit(1).forEach(x -> System.out.print(x+" "));//9
 
 //从大到小排序
list.stream().sorted((o1,o2)-> o2-o1).forEach(x -> System.out.print(x+" "));//10086 9 9 7 6 5 3 -5

//list每一个数据自增10
list.stream().map(x->x+10).forEach(x -> System.out.print(x+" "));//19 13 15 16 17 10096 5 19

//lists每一个数据自增10
List<Integer> list2=new ArrayList<>();
list2.add(-1000);
lists.add(list);
lists.add(list2);
List<List<Integer>> lists=new ArrayList<>();//[[9, 3, 5, 6, 7, 10086, -5, 9], [-1000]]
List<Integer> collect = lists.stream().flatMap(new Function<List<Integer>, Stream<Integer>>() {
       public Stream<Integer> apply(List<Integer> tmp) {
             Stream<Integer> stream1 = tmp.stream().map(x -> x + 10);
              return stream1;
         }
}).collect(Collectors.toList());
System.out.println(collect);//[19, 13, 15, 16, 17, 10096, 5, 19, -990]

终止操作

方法返回描述
allMatch(Predicate p)boolean检查是否匹配所有元素
anyMatch(Predicate p)boolean检查是否至少匹配一个元素
noneMatch(Predicate p)boolean检查是否没有匹配所有元素
findFirst()Optional返回第一个元素
findAny()Optional返回当前流中的任意元素
collect(Collector<? super T, A, R> collector)<R, A> R将流转换为其他形式。接收一个 Collector接口的实现,用于给Stream中元素做汇总的方法
count()long返回流中元素总数
max(Comparator c)Optional返回流中最大值
min(Comparator c)Optional返回流中最小值
forEach(Consumer c)void内部迭代(使用 Collection 接口需要用户去做迭代,称为外部迭代
reduce(BinaryOperator b)Optional可以将流中元素反复结合起来,得到一个值
reduce(T identity, BinaryOperator b)T可以将流中元素反复结合起来,得到一个值
List<Integer> list = Arrays.asList(9, 3, 5, 6, 5, 10086, -5);

System.out.println(list.stream().allMatch(e->e>12));//false

System.out.println(list.stream().anyMatch(e->e>12));//true

System.out.println(list.stream().noneMatch(e->e>=10086));//false
System.out.println(list.stream().noneMatch(e->e>10086));//true

//找list的第一个数据
System.out.println(list.stream().findFirst());//Optional[9]

//找list的随意一个数据
System.out.println(list.stream().findAny());//Optional[9]

//找list的数量
System.out.println(list.stream().count());//7

//找list的最大值
System.out.println(list.stream().max(Comparable::compareTo));//Optional[10086]

//将list转化为set
System.out.println(list.stream().collect(Collectors.toSet()));//[3, -5, 5, 6, 10086, 9]

//100逐一乘以list中的每一个数
List<Integer> list2 = Arrays.asList(9, 3,2,6);
Integer reduce = list2.stream().reduce(100, (x1, x2) -> x1 * x2);
System.out.println(reduce);//32400

03.Stream API案例

public class Order {

	private int id;
	private String name;
	private double price;
	private int amount;

	public Order(int id, String name, double price, int amount) {
		this.id = id;
		this.name = name;
		this.price = price;
		this.amount = amount;
	}
	//TODO Getter和Setter方法这里省略
}

数据

List<Order> list = new ArrayList<>();
list.add(new Order(1001, "a", 34,10));
list.add(new Order(1002, "b", 12,5));
list.add(new Order(1003, "cccc", 33,8));
list.add(new Order(1004, "d", 26,6));
list.add(new Order(1005, "eee", 65,0));
list.add(new Order(1006, "fff", 42,6));
list.add(new Order(1007, "gggggg", 26,3));
list.add(new Order(1008, "hhhhh", 35,7));

需求:查看价格大于30的订单,并将订单数据按订单数量从大到小排序

 List<Order> collect = list.stream().filter(x -> x.getPrice() > 30).
 sorted((o1, o2) -> o2.getAmount() - o1.getAmount()).collect(Collectors.toList());
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值