码迷,mamicode.com
首页 > 编程语言 > 详细

java8 StreamAPI 使用

时间:2017-11-02 14:29:11      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:worker   getname   static   display   any   create   filter   class   img   

技术分享
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by meicai on 2017/10/30.
 */
public class TestJava8_01 {
    public static void main(String[]args){
        test11();
    }

    /**
     * {士兵=[Worker{id=5, name=‘刘封‘, gender=0, age=20, salary=20.56, workType=士兵}], 谋士=[Worker{id=3, name=‘荀彧‘, gender=1, age=40, salary=40.56, workType=谋士}], 领导=[Worker{id=1, name=‘曹操‘, gender=1, age=50, salary=50.56, workType=领导}, Worker{id=4, name=‘刘备‘, gender=1, age=60, salary=60.56, workType=领导}], 武将=[Worker{id=2, name=‘典韦‘, gender=1, age=30, salary=30.56, workType=武将}]}
     ==--------------==
     key:士兵|count:1|[,刘封
     key:谋士|count:1|[,荀彧
     key:领导|count:2|[,曹操,刘备
     key:武将|count:1|[,典韦
     */
    private static void  test11(){
        List<Worker> workerList= Arrays.asList(
                new Worker(1, "曹操", 1, 50, 50.56, WorkType.领导),
                new Worker(2, "典韦", 1, 30, 30.56, WorkType.武将),
                new Worker(3, "荀彧", 1, 40, 40.56, WorkType.谋士),
                new Worker(4, "刘备", 1, 60, 60.56, WorkType.领导),
                new Worker(5, "刘封", 0, 20, 20.56, WorkType.士兵)
        ) ;
        Map<WorkType,List<Worker>> map= workerList.stream().collect(Collectors.groupingBy(i->i.getWorkType()));
        System.out.println(map);
        System.out.println("==--------------==");
        map.keySet().forEach(key->{
            String names=  map.get(key).stream().map(Worker::getName).reduce("[",(x,y)->{return x+","+y;});
            System.out.println("key:"+key+"|count:"+map.get(key).size()+"|"+names);
        });
    }

    /**
     * {士兵=[Worker{id=5, name=‘刘封‘, gender=0, age=20, salary=20.56, workType=士兵}], 谋士=[Worker{id=3, name=‘荀彧‘, gender=1, age=40, salary=40.56, workType=谋士}], 领导=[Worker{id=1, name=‘曹操‘, gender=1, age=50, salary=50.56, workType=领导}, Worker{id=4, name=‘刘备‘, gender=1, age=60, salary=60.56, workType=领导}], 武将=[Worker{id=2, name=‘典韦‘, gender=1, age=30, salary=30.56, workType=武将}]}
     * ==--------------==
     key:士兵|value:[Worker{id=5, name=‘刘封‘, gender=0, age=20, salary=20.56, workType=士兵}]
     Worker{id=5, name=‘刘封‘, gender=0, age=20, salary=20.56, workType=士兵}
     key:谋士|value:[Worker{id=3, name=‘荀彧‘, gender=1, age=40, salary=40.56, workType=谋士}]
     Worker{id=3, name=‘荀彧‘, gender=1, age=40, salary=40.56, workType=谋士}
     key:领导|value:[Worker{id=1, name=‘曹操‘, gender=1, age=50, salary=50.56, workType=领导}, Worker{id=4, name=‘刘备‘, gender=1, age=60, salary=60.56, workType=领导}]
     Worker{id=1, name=‘曹操‘, gender=1, age=50, salary=50.56, workType=领导}
     Worker{id=4, name=‘刘备‘, gender=1, age=60, salary=60.56, workType=领导}
     key:武将|value:[Worker{id=2, name=‘典韦‘, gender=1, age=30, salary=30.56, workType=武将}]
     Worker{id=2, name=‘典韦‘, gender=1, age=30, salary=30.56, workType=武将}
     */
    private static void  test10(){
        List<Worker> workerList= Arrays.asList(
                new Worker(1, "曹操", 1, 50, 50.56, WorkType.领导),
                new Worker(2, "典韦", 1, 30, 30.56, WorkType.武将),
                new Worker(3, "荀彧", 1, 40, 40.56, WorkType.谋士),
                new Worker(4, "刘备", 1, 60, 60.56, WorkType.领导),
                new Worker(5, "刘封", 0, 20, 20.56, WorkType.士兵)
        ) ;
        Map<WorkType,List<Worker>> map= workerList.stream().collect(Collectors.groupingBy(Worker::getWorkType));
        System.out.println(map);
        System.out.println("==--------------==");
        map.keySet().forEach(key->{
            System.out.println("key:"+key+"|value:"+map.get(key));
            List<Worker> workerListCurrent=map.get(key);
            workerListCurrent.forEach(System.out::println);
        });
    }

