This is a comprehensive exercise of stream stream collection. Before, I also wrote some common methods, such as conveyor belt: Java 8 set operation easy to use in work , this article is a supplement, more specific and structural, for everyone to learn, code can run, code can run, code can run!!! In addition, for the relevant usage of functional interface, please refer to: New features of Java 8 - functional interface
/** * It is also a common skill in development to practice the integrated practice of the flow operation of the set, which makes good use of half the effort with twice the effort * It's similar to the operation of sql statement * 1. Find out all transaction lines occurred in 2011 and sort by transaction amount * 2. Which cities have traders worked in * 3. Find all traders from Cambridge and sort by name * 4. Do traders work in Milan * 5. Print the total of all trades of all traders in Cambridge * 6. What is the smallest of all transactions * 7. What is the highest of all transactions * 8. Find the smallest transaction */ private List<Transaction> list; @Before public void init() { Trader raloul = new Trader("Raoul", "Cambridge"); Trader mario = new Trader("Mario", "Milan"); Trader alan = new Trader("Alan", "Cambridge"); Trader brian = new Trader("Brian", "Cambridge"); list=Arrays.asList( new Transaction(brian, 2011, 300), new Transaction(raloul, 2012, 1000), new Transaction(raloul, 2011, 400), new Transaction(mario, 2012, 710), new Transaction(mario, 2012, 700), new Transaction(alan, 2012, 950) ); } // Find out all transaction lines occurred in 2011 and sort by transaction amount @Test public void transaction1() { list.stream() .filter((e)->e.getYear()==2011) .sorted((t1,t2)->Integer.compare(t1.getDeal(), t2.getDeal())) .forEach(System.out::println); } // Which cities have traders worked in @Test public void transaction2() { list.stream() .map(t->t.getTrader().getAddress()) .distinct() .forEach(System.out::println); } // Find all traders from Cambridge and sort by name @Test public void transaction3() { list.stream() .filter(t->t.getTrader().getAddress().equals("Cambridge")) .map(t->t.getTrader().getName()) .sorted() .forEach(System.out::println); } //Do traders work in Milan @Test public void transaction4() { boolean b = list.stream() .anyMatch(t->t.getTrader().getAddress().equals("Milan")); } //Print the total of all trades of all traders in Cambridge @Test public void transaction5() { Optional<Integer> reduce = list.stream() .filter(t->t.getTrader().getAddress().equals("Cambridge")) .map(Transaction::getDeal) .reduce(Integer::sum); System.out.println(reduce.get()); } //What is the smallest of all transactions @Test public void transaction6() { list.stream() .map(t->t.getDeal()) .sorted() .limit(1) .forEach(System.out::println); } //What is the highest of all transactions @Test public void transaction7() { Optional<Integer> max = list.stream() .map(t->t.getDeal()) .max(Integer::compare); System.out.println(max.get()); } //Find the smallest transaction @Test public void transaction8() { Optional<Transaction> min = list.stream() .min((t1,t2)->Integer.compare(t1.getDeal(), t2.getDeal())); System.out.println(min.get()); }