Java 8 In Action 代码示例(四)

说明

内容来自《Java 8 In Action》


综合练习

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:" + this.name + " in " + this.city;
    }
}


public class Transaction {

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

    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;
    }

    public String toString() {
        return "{" + this.trader + ", " +
                "year: " + this.year + ", " +
                "value:" + this.value + "}";
    }
}


import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class TransactionMain {

    public static void main(String[] args) {
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");

        List<Transaction> 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(alan, 2012, 950)
        );

        List<Trader> traders = Arrays.asList(raoul, mario, alan, brian);

        task1(transactions);
        task2(transactions);
        task3(traders);
        task4(traders);
        task5(traders);
        task6(transactions);
        task7(transactions);
        task8(transactions);
    }

    public static void task1(List<Transaction> transactions) {
        System.out.println("问题:找出2011年发生的所有交易,并按交易额排序(从低到高)");
        transactions.stream()
                .filter(transaction -> transaction.getYear() == 2011)
                .sorted(Comparator.comparing(Transaction::getValue))
                .forEach(System.out::println);
    }

    public static void task2(List<Transaction> transactions) {
        System.out.println("问题:交易员都在哪些不同的城市工作过?");
        Map<String, List<Transaction>> map = transactions.stream()
                .collect(Collectors.groupingBy(transaction -> transaction.getTrader().getCity()));
        System.out.println(map);
    }

    public static void task3(List<Trader> traders) {
        System.out.println("问题:查找所有来自于剑桥的交易员,并按姓名排序");
        traders.stream()
                .filter(trader -> "Cambridge".equals(trader.getCity()))
                .sorted(Comparator.comparing(Trader::getName))
                .forEach(trader -> System.out.println(trader));
    }

    public static void task4(List<Trader> traders) {
        System.out.println("问题:返回所有交易员的姓名字符串,按字母顺序排序");
        String s = traders.stream()
                .map(Trader::getName)
                .distinct()
                .sorted()
                .collect(Collectors.joining());
        System.out.println(s);
        //.reduce(new Trader("", ""), (t1, t2)->new Trader(t1.getName()+t2.getName(), ""))    }

    public static void task5(List<Trader> traders) {
        System.out.println("问题:有没有交易员是在米兰工作的?");
        traders.stream()
                .filter(trader -> "Milan".equals(trader.getCity()))
                .findAny()
                .ifPresent(System.out::println);
    }

    public static void task6(List<Transaction> transactions) {
        System.out.println("问题:打印生活在剑桥的交易员的所有交易额");
        transactions.stream()
                .filter(transaction -> "Cambridge".equals(transaction.getTrader().getCity()))
                .map(Transaction::getValue)
                .forEach(v -> System.out.println(v));
    }

    public static void task7(List<Transaction> transactions) {
        System.out.println("问题:所有交易中,最高的交易额是多少?");
        transactions.stream()
                .map(Transaction::getValue)
                .reduce(Integer::max)
                .ifPresent(System.out::println);
    }

    public static void task8(List<Transaction> transactions) {
        System.out.println("问题:找到交易额最小的交易");
        transactions.stream()
                .reduce((t1, t2) -> t1.getValue() < t2.getValue() ? t1 : t2)
                .ifPresent(System.out::println);
    }
}



  

展开阅读全文