Java8新特性代码示例浅聊

  Java 8 (又称为 jdk 1.8) 是 Java 语言开发的一个主要版本。 Oracle 公司于 2014 年 3 月 18 日发布 Java 8 ,它支持函数式编程,新的 JavaScript 引擎,新的日期 API,新的Stream API 等。
  博主最近才有空研究了一下Java8的一些新特性,简单地写了一些测试用例,感觉开始有那味了,受益颇深,将示例代码贴出,以供大家学习交流。

  测试用例代码:

  

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

/**
 * @ClassName Java8Test
 * @Description java8新特性测试用例
 * @Author zzp
 * @Date 2020/6/30 11:06
 * @Version 1.0
 */
public class Java8Test {


    public static void main(String[] args) {
        Java8Test test=new Java8Test();
        List hobbies1= Arrays.asList("篮球","看书","写作");
        List hobbies2= Arrays.asList("足球","看书","睡觉");

        Person mayun=new Person("马云","老师",hobbies1);
        Person mahuateng=new Person("马化腾","码农",hobbies2);
        Person leijun=new Person("雷军","码农",hobbies1);

        List persons=Arrays.asList(mayun,mahuateng,leijun);

        //情景1:找一个人叫马云的这个人详细资料
        //java8之前
       Person person1= null;
        for (Person person:persons ) {
            if(person.getName().equals("马云")){
                person1=  person;
                break;
            }
        }
        System.out.println(person1);

       //java8 一行搞定
        Optional person2=persons.stream().filter(person -> person.getName().equals("马云")).findFirst();
        System.out.println(person2.orElse(new Person("叫马云的人不存在")));

        //情景2:按照职业给mayun mahuateng leijun这个三个人分组
        //java8之前
        Map> workPersonMap1=new HashMap<>();
        for (Person person:persons) {
            String work=person.getWork();
            List personList=null;
            if(null==workPersonMap1.get(work)){
                personList=new ArrayList<>();
            }else{
                personList=workPersonMap1.get(work);
            }
            personList.add(person);
            workPersonMap1.put(work,personList);
        }
        System.out.println(workPersonMap1);

        //java8 一行搞定
        Map> workPersonMap2=  persons.stream().collect(Collectors.groupingBy(Person::getWork));
        System.out.println(workPersonMap2);

        //情景3:获得mayun mahuateng leijun这个三个人的所有爱好
        //java8之前
        Set hobbiesSet=new HashSet<>();
        for (Person person:persons) {
            hobbiesSet.addAll(person.getHobbies());
        }
        System.out.println(hobbiesSet);

        //java8 一行搞定
        Set hobbiesSet2= persons.stream().flatMap(p->p.getHobbies().stream()).collect(Collectors.toSet());
        System.out.println(hobbiesSet2);


    }

}
import java.util.List;

/**
 * @ClassName Person
 * @Description TODO
 * @Author zzp
 * @Date 2020/6/30 11:17
 * @Version 1.0
 */
public class Person {
    private String name;
    private String work;
    private List hobbies;


    public Person() {
    }

    public Person(String name, String work, List hobbies) {
        this.name = name;
        this.work = work;
        this.hobbies = hobbies;
    }

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    public String getWork() {
        return work;
    }

    public void setWork(String work) {
        this.work = work;
    }

    public List getHobbies() {
        return hobbies;
    }

    public void setHobbies(List hobbies) {
        this.hobbies = hobbies;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", work='" + work + '\'' +
                ", hobbies=" + hobbies +
                '}';
    }
}

 详细的java8特性下一篇给大家贴出,大家看了有什么感受也可以在评论区交流。如果觉得博主不易,就给点个赞吧,拒绝白嫖~

你可能感兴趣的:(Java8新特性代码示例浅聊)