011—JAVA新特性Option类

Optional实际上是个容器:它可以保存类型T的值,或者仅仅保存null。Optional提供很多有用的方法,这样我们就不用显式进行空值检测。

如何创建Optional对象?或者说如何用Optional来装值对象或null值

(1)static <T> Optional<T> empty() :用来创建一个空的Optional

(2)static <T> Optional<T> of(T value) :用来创建一个非空的Optional

(3)static <T> Optional<T> ofNullable(T value) :用来创建一个可能是空,也可能非空的Optional

public class OptionTest {
    @Test
    public void test03(){
        String s = "hello";
//        String s = null;


        Optional<String> s1 = Optional.ofNullable(s);
        //非null显示原有值   null的时候自定义异常   
        String s2 = s1.orElseThrow(ArrayIndexOutOfBoundsException::new);
        System.out.println("s2 = " + s2);

        /*  *//*Optional<String> s1 = Optional.ofNullable(s);
        String s2 = s1.orElseThrow(ArrayIndexOutOfBoundsException::new);
        System.out.println("s2 = " + s2);
        System.out.println("s = " + s);*//*
        Optional<Integer> length1 = Optional.of( s.length());
        Integer integer = length1.orElseThrow(RuntimeException::new);
        System.out.println("integer = " + integer);*/


    }

    @Test
    public void test02(){
        String s = "hello";
        Optional<String> s1 = Optional.ofNullable(s);
        //如果数据是null 采用备用值,否则使用原有的值
        String orElse = s1.orElse("世界");
        System.out.println("s = " + s);

    }

    @Test
    public void test01(){
        String s = null;
        System.out.println("s.length() = " + s.length());
    }

}

练习一: 

public class Girl {
    String name;

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

    @Override
    public String toString() {
        return "Girl{" +
                "name='" + name + '\'' +
                '}';
    }
}
class Boy{
    String name;
    Girl girlFriend;

    public Boy(String name, Girl girlFriend) {
        this.name = name;
        this.girlFriend = girlFriend;
    }

    public String getName() {
        return name;
    }

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

    public Girl getGirlFriend() {
        return girlFriend;
    }

    public void setGirlFriend(Girl girlFriend) {
        this.girlFriend = girlFriend;
    }

    @Override
    public String toString() {
        return "Boy{" +
                "name='" + name + '\'' +
                ", girlFriend=" + girlFriend +
                '}';
    }
}
class TestBoy{
    public static void main(String[] args) {
        Boy zhangsan = new Boy("张三", null);
        Boy lihua = new Boy("李华", new Girl("小花"));


        Optional<Girl> girlFriend = Optional.ofNullable(zhangsan.getGirlFriend());

        //正常
//        Optional<Girl> girlFriend1 = Optional.ofNullable(lihua.getGirlFriend());
//
//        Optional.ofNullable(girlFriend1.orElse(new Girl("嫦娥"))).ifPresent(System.out::println);

        //异常
        Optional.ofNullable(girlFriend.orElse(new Girl("嫦娥"))).ifPresent(System.out::println);


    }

}

练习2

1)声明学生类,包含姓名和年龄

(2)添加几个学生对象到一个ArrayList<Student>集合中

(3)对集合中的学生进行操作,找出年龄大于30岁的,并取出第一个学生,如果没有这样的学生,用无参构造new一个学生对象,打印学生信息

public class Student {
    private String name;
    private int age;
    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public Student() {
        super();
    }
    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;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + "]";
    }
}
class Test{
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        list.add(new Student("张三", 23));
        list.add(new Student("李四", 22));
        list.add(new Student("王五", 25));

        Student student = list.stream()
                .filter(s -> s.getAge() > 30) //找出年龄大于30的
                .findFirst().orElse(new Student()); //取出第一个  如果没有年龄大于30的,此时取出操作肯定会报空指针异常
        System.out.println("学生的年龄" + student.getAge());


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值