亲宝软件园·资讯

展开

Java8 Stream API 熟练掌握Java8新特性之Stream API的全面应用

张起灵-小哥 人气:0
想了解熟练掌握Java8新特性之Stream API的全面应用的相关内容吗,张起灵-小哥在本文为您仔细讲解Java8 Stream API的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java8,Stream,API,Java8,新特性,下面大家一起来学习吧。

1.写在前面

关于Stream API的内容,已经基本上说完了。大家可以参考我的这两篇文章:

深入理解Java8新特性之Stream API的创建方式和中间操作步骤深入理解Java8新特性之Stream API的终止操作步骤

那么这篇文章主要就是说一个Stream API的简单应用练习题。

2.应用举例

首先,我们需要创建一个交易员类、交易类、存储这两个类对象的List集合。

package com.szh.java8.exer;
 
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
 
/**
 * 交易员类
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Trader {
 
	private String name;
	private String city;
 
}
package com.szh.java8.exer;
 
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
 
/**
 * 交易类
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Transaction {
 
	private Trader trader;
	private int year;
	private int value;
 
}
    List<Transaction> transactions = Arrays.asList(
            new Transaction(new Trader("Brian", "Cambridge"),2011,300),
            new Transaction(new Trader("Raoul", "Cambridge"),2012,1000),
            new Transaction(new Trader("Raoul", "Cambridge"),2011, 400),
            new Transaction(new Trader("Mario", "Milan"),2012, 710),
            new Transaction(new Trader("Mario", "Milan"),2012, 700),
            new Transaction(new Trader("Alan", "Cambridge"),2012,950)
    );
    @Test
    public void test1() {
        transactions.stream()
                .filter((t) -> t.getYear() == 2011)
                .sorted((t1,t2) -> Integer.compare(t1.getValue(),t2.getValue()))
                .forEach(System.out::println);
    }

    @Test
    public void test2() {
        transactions.stream()
                .map((t) -> t.getTrader().getCity())
                .distinct()
                .forEach(System.out::println);
    }

    @Test
    public void test3() {
        transactions.stream()
                .filter((t) -> t.getTrader().getCity().equals("Cambridge"))
                .map(Transaction::getTrader)
                .sorted((t1,t2) -> t1.getName().compareTo(t2.getName()))
                .distinct()
                .forEach(System.out::println);
    }

    @Test
    public void test4() {
        transactions.stream()
                .map((t) -> t.getTrader().getName())
                .sorted()
                .forEach(System.out::println);
    }
    @Test
    public void test5() {
        boolean b = transactions.stream()
                .anyMatch((t) -> t.getTrader().getCity().equals("Milan"));
        System.out.println(b);
    }

    @Test
    public void test6() {
        Optional<Integer> sum = transactions.stream()
                .filter((t) -> t.getTrader().getCity().equals("Cambridge"))
                .map(Transaction::getValue)
                .reduce(Integer::sum);
        System.out.println(sum.get());
//        Integer reduce = transactions.stream()
//                .filter((t) -> t.getTrader().getCity().equals("Cambridge"))
//                .map(Transaction::getValue)
//                .reduce(0, (x, y) -> x + y);
//        System.out.println(reduce);
    }

    @Test
    public void test7() {
        Optional<Integer> max = transactions.stream()
                .map((t) -> t.getValue())
                .max(Integer::compare);
        System.out.println(max.get());
    }

    @Test
    public void test8() {
        Optional<Transaction> min = transactions.stream()
                .min((t1, t2) -> Integer.compare(t1.getValue(), t2.getValue()));
        System.out.println(min.get());
    }

加载全部内容

相关教程
猜你喜欢
用户评论