Java 8-Stream API-练习

交易员类

public class Trader {
    private final String name;
    private final String city;


    public Trader(String name, String city) {
        this.name = name;
        this.city = city;
    }

    public String getName() {
        return name;
    }


    public String getCity() {
        return city;
    }


    @Override
    public String toString() {
        return "Trader{" +
                "name='" + name + '\'' +
                ", city='" + city + '\'' +
                '}';
    }
}

交易类

public class Transaction {

    private final Trader trader;
    private final int year;
    private final int value;

    private Currency currency;

    public Currency getCurrency() {
        return currency;
    }

    public void setCurrency(Currency currency) {
        this.currency = currency;
    }

    public Transaction(Trader trader, int year, int value) {
        this.trader = trader;
        this.year = year;
        this.value = value;
    }

    public Trader getTrader() {
        return trader;
    }


    public int getYear() {
        return year;
    }


    public int getValue() {
        return value;
    }


    @Override
    public String toString() {
        return "Transaction{" +
                "trader=" + trader +
                ", year=" + year +
                ", value=" + value +
                '}';
    }
}

测试类

public class TestTransaction {

    List<Transaction> transactions = null;

    @Before
    public void before() {
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");

        transactions = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(mario, 2012, 950)
        );
    }


    @Test
    public void test1() {
        //找出2011年发生的所有交易,并按交易排序(从低到高)
        List<Transaction> tr2011 = transactions.stream()
                .filter((t) -> t.getYear() == 2011)
                .sorted(comparingInt(Transaction::getValue))
                .collect(toList());

    }

    @Test
    public void test2() {
        //交易员都在哪些不同的城市工作过
        List<String> cities = transactions.stream()
                .map((transaction) -> transaction.getTrader().getCity())
                .distinct()
                .collect(toList());

        Set<String> cities2 = transactions.stream()
                .map(transaction -> transaction.getTrader().getCity())
                .collect(toSet());
    }


    @Test
    public void test3() {
        //查找所有来自剑桥的交易员,并按姓名排序
        List<Trader> traders = transactions.stream()
                .map(Transaction::getTrader)
                .filter((trader) -> trader.getCity().equals("Cambridge"))
                .sorted(comparing(Trader::getName))
                .collect(toList());
    }


    @Test
    public void test4() {
        //返回所有交易员的姓名字符串,按字母顺序排序。
        String traderStr = transactions.stream()
                .map((transaction) -> transaction.getTrader().getName())
                .distinct()
                .sorted()
                .reduce("", (n1, n2) -> n1 + n2);

        /**
         * 上面的方法效率较低(所有字符串都被反复连接,每次迭代的时候都要建立一个新的String对象)
         * 可以使用join方法(其内部会用到StringBuilder)
         */
        String traderStr2=transactions.stream()
                .map((transaction) -> transaction.getTrader().getName())
                .distinct()
                .sorted()
                .collect(joining());
    }

    @Test
    public void test5() {
        //有没有交易员是在米兰工作的
        boolean milanBased =
                transactions.stream()
                        .anyMatch((transaction) -> "Milan".equals(transaction.getTrader().getCity()));

        System.out.println(milanBased);
    }

    @Test
    public void test6() {
        //打印生活在剑桥的交易员的所有交易额
        transactions.stream()
                .filter(t -> "Cambridge".equals(t.getTrader().getCity()))
                .map(Transaction::getValue)
                .forEach(System.out::println);

    }

    @Test
    public void test7() {
        //所有交易中,最高的交易额是多少
        Optional<Integer> highestValue = transactions.stream()
                        .map(Transaction::getValue)
                        .reduce(Integer::max);

    }

    @Test
    public void test8() {
        //找到交易额最小的交易
        Optional<Transaction> smallestTransaction=transactions.stream()
                .reduce((t1,t2)->
                        t1.getValue()<t2.getValue()?t1:t2);

        Optional<Transaction> smallestTransaction2=transactions.stream()
                .min(comparing(Transaction::getValue));
    }


}

猜你喜欢

转载自blog.csdn.net/zsx157326/article/details/80904543
今日推荐