Java入门与基础(optional实例)

package Option;
public class Computer {
	private Soundcard soundcard;
	public Computer() {
	}
	
	
	public Computer(Soundcard soundcard) {
		this.soundcard = soundcard;
	}
	
	
	public Soundcard getSoundcard() {
		return soundcard;
	}
	
	
	public void setSoundcard(Soundcard soundcard) {
		this.soundcard = soundcard;
	}

}
package Option;

import java.util.Optional;

public class OptionalTest {

    public static void main(String[] args) {
        //ifPresent(null);
        //map(null);
        //or(new USB("3.0"));
        getVersion2(new Computer(new Soundcard(new USB("3.0"))));
    }

    /**
     *
     * @param com
     * @return
     */
    public static String getVersion0(Computer com) {
        String v = com.getSoundcard().getUsb().getVersion();
        return v == null ? "UNKNOWN" : v;
    }

    /**
     * 冗余代码
     *
     * @param com
     * @return
     */
    private static String getVersion1(Computer com) {
        String version = "UNKNOWN";
        if (com != null) {
            Soundcard soundcard = com.getSoundcard();
            if (soundcard != null) {
                USB usb = soundcard.getUsb();
                if (usb != null) {
                    version = usb.getVersion();
                }
            }
        }
        return version;
    }

    /**
     * 打印传入的usb版本。传入的usb对象可能为空
     * 仅不为空执行,执行时既然可能空引用
     * @param usb
     */
    private static void ifPresent(USB usb) {
        Optional<USB> usbOP = Optional.ofNullable(usb);
        usbOP.ifPresent(u -> System.out.println(u.getVersion()));

        Optional.ofNullable(usb)
                .ifPresent(u -> System.out.println(u.getVersion()));
        System.out.println("dfsf");
        Optional.ofNullable(new Computer())
                .ifPresent(c -> System.out.println(c.getSoundcard().getUsb()));
    }


    /**
     * java9
     */
    private static void ifPresentOrElse(USB usb) {
        Optional.ofNullable(usb)
                .ifPresentOrElse(u -> System.out.println(u.getVersion()),
                        () -> System.out.println("dfdf"));

    }

    /**
     * 如果版本为3.0打印,否则不执行操作
     */
    private static void filter(USB usb) {
        Optional.ofNullable(usb)
                .filter(u -> "3.0".equals(u.getVersion()))
                .ifPresent(u -> System.out.println(u.getVersion()));
    }

    /**
     * 基于map映射usb版本,打印
     * @param usb
     */
    private static void map(USB usb) {
        Optional.ofNullable(usb)
                .map(USB::getVersion)
                .ifPresent(System.out::println);
    }

    /**
     * 打印版本
     * 为UNKNOWN,创建一个1.1版本usb
     * 不为UNKNOWN,直接打印
     */
    private static void or(USB usb) {
        Optional.of(usb)
                .filter(u -> !"UNKNOWN".equals(u.getVersion()))
                .or(() -> {
                    return Optional.of(new USB("1.1"));
                })
                .ifPresent(u -> System.out.println(u.getVersion()));

    }

    /**
     * 如果usb为空,创建usb 1.1
     * 获取版本
     *
     * @param usb
     */
    private static void orElse(USB usb) {
        String v1 = Optional.ofNullable(usb)
                .orElseGet(() -> {
                    return new USB("1.1");
                }).getVersion();
        System.out.println(v1);

        String v2 = Optional.ofNullable(usb)
                .orElse(new USB("1.1"))
                .getVersion();
        System.out.println(v2);

    }

    private static void Get(USB usb) {
        String v = Optional.ofNullable(usb)
                .or(() -> {
                    System.out.println("怎么会没有呢");
                    return Optional.of(new USB("1.1"));
                })
                .get()
                .getVersion();
    }

    /**
     * 获取Computer的usb版本
     * @param com
     */
    private static void getVersion2(Computer com) {
        String v = Optional.ofNullable(com)
                .map(Computer::getSoundcard)
                .map(Soundcard::getUsb)
                .map(USB::getVersion)
                .orElse("UNKNOWN");
        System.out.println(v);
    }


package Option;

public class Soundcard {
	private USB usb;
	public Soundcard() {
	}
	
	
	public Soundcard(USB usb) {
		this.usb = usb;
	}
	
	public USB getUsb() {
		return usb;
	}
	
	public void setUsb(USB usb) {
		this.usb = usb;
	}

}
package Option;

public class USB {
	private String version;
	public USB() {
	}
	
	public USB(String version) {
		this.version = version;
	}


	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值