美文网首页
Java 从入门到放弃 Java8实战

Java 从入门到放弃 Java8实战

作者: Cocoonshu粽子 | 来源:发表于2017-11-27 21:27 被阅读34次

开门见山

我有以下条件,后面等着8个问题,请大家思考后,写出代码~

交易员
public class Trader {
    private final String name;
    private final String city;
    public Trader(String n,String c){
        this.name = n;
        this.city = c;
    }

    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;

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

交易员和交易数据
   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)
                );
问题
  1. 找出2011年发生的所有交易,并按交易额排序(从低到高);
  2. 交易员都在哪些不同的城市工作过;
  3. 查找所有来自于剑桥的交易员,并按姓名排序;
  4. 返回所有交易员的姓名字符串,按字母排序;
  5. 有没有交易员是在米兰工作的;
  6. 打印生活在剑桥的交易员的交易额;
  7. 所有交易中最高的交易额是多少;
  8. 找到交易额最小的交易;
答案
 /**
     * 找出2011年发生的所有交易,并按交易额排序(从低到高);
     * @param transactions
     * @return
     */
    private static List<Transaction> questionOne( List<Transaction> transactions){
        return transactions.stream().filter(transaction -> transaction.getYear()==2011).sorted(Comparator.comparing(Transaction::getValue)).collect(Collectors.toList());
    }

    /**
     *  交易员都在哪些不同的城市工作过;
     * @param transactions
     * @return
     */
    private static List<String> questionTwo(List<Transaction> transactions){
        return transactions.stream().map(transaction -> transaction.getTrader().getCity()).distinct().collect(Collectors.toList());
    }

    /**
     * 查找所有来自于剑桥的交易员,并按姓名排序;
     * @param transactions
     */
    private static List<Trader> questionThree(List<Transaction> transactions){
        String cityStr = "Cambridge";
        return transactions.stream().map(Transaction::getTrader).filter(trader->trader.getCity().equals(cityStr)).distinct().sorted(Comparator.comparing(Trader::getName)).collect(Collectors.toList());
    }

    /**
     * 返回所有交易员的姓名字符串,按字母排序;
     * @param transactions
     * @return
     */
    private static String questionFour(List<Transaction> transactions){
        return transactions.stream().map(transaction->transaction.getTrader().getName()).distinct().sorted().reduce("",(n1,n2)->n1+n2);
    }

  /**
     * 有没有交易员是在米兰工作的;
     * @param transactions
     * @return
     */
    private static boolean questionFive(List<Transaction> transactions){
        String cityStr = "Milan";
        return transactions.stream().anyMatch(transaction -> transaction.getTrader().getCity().equals(cityStr));
    }

    /**
     * 打印生活在剑桥的交易员的交易额;
     * @param transactions
     */
    private static void questionSix(List<Transaction> transactions){
        String cityStr = "Cambridge";
        transactions.stream().filter(transaction -> transaction.getTrader().getCity().equals(cityStr)).map(Transaction::getValue).forEach(System.out::println);
    }

    /**
     * 所有交易中最高的交易额是多少;
     * @param transactions
     * @return
     */
    private static Optional<Integer> questionSeven(List<Transaction> transactions){
        return transactions.stream().map(Transaction::getValue).reduce(Integer::max);
    }

    /**
     *  找到交易额最小的交易;
     * @param transactions
     * @return
     */
    private static Optional<Transaction> questionEight(List<Transaction> transactions){
        return transactions.stream().min(Comparator.comparing(Transaction::getValue));
    }

相关文章

网友评论

      本文标题:Java 从入门到放弃 Java8实战

      本文链接:https://www.haomeiwen.com/subject/maxcbxtx.html