(1) This optional. of method still does not accept null as an input parameter:

Optional. OfNullable accepts null arguments:

IsPresent determines whether there is data or not, there is nothing to say about this:

If the Optional container contains a value other than null, execute the action.

OrElse: Returns the original object if the Optional object exists, otherwise returns the new object constructed in orElse:

GetName on line 84 above is actually a mapper that maps the Person object to a String.

In the debugger, the mapper is actually a Lambda Function:

The result is that the wrapper type is the second argument to the mapping function:

IfPresent takes an action:

My test code:

package optionalTest;

import java.util.Optional;
import java.util.function.Function;

public class OptionalTest {

	class Person {
		private String mName;
		private Skill mSkill;
		public Person(String name) {
			mName = name;
		}
		
		public Skill getSkill(a){
			return mSkill;
		}
		
		public Optional<Skill> getSkillOptional(a){
			return Optional.ofNullable(mSkill);
		}
		public void setSkill(Skill skill){
			mSkill = skill;
		}
		public String getName(a) {
			return mName;
		}
		
		public void greet(a) {
			System.out.println("I am: "+ mName); }}class Skill{
		private String mName;
		private int mLevel;
		public Skill(String name, int level) {
			mName = name;
			mLevel = level;
		}
		public void display(a){
			System.out.println("Skill: " + mName + " level: "+ mLevel); }}@SuppressWarnings("unused")
	public void scenario1(a){
		Person person = null;
		try {
			Optional<Person> p1 = Optional.of(person);
		}
		catch ( Exception e) {
			System.out.println("Error: " + e.getClass().getName() + "-" + e.getLocalizedMessage());
		}
		
		Optional<Person> p2 = Optional.ofNullable(person);
		System.out.println(p2.isPresent());
		
		Person jerry = new Person("Jerry");
		Optional<Person> p3 = Optional.ofNullable(jerry);
		System.out.println(p3.isPresent());
		
		System.out.println("p2 test");
		p2.ifPresent((p) -> p.greet());
		System.out.println("p3 test");
		p3.ifPresent((p) -> p.greet());
		
		System.out.println("Example 3");
		// before Jave8Person oldImplementation = ( person ! =null? person:new Person("Ji")); 
		Person newPerson = p2.orElse(new Person("Ji"));
		Person existPerson = p3.orElse(new Person("Mr Unknown"));
		newPerson.greet();
		existPerson.greet();
		
		if( jerry ! =null && jerry.getName().equals("Jerry")) {
			System.out.println("old style-> Jerry found: " + jerry.getName());
		}
		p3.filter( personHolder -> "Jerry".equals(personHolder.getName())).
			ifPresent((personH2) -> System.out.println("new style-> Jerry found: " + personH2.getName()));
		
		Function<Person,String> mapper = Person::getName;
		
		Optional<String> mapResult = p3.map(mapper);
		
		p3.map(Person::getName)
			.filter( Name -> "Jerry".equals(Name))
			.ifPresent( foundName -> System.out.println("new style 2-> Jerry found: " + foundName));
		
		/*jerry.setSkill("JavaScript"); Optional
      
        jerrySkill = p3.map(Person::getSkill); System.out.println(jerrySkill.isPresent()); p3.map(Person::getSkill) .filter(Skill -> "JavaScript".equals(Skill)) .ifPresent( Skill2 -> System.out.println("Jerry's skill is: " + Skill2 ) ); * /
      
		
		Skill jsSkill = new Skill("JavaScript".80);
		jerry.setSkill(jsSkill);
		
		Optional<Skill> temp = p3.map(Person::getSkill); 
		Optional<Skill> temp2 = p3.flatMap(Person::getSkillOptional);
		
		Skill s1 = temp.get();
		Skill s2 = temp2.get();
		
		s1.display();
		s2.display();

	}
	
	public static void main(String[] args) {
		OptionalTest test = newOptionalTest(); test.scenario1(); }}Copy the code

For more of Jerry’s original articles, please follow the public account “Wang Zixi “: