java8 Streaming Exercise-2 (Adapted from: java8 combat book)

Posted May 27, 20203 min read

package com.niewj.sbdemo.java8;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author weijun.nie
 * @Date 2020/5/26 10:35
 * @Version 1.0
 */
@Slf4j
public class TranTestDemo {
    public static void main(String[]args) {
        Trader niuj = new Trader("nj  ", "  ");
        Trader maik = new Trader("mk  ", "  ");
        Trader niuf = new Trader("nf  ", "  ");
        Trader jiny = new Trader("jy  ", "  ");
        List<Transaction> transactions = Arrays.asList(
                new Transaction(jiny, 2011, 300),
                new Transaction(niuj, 2012, 1000),
                new Transaction(niuj, 2011, 400),
                new Transaction(maik, 2012, 710),
                new Transaction(maik, 2012, 700),
                new Transaction(niuf, 2012, 950)
       );

        //(1)   2011                       
        //(1-1).   
        log.info("(1)(1-1).    1:");
        transactions.stream().filter(t -> 2011 == t.getYear()).sorted((t1, t2) -> Integer.valueOf(t1.getValue()).compareTo(Integer.valueOf(t2.getValue()))).forEach(System.out::println);
        log.info("(1)(1-2).    2:");
        transactions.stream().filter(t -> 2011 == t.getYear()).sorted(Comparator.comparingInt(Transaction::getValue)).forEach(System.out::println);
        log.info("(1)(1-3).    3:");
        transactions.stream().filter(t -> 2011 == t.getYear()).sorted(Comparator.comparing(Transaction::getValue)).forEach(System.out::println);

        log.info("(1)(1-4).    :");
        //(1-2).   List<Transaction>
        List<Transaction> result1List = transactions.stream().filter(t -> 2011 == t.getYear()).sorted(Comparator.comparingInt(Transaction::getValue)).collect(Collectors.toList());
        log.info("(1)(1-4).  2011                      :{}", result1List);

        //(2)                 
        List<String> cities = transactions.stream().map(t -> t.getTrader().getCity()).distinct().collect(Collectors.toList());
        log.info("(2).               :{}", cities);

        //(3)                      
        List<Trader> SHTradersSorted = transactions.stream().map(Transaction::getTrader).filter(t -> t.getCity().equals("  ")).sorted(Comparator.comparing(Trader::getName)).collect(Collectors.toList());
        log.info("(3).                    :{}", SHTradersSorted);

        //(4)                       
        List<String> traderNamesList = transactions.stream().map(t -> t.getTrader().getName()).distinct().sorted().collect(Collectors.toList());
        log.info("(4).                     :{}", traderNamesList);

        //(5)               
        boolean hasInBeijing = transactions.stream().filter(t -> t.getTrader().getCity().equals("  ")).findAny().isPresent();
        log.info("(5).             :{}", hasInBeijing);

        //(6)                   
        List<Integer> valuesInShanghai = transactions.stream().filter(t -> t.getTrader().getCity().equals("  ")).map(t -> t.getValue()).collect(Collectors.toList());
        log.info("(6).                 :{}", valuesInShanghai);

        //(7)                 
        Optional<Integer> max = transactions.stream().map(t -> t.getValue()).max(Comparator.comparingInt(Integer::intValue));
        log.info("(7).                {}", max.get());

        //(8)            
        Transaction minTransanction = transactions.stream().collect(Collectors.minBy(Comparator.comparing(t -> t.getValue()))).get();
        log.info("(8).           :{}", minTransanction);
    }
}

(1)(1-1). 1:
{Trader{name='jy ', city=' '}, year:2011, value:300}
{Trader{name='nj ', city=' '}, year:2011, value:400}
(1)(1-2). 2:
{Trader{name='jy ', city=' '}, year:2011, value:300}
{Trader{name='nj ', city=' '}, year:2011, value:400}
(1)(1-3). 3:
{Trader{name='jy ', city=' '}, year:2011, value:300}
{Trader{name='nj ', city=' '}, year:2011, value:400}
(1)(1-4). :
(1)(1-4). 2011 :[{Trader{name='jy ', city=' '}, year:2011, value:300}, {Trader{name='nj ', city=' '}, year:2011, value:400}]
(2). :[ , ]
(3). :[Trader{name='jy ', city=' '}, Trader{name='nf ', city=' '}, Trader{name='nj ', city=' '}, Trader{name='nj ', city=' '}]
(4). :[jy , mk , nf , nj ]
(5). :false
(6). :[300, 1000, 400, 950]
(7). 1000
(8). :{Trader{name='jy ', city=' '}, year:2011, value:300}