java8新特性学习(一)Lambda表达式用法&Stream类操对对象的操作和集合操作&日期类操作

版权声明:本文为博主原创文章,未经博主允许不得转载。如需要授权请联系[email protected] https://blog.csdn.net/leeue/article/details/89028906

Java8新特性学习 基础部分

一、lambda 表达式学习

1、为什么要使用lambda表达式?

答:1、Java是面向对象的语言,不能像函数式语言那样嵌套方法。
2、Java的匿名内部类只能存在于创建它的线程中,不能运行在多线程中。无法利用多核的优点。
Java匿名函数在写法上的缺点:
1、语法相对复杂
2、在调用内部类的上下文中,指引和this代指容易混淆
3、类加载和实例创建不可避免
4、不能引用外部的非final对象
5、不能抽象化控制流程

2、如和使用Lambda?

lambda 语法有三部分:
1、参数列表 
2、箭头符号 ->
3、代码块

案例:使用lambda简化实现Runnable接口
Java8之前的写法
package com.leeue.test;
/**
 *
 * 内部类实现的区别
 * lambda 使用lambda实现以前的runable接口
 *
 * @date 2019/4/2 11 04
 */
public class Demo01_lambda {

    public static void main(String[] args) {
         //java8之前的写法
        new Runnable(){
            public void  run(){
                System.out.println("使用匿名内部类的方式实现Runnable接口");
            }
        }.run();
    }
}
java8使用lamdba写法
package com.leeue.test;

/**
 *
 * 内部类实现的区别
 * lambda 使用lambda实现以前的runable接口
 *
 * @date 2019/4/2 11 04
 */
public class Demo01_lambda {

    public static void main(String[] args) {
    
        //java8 使用lambda写法
        int i = 0;  //如果要在匿名内部类中做操作还是要加final
        Runnable  r = ()->{
            System.out.println("Java8新特性实现内部类");
           System.out.println(i);
        };
        r.run();
    }
}
案例2 使用lambda表达式模拟登录
package com.leeue.test;

/**
 * 案例:lambda自定义接口,模拟登录操作
 *
 * @author liyue
 * @date 2019/4/2 11 25
 */
public class Demo02_lambda {

    public static void main(String[] args) {

        //Java8之前的方式
        new Action() {

            @Override
            public void execute(String content) {

                System.out.println(content);
            }
        }.execute("java8之前匿名内部类方式执行登录操作");

        //Java8 使用lambda写法  参数列表 箭头 代码块

        Action login = (String content) -> {
            System.out.println(content);
        };
        login.execute("使用java8执行的内部类操作");

    }

    static interface Action {


        void execute(String content);
    }
}
Stream类
1、什么是Steam类?
答:1、Stream类在Java8中被定义成为是泛型的接口。
   2、Stream接口代表数据流。
   3、Stream不是数据结构,不能直接存储数据。
   4、Stream通过管道来操作数据
   5、创建Stream接口实现类对象:
   	Stream<User>  stream = person.stream();


2、什么是管道?
答:管道代表操作序列,管道组件有:数据集、数组等 还有终端操作如forEach()
案例1:创建一个元素为Person类的集合:people使用Stream和forEach显示该集合的所有元素
Person.java
package com.leeue.entity;

import lombok.Data;

/**
 * @author liyue
 * @date 2019/4/2 11 37
 */
@Data
public class Person {

    public static enum Sex {
        male, FEMALE;
    }

    private String name;
    private Sex gender;
    private int age;
    private double height;

    public Person(String name, Sex gender, int age, double height) {
        this.name = name;
        this.gender = gender;
        this.age = age;
        this.height = height;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", gender=" + gender +
                ", age=" + age +
                ", height=" + height +
                '}';
    }
}

使用Stream来遍历
package com.leeue.test;

import com.leeue.entity.Person;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * @author liyue
 * @date 2019/4/2 11 34
 */
public class Demo03_Stream {

    public static void main(String[] args) {

        List<Person> people = createPeople();
        //Java8 Stream接口 进行遍历
        Stream<Person> stream = people.stream();
        stream.forEach(p->System.out.println(p.toString()));
    }
    static List<Person> createPeople() {

        List<Person> people = new ArrayList<>();
        Person person = new Person("张飞", Person.Sex.FEMALE, 21, 180);
        people.add(person);
        person = new Person("张飞1", Person.Sex.FEMALE, 22, 180);
        people.add(person);
        person = new Person("张飞2", Person.Sex.FEMALE, 22, 180);
        people.add(person);
        person = new Person("张飞3", Person.Sex.FEMALE, 22, 180);
        people.add(person);
        person = new Person("张飞4", Person.Sex.FEMALE, 22, 180);
        people.add(person);
        return people;
    }
}
案例2:使用Stream筛选出性别为女的
package com.leeue.test;

import com.leeue.entity.Person;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liyue
 * @date 2019/4/2 11 34
 */
public class Demo04_Collection_Stream {

    public static void main(String[] args) {

        List<Person> people = createPeople();
        //Java8 Stream接口 进行遍历
        people.stream().
                filter(p -> p.getGender() == Person.Sex.FEMALE).
                forEach(p -> System.out.println(p.toString()));


    }

    static List<Person> createPeople() {

        List<Person> people = new ArrayList<>();

        Person person = new Person("张飞", Person.Sex.FEMALE, 21, 180);
        people.add(person);
        person = new Person("张飞1", Person.Sex.male, 22, 180);
        people.add(person);
        person = new Person("张飞2", Person.Sex.FEMALE, 22, 180);
        people.add(person);
        person = new Person("张飞3", Person.Sex.male, 22, 180);
        people.add(person);
        person = new Person("张飞4", Person.Sex.FEMALE, 22, 180);
        people.add(person);
        return people;
    }
}
案例3:DoubleStream 用法
package com.leeue.test;

import com.leeue.entity.Person;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liyue
 * @date 2019/4/3 14 36
 */
public class Demo05_DoubleStream {
    public static void main(String[] args) {
        List<Person> people = createPeople();

        Double d = people.stream()
                .filter(p -> p.getName().indexOf('飞') >= 0)
                .mapToDouble(p -> p.getHeight()).average().getAsDouble();

        System.out.println(d);

        LocalDateTime date = LocalDateTime.of(2018, 10, 10, 24, 0, 0);
        System.out.println(date);

    }


    static List<Person> createPeople() {

        List<Person> people = new ArrayList<>();

        Person person = new Person("张三", Person.Sex.FEMALE, 21, 180);
        people.add(person);
        person = new Person("张飞1", Person.Sex.male, 22, 180);
        people.add(person);
        person = new Person("张飞2", Person.Sex.FEMALE, 22, 180);
        people.add(person);
        person = new Person("张飞3", Person.Sex.male, 22, 180);
        people.add(person);
        person = new Person("张飞4", Person.Sex.FEMALE, 22, 180);
        people.add(person);
        return people;
    }
}

DoubleStream还有其他方法

在这里插入图片描述

三、java8还有几个日期类,这里我直接贴上方法。现在用Java8的日期类很方便。
1、LocalDate类常用方法

在这里插入图片描述
在这里插入图片描述

Java8之前的Calendar类显示的日期要比实际的日期小一个数。这里的LocalDate解决了。

时间类-LocalTime

在这里插入图片描述

日期时间类—LocalDateTime

在这里插入图片描述

四、Java8提供对日期时间的格式化方法

DateTimeFormatter类:解析字符串为日期

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

ZonedDateTime类格式化日期为字符串

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/leeue/article/details/89028906