    /**
     * 曹操
     典韦
     荀彧
     刘备
     */
    private static void  test9(){
        List<Worker> workerList= Arrays.asList(
                new Worker(1, "曹操", 1, 50, 50.56, WorkType.领导),
                new Worker(2, "典韦", 1, 30, 30.56, WorkType.武将),
                new Worker(3, "荀彧", 1, 40, 40.56, WorkType.谋士),
                new Worker(4, "刘备", 1, 60, 60.56, WorkType.领导),
                new Worker(5, "刘备", 0, 20, 20.56, WorkType.士兵)
        ) ;
        Stream<String> stream=  workerList.stream().map(Worker::getName).distinct();
        List<String> nameList= stream.collect(Collectors.toList());
        nameList.forEach(System.out::println);
    }

    /**
     * 无序+去重
     *典韦
     刘备
     荀彧
     曹操
     */
    private static void  test8(){
        List<Worker> workerList= Arrays.asList(
                new Worker(1, "曹操", 1, 50, 50.56, WorkType.领导),
                new Worker(2, "典韦", 1, 30, 30.56, WorkType.武将),
                new Worker(3, "荀彧", 1, 40, 40.56, WorkType.谋士),
                new Worker(4, "刘备", 1, 60, 60.56, WorkType.领导),
                new Worker(5, "刘备", 0, 20, 20.56, WorkType.士兵)
        ) ;
        Stream<String> stream=  workerList.stream().map(i->i.getName());
        Set<String> nameList= stream.collect(Collectors.toSet());
        nameList.forEach(System.out::println);

    }

    /**
     * 曹操
     典韦
     荀彧
     刘备
     刘备
     */
    private static void  test7(){
        List<Worker> workerList= Arrays.asList(
                new Worker(1, "曹操", 1, 50, 50.56, WorkType.领导),
                new Worker(2, "典韦", 1, 30, 30.56, WorkType.武将),
                new Worker(3, "荀彧", 1, 40, 40.56, WorkType.谋士),
                new Worker(4, "刘备", 1, 60, 60.56, WorkType.领导),
                new Worker(5, "刘备", 0, 20, 20.56, WorkType.士兵)
        ) ;
        Stream<String> stream=  workerList.stream().map(i->i.getName());
        List<String> nameList= stream.collect(Collectors.toList());
        nameList.forEach(System.out::println);

    }

    /**
     * true
     */
    private static void  test6(){
        List<Worker> workerList= Arrays.asList(
                new Worker(1, "曹操", 1, 50, 50.56, WorkType.领导),
                new Worker(1, "典韦", 1, 30, 30.56, WorkType.武将),
                new Worker(1, "荀彧", 1, 40, 40.56, WorkType.谋士),
                new Worker(1, "刘备", 1, 60, 60.56, WorkType.领导)
        ) ;
        boolean bEixst=  workerList.stream().anyMatch(i->i.getName().equals("荀彧"));
        System.out.println(bEixst);

    }

