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);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值