Java8新特性與Java7區別的運用例子

創建一個實體類

/**
 * 項目名  
 * Created by fu.
 * Created at 2019/7/15
 * 描述:
 */
public class PersonModel {
    String name;
    int age;
    String sex;

    public PersonModel(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public String getName() {

        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

方法運用


import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 項目名
 * Created by fu.
 * Created at 2019/7/15
 * 描述:
 */
public class Data {
    private static List<PersonModel>list=null;
    static {
        PersonModel wu = new PersonModel("wu qi", 18, "男");
        PersonModel zhang = new PersonModel("zhang san", 19, "男");
        PersonModel wang = new PersonModel("wang si", 20, "女");
        PersonModel zhao = new PersonModel("zhao wu", 20, "男");
        PersonModel chen = new PersonModel("chen liu", 21, "男");
        list = Arrays.asList(wu, zhang, wang, zhao, chen);
    }
    public static List<PersonModel>getData(){
        return list;
    }
    //過濾所有的男性
    public static void fiterSex(){
        List<PersonModel>data=Data.getData();
        List<PersonModel>temp=new ArrayList<>();
        for (PersonModel person:data){
            if ("男".equals(person.getSex())){
                temp.add(person);
            }
        }
        System.out.println(temp);
        //new
        List<PersonModel>collect=data.stream()
                .filter(perso->"男".equals(perso.getSex()))
                .collect(Collectors.toList());
        System.out.println(collect);
    }
    //過濾男性並且小於20歲
    public static void filterSexAndAge(){
        List<PersonModel>data=Data.getData();
        List<PersonModel>temp=new ArrayList<>();
        for (PersonModel person:data){
            if ("男".equals(person.getSex())&&person.getAge()<20){
                temp.add(person);
            }
        }
        List<PersonModel>collect=data.stream()
                .filter(person->{
                    if ("男".equals(person.getSex())&&person.getAge()<20){
                        return true;
                    }
                    return false;
                }).collect(Collectors.toList());
        List<PersonModel>collects=data.stream()
                .filter(person->("男".equals(person.getSex())&&person.getAge()<20))
                .collect(Collectors.toList());
    }
    //https://www.jianshu.com/p/9fe8632d0bc2
    //取出所有的用戶名
    public static void getUsernameList(){
        List<PersonModel>dsts=Data.getData();
        List<String>list=new ArrayList<>();
        for (PersonModel person:dsts){
            list.add(person.getName());
        }
        System.out.println(list);

        //new
        List<String>collect=dsts.stream()
                .map(person->person.getName())
                .collect(Collectors.toList());
        //new2
        List<String>collect2=dsts.stream()
                .map(PersonModel::getName)
                .collect(Collectors.toList());
        //new3
        List<String>collect3=dsts.stream()
                .map(person->{
                    System.out.println(person.getName());
                    return person.getName();
                }).collect(Collectors.toList());

    }
    public static void flatMapString(){
        List<PersonModel>data=Data.getData();
        List<String>collect=data.stream()
                .flatMap(person->Arrays.stream(person.getName().split("")))
                .collect(Collectors.toList());
        List<Stream<String>> collrct1=data.stream()
                .map(person->Arrays.stream(person.getName().split(" ")))
                .collect(Collectors.toList());
        List<String>collect3=data.stream()
                .map(person->person.getName().split(""))
                .flatMap(Arrays::stream)
                .collect(Collectors.toList());;
    }
    public static void reduceTest(){
        Integer re
                =Stream.of(1,2,3,4)
                .reduce(10,(count,item)->{
                    System.out.println("count:"+count);
                    System.out.println("item:"+item);
                    return count+item;
                });
        System.out.println(re);
        Integer reduce1=Stream.of(1,2,3,4)
                .reduce(0,(x,y)->x+y);
        System.out.println(reduce1);

//        Stream reduce2=Stream.of(1,2,3)
//                .reduce("0",(x,y)->(x+","+y));
    }
    public static void toListTest(){
        List<PersonModel>data=Data.getData();
        List<String>collect=data.stream()
                .map(PersonModel::getName)
                .collect(Collectors.toList());
    }
    //tomap
    public static void toMapTest(){
        List<PersonModel>data=Data.getData();
        Map<String,Integer>collect=data.stream()
                .collect(Collectors.toMap(PersonModel::getName,PersonModel::getAge)
                );
        data.stream()
                .collect(Collectors.toMap(PersonModel::getName, value->{
                    return value+"1";
                }));
    }
    //指定類型
    public static void toTreeSetTest(){
        List<PersonModel>data=Data.getData();
        TreeSet<PersonModel>collct=data.stream()
                .collect(Collectors.toCollection(TreeSet::new));
        System.out.println(collct);
    }
    //分租
    public static void toGroupTest(){
        List<PersonModel>data=Data.getData();
        Map<Boolean,List<PersonModel>>collect=data.stream()
                .collect(Collectors.groupingBy(per->"男".equals(per
                .getSex())));
        System.out.println(collect);
    }
    //分割
    public static void toJoinTest(){
        List<PersonModel>data=Data.getData();
        String collect=data.stream()
                .map(person->person.getName())
                .collect(Collectors.joining(",","{","}"));
        System.out.println(collect);
    }
    //測試LIST
    private static int size=10000000;
    public static void testList(){
        List<Integer>list=new ArrayList<>(size);
        for (Integer i=0;i<size;i++){
            list.add(new Integer(i));
        }
        List<Integer>temp1=new ArrayList<>(size);
        //laode
        long star=System.currentTimeMillis();
        for (Integer i:list){
            temp1.add(i);
        }
        System.out.println(+System.currentTimeMillis()-star);
        //同步
        long start1=System.currentTimeMillis();
        list.stream().collect(Collectors.toList());
        System.out.println(System.currentTimeMillis()-start1);
        //併發
        long start2=System.currentTimeMillis();
        list.parallelStream().collect(Collectors.toList());
        System.out.println(System.currentTimeMillis()-start2);
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章