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 test2(){ transactions.stream() .map((t) -> t.getTrader().getCity()) .distinct() .forEach(System.out::println); }
@Test public void test3(){ transactions.stream() .filter((t) -> t.getTrader().getCity().equals("Cambridge")) .map(Transaction::getTrader) .sorted((t1, t2) -> t1.getName().compareTo(t2.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 bl = transactions.stream() .anyMatch((t) -> t.getTrader().getCity().equals("Milan")); System.out.println(bl); }
@Test public void test6(){ Optional<Integer> sum = transactions.stream() .filter((e) -> e.getTrader().getCity().equals("Cambridge")) .map(Transaction::getValue) .reduce(Integer::sum); System.out.println(sum.get()); }