流的使用练习


/**
 * 交易员
 */
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;

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

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

public class Test {
    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<Trader> traders = Arrays.asList(raoul,mario,alan,brian);
        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)
        );

        //(1) 找出2011年发生的所有交易,并按交易额排序(从低到高)。
        System.out.println("(1)");
        List<Transaction> tr2011 = transactions.stream()
                .filter(transaction -> transaction.getYear() == 2011)
                .sorted(comparing(Transaction::getValue))
                .collect(toList());
        System.out.println(tr2011);
        //(2) 交易员都在哪些不同的城市工作过?
        System.out.println("(2)");
        transactions.stream()
                .map(t->t.getTrader().getCity())
                .distinct()
                .collect(toList());
        //或
        transactions.stream()
                .map(t->t.getTrader().getCity())
                .collect(toSet());
        //或
        Set<String> collect = traders.stream().map(Trader::getCity).collect(toSet());
        System.out.println(collect);
        //(3) 查找所有来自于剑桥的交易员,并按姓名排序。
        System.out.println("(3)");
        List<Trader> traderList = traders.stream().filter(t -> "Cambridge".equals(t.getCity()))
                .distinct()
                .sorted(comparing(Trader::getName))
                .collect(toList());
        System.out.println(traderList);
        //(4) 返回所有交易员的姓名字符串,按字母顺序排序。
        System.out.println("(4)");
        String traderStr = traders.stream()
                .map(Trader::getName)
                .distinct()
                .sorted()
                .reduce("",(n1,n2)->n1+n2+" ");
        System.out.println(traderStr);
        String traderStr2 = traders.stream()
                .map(Trader::getName)
                .distinct()
                .sorted()
                .collect(Collectors.joining(" "));
        System.out.println(traderStr2);
        //(5) 有没有交易员是在米兰工作的?
        System.out.println("(5)");
        boolean milanBased = traders.stream()
                .anyMatch(t -> "Milan".equals(t.getCity()));
        //(6) 打印生活在剑桥的交易员的所有交易额。
        System.out.println("(6)");
        transactions.stream()
                .filter(t->"Cambridge".equals(t.getTrader().getCity()))
                .map(Transaction::getValue)
                .forEach(System.out::println);
        //(7) 所有交易中,最高的交易额是多少?
        System.out.println("(7)");
        int maxValue = transactions.stream()
                .max(comparing(Transaction::getValue))
                .get().getValue();
        System.out.println(maxValue);
        //或
        Integer maxValue2 = transactions.stream()
                .map(Transaction::getValue)
                .reduce(Integer::max)
                .get();
        System.out.println(maxValue2);
        //(8) 找到交易额最小的交易。
        System.out.println("(8)");
        Integer minValue = transactions.stream()
                .map(Transaction::getValue)
                .reduce(Integer::min)
                .get();
        System.out.println(minValue);
        Integer minValue2 = transactions.stream()
                .map(Transaction::getValue)
                .reduce((v1,v2)->v1>v2?v2:v1)
                .get();
        System.out.println(minValue2);
        int minValue3 = transactions.stream()
                .min(comparing(Transaction::getValue))
                .get()
                .getValue();
        System.out.println(minValue3);
    }
}
posted @ 2020-04-06 17:05  fly_bk  阅读(125)  评论(0编辑  收藏  举报