|
| 1 | +/** |
| 2 | + * |
| 3 | + */ |
| 4 | +package com.java8in.action.ch5; |
| 5 | + |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.Comparator; |
| 8 | +import java.util.List; |
| 9 | +import java.util.stream.Collectors; |
| 10 | + |
| 11 | +/** |
| 12 | + * @author Sanket Gupta |
| 13 | + * |
| 14 | + */ |
| 15 | +public class TestClass { |
| 16 | + |
| 17 | +/** |
| 18 | + * @param args |
| 19 | + */ |
| 20 | +public static void main(String[] args) { |
| 21 | +Trader raoul = new Trader("Raoul", "Cambridge"); |
| 22 | +Trader mario = new Trader("Mario", "Milan"); |
| 23 | +Trader alan = new Trader("Alan", "Cambridge"); |
| 24 | +Trader brain = new Trader("Brain", "Cambridge"); |
| 25 | + |
| 26 | +List<Transaction> transactions = Arrays.asList( |
| 27 | +new Transaction(brain, 2011, 300), |
| 28 | +new Transaction(raoul, 2012, 1000), |
| 29 | +new Transaction(raoul, 2011, 400), |
| 30 | +new Transaction(mario, 2012, 710), |
| 31 | +new Transaction(mario, 2012, 700), |
| 32 | +new Transaction(alan, 2012, 950)); |
| 33 | + |
| 34 | + |
| 35 | +// 1. Find all transactions in the year 2011 and sort them by value (small to high). |
| 36 | +System.out.println("Answer 1:"); |
| 37 | +transactions.stream().filter( t->t.getYear()==2011).sorted((t1,t2)->Integer.compare(t1.getValue(), t2.getValue())).forEach(t3-> System.out.println(t3)); |
| 38 | +transactions.stream().filter( t->t.getYear()==2011).sorted(Comparator.comparing(Transaction::getValue)).forEach(t3-> System.out.println(t3));//.collect(Collectors.toList()); |
| 39 | + |
| 40 | +// 2. What are all the unique cities where the traders work? |
| 41 | +System.out.println("Answer 2:"); |
| 42 | +transactions.stream().map(t->t.getTrader().getCity()).distinct().forEach(c->System.out.println(c)); |
| 43 | + |
| 44 | +// 3. Find all traders from Cambridge and sort them by name. |
| 45 | +System.out.println("Answer 3:"); |
| 46 | +transactions.stream().filter(t->t.getTrader().getCity()=="Cambridge").map(t->t.getTrader()).sorted(Comparator.comparing(Trader::getName)).distinct().forEach(trader->System.out.println(trader));//.collect(Collectors.toList()); |
| 47 | + |
| 48 | +// 4. Return a string of all traders's names sorted alphabetically. |
| 49 | +System.out.println("Answer 4:"); |
| 50 | +// String names=transactions.stream().map(tx->tx.getTrader().getName()).distinct().sorted().reduce("",(n1,n2)->n1+n2); |
| 51 | +String names=transactions.stream().map(tx->tx.getTrader().getName()).distinct().sorted().collect(Collectors.joining()); |
| 52 | +System.out.println(names); |
| 53 | +// transactions.stream().map(tx->tx.getTrader().getName()).sorted().collect(joining()); |
| 54 | + |
| 55 | +// 5. Are any traders based in Milan? |
| 56 | +System.out.println("Answer 5:"); |
| 57 | +Boolean isMialBased=transactions.stream().anyMatch(transaction -> transaction.getTrader().getCity().equals("Milan")); |
| 58 | +System.out.println(isMialBased); |
| 59 | + |
| 60 | +// 6. Print all transactions' values from the traders living in Cambridge. |
| 61 | +System.out.println("Answer 6:"); |
| 62 | +transactions.stream().filter(t->t.getTrader().getCity()=="Cambridge").map(Transaction::getValue).distinct().forEach(tradeValue->System.out.println(tradeValue)); |
| 63 | + |
| 64 | +// 7. What's the highest value of all the transactions? |
| 65 | +System.out.println("Answer 7:"); |
| 66 | +System.out.println(transactions.stream().map(tx->tx.getValue()).max(Comparator.comparing(Integer::intValue))); |
| 67 | +// 8. Find the transaction with the smallest value. |
| 68 | +System.out.println("Answer 8:"); |
| 69 | +System.out.println(transactions.stream().min(Comparator.comparing(Transaction::getValue))); |
| 70 | +} |
| 71 | + |
| 72 | +} |
0 commit comments