    /**
     * Worker{id=1, name=‘曹操‘, gender=1, age=50, salary=50.56, workType=领导}
     */
    private static void  test5(){
        List<Worker> workerList= Arrays.asList(
                new Worker(1, "曹操", 1, 50, 50.56, WorkType.领导),
                new Worker(1, "典韦", 1, 30, 30.56, WorkType.武将),
                new Worker(1, "荀彧", 1, 40, 40.56, WorkType.谋士),
                new Worker(1, "刘备", 1, 60, 60.56, WorkType.领导)
        ) ;
        Optional<Worker> opWorker=  workerList.stream().findAny();
        if(opWorker.isPresent()){
            System.out.println(opWorker.get());
        }
        else {
            System.out.println("没找到");
        }

    }

    /**
     * Worker{id=1, name=‘典韦‘, gender=1, age=30, salary=30.56, workType=武将}
     */
    private static void  test4(){
        List<Worker> workerList= Arrays.asList(
                new Worker(1, "曹操", 1, 50, 50.56, WorkType.领导),
                new Worker(1, "典韦", 1, 30, 30.56, WorkType.武将),
                new Worker(1, "荀彧", 1, 40, 40.56, WorkType.谋士),
                new Worker(1, "刘备", 1, 60, 60.56, WorkType.领导)
        ) ;
        Optional<Worker> opWorker=  workerList.stream().filter(i->i.getWorkType()==WorkType.士兵).findFirst();
        if(opWorker.isPresent()){
            System.out.println(opWorker.get());
        }
        else {
            System.out.println("没找到");
        }

    }

    /**
     *Worker{id=1, name=‘曹操‘, gender=1, age=50, salary=50.56, workType=领导}
     */
    private static void  test3(){
        List<Worker> workerList= Arrays.asList(
                new Worker(1, "曹操", 1, 50, 50.56, WorkType.领导),
                new Worker(1, "典韦", 1, 30, 30.56, WorkType.武将),
                new Worker(1, "荀彧", 1, 40, 40.56, WorkType.谋士),
                new Worker(1, "刘备", 1, 60, 60.56, WorkType.领导)
        ) ;
        Optional<Worker> opWorker=  workerList.stream().filter(i->i.getWorkType()==WorkType.领导).findFirst();
        System.out.println(opWorker.get());
    }

    /**
     * Worker{id=1, name=‘典韦‘, gender=1, age=30, salary=30.56, workType=武将}
     */
    private static void  test2(){
        List<Worker> workerList= Arrays.asList(
                new Worker(1, "曹操", 1, 50, 50.56, WorkType.领导),
                new Worker(1, "典韦", 1, 30, 30.56, WorkType.武将),
                new Worker(1, "荀彧", 1, 40, 40.56, WorkType.谋士),
                new Worker(1, "刘备", 1, 60, 60.56, WorkType.领导)
        ) ;
        Stream<Worker> stream=  workerList.stream().filter(i->i.getWorkType().equals(WorkType.武将));
        stream.forEach(System.out::println);
    }

    /**
     * Worker{id=1, name=‘曹操‘, gender=1, age=50, salary=50.56, workType=领导}
     Worker{id=1, name=‘刘备‘, gender=1, age=60, salary=60.56, workType=领导}
     */
    private static void  test1(){
        List<Worker> workerList= Arrays.asList(
                new Worker(1, "曹操", 1, 50, 50.56, WorkType.领导),
                new Worker(1, "典韦", 1, 30, 30.56, WorkType.武将),
                new Worker(1, "荀彧", 1, 40, 40.56, WorkType.谋士),
                new Worker(1, "刘备", 1, 60, 60.56, WorkType.领导)
        ) ;
        Stream<Worker> stream=  workerList.stream().filter(i->i.getWorkType()==WorkType.领导);
        stream.forEach(i->System.out.println(i));
    }
}
Stream

 

java8 StreamAPI 使用

标签:worker   getname   static   display   any   create   filter   class   img   

原文地址:http://www.cnblogs.com/caohuimingfa/p/7771830.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!