Transaction { public Trader getTrader() { return trader; } Transaction();  Transaction(Trader trader, int year, int value); Trader getTrader(); void setTrader(Trader trader); int getYear(); void setYear(int year); int getValue(); void setValue(int value); @Override String toString();  }
@Test public void test3(){ transactions.stream() .filter((x) -> x.getTrader().getCity().equals("Cambridge")) .map(Transaction::getTrader) .sorted((e1,e2) -> e1.getName().compareTo(e1.getName())) .distinct() .forEach(System.out::println); }
@Test public void test4(){ transactions.stream() .map((t) -> t.getTrader().getName()) .sorted() .forEach(System.out::println); System.out.println("------------------------------------"); String str = transactions.stream() .map((t) -> t.getTrader().getName()) .sorted() .reduce("", String::concat); System.out.println(str); System.out.println("------------------------------------"); transactions.stream() .map((t) -> t.getTrader().getName()) .flatMap(TestTransaction::filterCharacter) .sorted((s1, s2) -> s1.compareToIgnoreCase(s2)) .forEach(System.out::print); }
@Test public void test5(){ boolean milan = transactions.stream() .anyMatch((t) -> t.getTrader().getCity().equals("Milan")); System.out.println(milan); }
@Test public void test6(){ Integer cambridge = transactions.stream() .filter((t) -> t.getTrader().getCity().equals("Cambridge")) .map(Transaction::getValue) .reduce(0, Integer::sum); System.out.println(cambridge); }
@Test public void test7(){ Optional<Integer> cambridge = transactions.stream() .filter((t) -> t.getTrader().getCity().equals("Cambridge")) .map(Transaction::getValue) .max(Integer::compare); System.out.println(cambridge); }
@Test public void test8(){ Optional<Integer> cambridge = transactions.stream() .filter((t) -> t.getTrader().getCity().equals("Cambridge")) .map(Transaction::getValue) .min(Integer::compare); System.out.println(cambridge); }