คำแนะนำเกี่ยวกับ Java 8 ทางเลือก

1. ภาพรวม

ในบทช่วยสอนนี้เราจะแสดงคลาสเสริมที่เปิดตัวใน Java 8

วัตถุประสงค์ของคลาสคือการจัดเตรียมโซลูชันระดับชนิดสำหรับการแสดงค่าทางเลือกแทนการอ้างอิงค่าว่าง

หากต้องการทำความเข้าใจให้ลึกซึ้งยิ่งขึ้นว่าเหตุใดเราจึงควรสนใจคลาสที่เป็นตัวเลือกโปรดดูบทความอย่างเป็นทางการของ Oracle

2. การสร้างวัตถุเสริม

มีหลายวิธีในการสร้างอ็อบเจกต์เสริม

ในการสร้างอ็อบเจกต์ทางเลือกที่ว่างเปล่าเราจำเป็นต้องใช้วิธีการคงที่ว่าง () :

@Test public void whenCreatesEmptyOptional_thenCorrect() { Optional empty = Optional.empty(); assertFalse(empty.isPresent()); }

โปรดทราบว่าเราใช้เมธอด isPresent ()เพื่อตรวจสอบว่ามีค่าอยู่ในอ็อบเจกต์เสริมหรือไม่ ค่าที่เป็นปัจจุบันเฉพาะในกรณีที่เราได้สร้างตัวเลือกที่มีไม่ใช่nullค่า เราจะดูวิธีisPresent ()ในส่วนถัดไป

นอกจากนี้เรายังสามารถสร้างอ็อบเจกต์ทางเลือกด้วยวิธีการแบบคงที่ของ () :

@Test public void givenNonNull_whenCreatesNonNullable_thenCorrect() { String name = "baeldung"; Optional opt = Optional.of(name); assertTrue(opt.isPresent()); }

อย่างไรก็ตามอาร์กิวเมนต์ที่ส่งไปยังเมธอดof ()ต้องไม่เป็นโมฆะ มิฉะนั้นเราจะได้รับNullPointerException :

@Test(expected = NullPointerException.class) public void givenNull_whenThrowsErrorOnCreate_thenCorrect() { String name = null; Optional.of(name); }

แต่ในกรณีที่เราคาดหวังค่าว่างบางค่าเราสามารถใช้เมธอด ofNullable () :

@Test public void givenNonNull_whenCreatesNullable_thenCorrect() { String name = "baeldung"; Optional opt = Optional.ofNullable(name); assertTrue(opt.isPresent()); }

การทำเช่นนี้หากเราส่งผ่านการอ้างอิงที่เป็นโมฆะมันจะไม่ทำให้เกิดข้อยกเว้น แต่จะส่งคืนอ็อบเจ็กต์ทางเลือกที่ว่างเปล่า:

@Test public void givenNull_whenCreatesNullable_thenCorrect() { String name = null; Optional opt = Optional.ofNullable(name); assertFalse(opt.isPresent()); }

3. การตรวจสอบการแสดงค่า: isPresent ()และisEmpty ()

เมื่อเรามีอ็อบเจ็กต์ทางเลือกที่ส่งคืนจากเมธอดหรือสร้างโดยเราเราสามารถตรวจสอบว่ามีค่าอยู่หรือไม่ด้วยเมธอด isPresent () :

@Test public void givenOptional_whenIsPresentWorks_thenCorrect() { Optional opt = Optional.of("Baeldung"); assertTrue(opt.isPresent()); opt = Optional.ofNullable(null); assertFalse(opt.isPresent()); }

เมธอดนี้จะคืนค่าจริงหากค่าที่รวมไว้ไม่ใช่null

นอกจากนี้ใน Java 11 เราสามารถทำสิ่งที่ตรงกันข้ามกับเมธอดisEmpty :

@Test public void givenAnEmptyOptional_thenIsEmptyBehavesAsExpected() { Optional opt = Optional.of("Baeldung"); assertFalse(opt.isEmpty()); opt = Optional.ofNullable(null); assertTrue(opt.isEmpty()); }

4. การดำเนินการตามเงื่อนไขด้วยifPresent ()

ifPresent ()วิธีการช่วยให้เราสามารถเรียกใช้รหัสบางอย่างเกี่ยวกับค่าห่อถ้ามันพบว่าเป็นที่ไม่ใช่null ก่อนทางเลือกเราจะทำ:

if(name != null) { System.out.println(name.length()); }

รหัสนี้ตรวจสอบว่าตัวแปรชื่อเป็นโมฆะหรือไม่ก่อนที่จะดำเนินการกับโค้ดบางส่วน วิธีนี้มีความยาวและไม่ใช่ปัญหาเดียว - ยังมีแนวโน้มที่จะเกิดข้อผิดพลาด

แน่นอนว่าอะไรรับประกันว่าหลังจากพิมพ์ตัวแปรนั้นแล้วเราจะไม่ใช้มันอีกแล้วลืมทำการตรวจสอบค่าว่าง?

สิ่งนี้สามารถส่งผลให้เกิดNullPointerExceptionที่รันไทม์หากค่า null พบทางเข้าไปในโค้ดนั้น เมื่อโปรแกรมล้มเหลวเนื่องจากปัญหาการป้อนข้อมูลมักเป็นผลมาจากแนวทางปฏิบัติในการเขียนโปรแกรมที่ไม่ดี

ทางเลือกทำให้เราจัดการกับค่าที่เป็นโมฆะได้อย่างชัดเจนเพื่อบังคับใช้แนวทางปฏิบัติในการเขียนโปรแกรมที่ดี

ตอนนี้มาดูกันว่าโค้ดด้านบนสามารถ refactored ใน Java 8 ได้อย่างไร

ในรูปแบบการเขียนโปรแกรมเชิงฟังก์ชันทั่วไปเราสามารถดำเนินการดำเนินการกับวัตถุที่มีอยู่จริง:

@Test public void givenOptional_whenIfPresentWorks_thenCorrect() { Optional opt = Optional.of("baeldung"); opt.ifPresent(name -> System.out.println(name.length())); }

ในตัวอย่างข้างต้นเราใช้โค้ดเพียงสองบรรทัดเพื่อแทนที่ห้าบรรทัดที่ทำงานในตัวอย่างแรก: หนึ่งบรรทัดเพื่อรวมอ็อบเจกต์ไว้ในอ็อบเจ็กต์ทางเลือกและถัดไปเพื่อทำการตรวจสอบความถูกต้องโดยนัยรวมทั้งรันโค้ด

5. ค่าเริ่มต้นด้วยorElse ()

orElse ()วิธีการที่ใช้ในการเรียกค่าห่อภายในตัวเลือกเช่น ใช้เวลาหนึ่งพารามิเตอร์ซึ่งทำหน้าที่เป็นค่าเริ่มต้น orElse ()วิธีการส่งกลับค่าห่อถ้ามันเป็นปัจจุบันและโต้แย้งอย่างอื่น

@Test public void whenOrElseWorks_thenCorrect() { String nullName = null; String name = Optional.ofNullable(nullName).orElse("john"); assertEquals("john", name); }

6. ค่าเริ่มต้นด้วยorElseGet ()

orElseGet ()วิธีการคล้ายกับorElse () อย่างไรก็ตามแทนที่จะรับค่าเพื่อส่งคืนหากไม่มีค่าทางเลือกจะใช้อินเทอร์เฟซการทำงานของซัพพลายเออร์ซึ่งเรียกใช้และส่งคืนค่าของการเรียกใช้:

@Test public void whenOrElseGetWorks_thenCorrect() { String nullName = null; String name = Optional.ofNullable(nullName).orElseGet(() -> "john"); assertEquals("john", name); }

7. ความแตกต่างระหว่างorElseและorElseGet ()

สำหรับโปรแกรมเมอร์จำนวนมากที่ยังใหม่กับOptionalหรือ Java 8 ความแตกต่างระหว่างorElse ()และorElseGet ()ยังไม่ชัดเจน ตามความเป็นจริงสองวิธีนี้ให้ความรู้สึกว่าทับซ้อนกันในฟังก์ชันการทำงาน

อย่างไรก็ตามมีความแตกต่างที่ลึกซึ้ง แต่สำคัญมากระหว่างทั้งสองอย่างที่อาจส่งผลต่อประสิทธิภาพของโค้ดของเราอย่างมากหากไม่เข้าใจดี

มาสร้างเมธอดที่เรียกว่าgetMyDefault ()ในคลาสทดสอบซึ่งไม่มีอาร์กิวเมนต์และส่งกลับค่าดีฟอลต์:

public String getMyDefault() { System.out.println("Getting Default Value"); return "Default Value"; }

มาดูการทดสอบสองครั้งและสังเกตผลข้างเคียงเพื่อสร้างทั้งที่orElse ()และorElseGet ()ทับซ้อนกันและจุดที่แตกต่างกัน:

@Test public void whenOrElseGetAndOrElseOverlap_thenCorrect() { String text = null; String defaultText = Optional.ofNullable(text).orElseGet(this::getMyDefault); assertEquals("Default Value", defaultText); defaultText = Optional.ofNullable(text).orElse(getMyDefault()); assertEquals("Default Value", defaultText); }

In the above example, we wrap a null text inside an Optional object and attempt to get the wrapped value using each of the two approaches.

The side effect is:

Getting default value... Getting default value...

The getMyDefault() method is called in each case. It so happens that when the wrapped value is not present, then both orElse() and orElseGet() work exactly the same way.

Now let's run another test where the value is present, and ideally, the default value should not even be created:

@Test public void whenOrElseGetAndOrElseDiffer_thenCorrect() { String text = "Text present"; System.out.println("Using orElseGet:"); String defaultText = Optional.ofNullable(text).orElseGet(this::getMyDefault); assertEquals("Text present", defaultText); System.out.println("Using orElse:"); defaultText = Optional.ofNullable(text).orElse(getMyDefault()); assertEquals("Text present", defaultText); }

In the above example, we are no longer wrapping a null value, and the rest of the code remains the same.

Now let's take a look at the side effect of running this code:

Using orElseGet: Using orElse: Getting default value...

Notice that when using orElseGet() to retrieve the wrapped value, the getMyDefault() method is not even invoked since the contained value is present.

However, when using orElse(), whether the wrapped value is present or not, the default object is created. So in this case, we have just created one redundant object that is never used.

In this simple example, there is no significant cost to creating a default object, as the JVM knows how to deal with such. However, when a method such as getMyDefault() has to make a web service call or even query a database, the cost becomes very obvious.

8. Exceptions With orElseThrow()

The orElseThrow() method follows from orElse() and orElseGet() and adds a new approach for handling an absent value.

Instead of returning a default value when the wrapped value is not present, it throws an exception:

@Test(expected = IllegalArgumentException.class) public void whenOrElseThrowWorks_thenCorrect() { String nullName = null; String name = Optional.ofNullable(nullName).orElseThrow( IllegalArgumentException::new); }

Method references in Java 8 come in handy here, to pass in the exception constructor.

Java 10 introduced a simplified no-arg version of orElseThrow() method. In case of an empty Optional it throws a NoSuchElelementException:

@Test(expected = NoSuchElementException.class) public void whenNoArgOrElseThrowWorks_thenCorrect() { String nullName = null; String name = Optional.ofNullable(nullName).orElseThrow(); }

9. Returning Value With get()

The final approach for retrieving the wrapped value is the get() method:

@Test public void givenOptional_whenGetsValue_thenCorrect() { Optional opt = Optional.of("baeldung"); String name = opt.get(); assertEquals("baeldung", name); }

However, unlike the previous three approaches, get() can only return a value if the wrapped object is not null; otherwise, it throws a no such element exception:

@Test(expected = NoSuchElementException.class) public void givenOptionalWithNull_whenGetThrowsException_thenCorrect() { Optional opt = Optional.ofNullable(null); String name = opt.get(); }

This is the major flaw of the get() method. Ideally, Optional should help us avoid such unforeseen exceptions. Therefore, this approach works against the objectives of Optional and will probably be deprecated in a future release.

So, it's advisable to use the other variants that enable us to prepare for and explicitly handle the null case.

10. Conditional Return With filter()

We can run an inline test on our wrapped value with the filter method. It takes a predicate as an argument and returns an Optional object. If the wrapped value passes testing by the predicate, then the Optional is returned as-is.

However, if the predicate returns false, then it will return an empty Optional:

@Test public void whenOptionalFilterWorks_thenCorrect() { Integer year = 2016; Optional yearOptional = Optional.of(year); boolean is2016 = yearOptional.filter(y -> y == 2016).isPresent(); assertTrue(is2016); boolean is2017 = yearOptional.filter(y -> y == 2017).isPresent(); assertFalse(is2017); }

The filter method is normally used this way to reject wrapped values based on a predefined rule. We could use it to reject a wrong email format or a password that is not strong enough.

Let's look at another meaningful example. Say we want to buy a modem, and we only care about its price.

We receive push notifications on modem prices from a certain site and store these in objects:

public class Modem { private Double price; public Modem(Double price) { this.price = price; } // standard getters and setters }

We then feed these objects to some code whose sole purpose is to check if the modem price is within our budget range.

Let's now take a look at the code without Optional:

public boolean priceIsInRange1(Modem modem) { boolean isInRange = false; if (modem != null && modem.getPrice() != null && (modem.getPrice() >= 10 && modem.getPrice() <= 15)) { isInRange = true; } return isInRange; }

Pay attention to how much code we have to write to achieve this, especially in the if condition. The only part of the if condition that is critical to the application is the last price-range check; the rest of the checks are defensive:

@Test public void whenFiltersWithoutOptional_thenCorrect() { assertTrue(priceIsInRange1(new Modem(10.0))); assertFalse(priceIsInRange1(new Modem(9.9))); assertFalse(priceIsInRange1(new Modem(null))); assertFalse(priceIsInRange1(new Modem(15.5))); assertFalse(priceIsInRange1(null)); }

Apart from that, it's possible to forget about the null checks over a long day without getting any compile-time errors.

Now let's look at a variant with Optional#filter:

public boolean priceIsInRange2(Modem modem2) { return Optional.ofNullable(modem2) .map(Modem::getPrice) .filter(p -> p >= 10) .filter(p -> p <= 15) .isPresent(); }

The map call is simply used to transform a value to some other value. Keep in mind that this operation does not modify the original value.

In our case, we are obtaining a price object from the Model class. We will look at the map() method in detail in the next section.

First of all, if a null object is passed to this method, we don't expect any problem.

Secondly, the only logic we write inside its body is exactly what the method name describes — price-range check. Optional takes care of the rest:

@Test public void whenFiltersWithOptional_thenCorrect() { assertTrue(priceIsInRange2(new Modem(10.0))); assertFalse(priceIsInRange2(new Modem(9.9))); assertFalse(priceIsInRange2(new Modem(null))); assertFalse(priceIsInRange2(new Modem(15.5))); assertFalse(priceIsInRange2(null)); }

The previous approach promises to check price range but has to do more than that to defend against its inherent fragility. Therefore, we can use the filter method to replace unnecessary if statements and reject unwanted values.

11. Transforming Value With map()

In the previous section, we looked at how to reject or accept a value based on a filter.

We can use a similar syntax to transform the Optional value with the map() method:

@Test public void givenOptional_whenMapWorks_thenCorrect() { List companyNames = Arrays.asList( "paypal", "oracle", "", "microsoft", "", "apple"); Optional
    
      listOptional = Optional.of(companyNames); int size = listOptional .map(List::size) .orElse(0); assertEquals(6, size); }
    

In this example, we wrap a list of strings inside an Optional object and use its map method to perform an action on the contained list. The action we perform is to retrieve the size of the list.

The map method returns the result of the computation wrapped inside Optional. We then have to call an appropriate method on the returned Optional to retrieve its value.

Notice that the filter method simply performs a check on the value and returns a boolean. The map method however takes the existing value, performs a computation using this value, and returns the result of the computation wrapped in an Optional object:

@Test public void givenOptional_whenMapWorks_thenCorrect2() { String name = "baeldung"; Optional nameOptional = Optional.of(name); int len = nameOptional .map(String::length) .orElse(0); assertEquals(8, len); }

We can chain map and filter together to do something more powerful.

Let's assume we want to check the correctness of a password input by a user. We can clean the password using a map transformation and check its correctness using a filter:

@Test public void givenOptional_whenMapWorksWithFilter_thenCorrect() { String password = " password "; Optional passOpt = Optional.of(password); boolean correctPassword = passOpt.filter( pass -> pass.equals("password")).isPresent(); assertFalse(correctPassword); correctPassword = passOpt .map(String::trim) .filter(pass -> pass.equals("password")) .isPresent(); assertTrue(correctPassword); }

As we can see, without first cleaning the input, it will be filtered out — yet users may take for granted that leading and trailing spaces all constitute input. So, we transform a dirty password into a clean one with a map before filtering out incorrect ones.

12. Transforming Value With flatMap()

Just like the map() method, we also have the flatMap() method as an alternative for transforming values. The difference is that map transforms values only when they are unwrapped whereas flatMap takes a wrapped value and unwraps it before transforming it.

Previously, we created simple String and Integer objects for wrapping in an Optional instance. However, frequently, we will receive these objects from an accessor of a complex object.

To get a clearer picture of the difference, let's have a look at a Person object that takes a person's details such as name, age and password:

public class Person { private String name; private int age; private String password; public Optional getName() { return Optional.ofNullable(name); } public Optional getAge() { return Optional.ofNullable(age); } public Optional getPassword() { return Optional.ofNullable(password); } // normal constructors and setters }

We would normally create such an object and wrap it in an Optional object just like we did with String.

Alternatively, it can be returned to us by another method call:

Person person = new Person("john", 26); Optional personOptional = Optional.of(person);

Notice now that when we wrap a Person object, it will contain nested Optional instances:

@Test public void givenOptional_whenFlatMapWorks_thenCorrect2() { Person person = new Person("john", 26); Optional personOptional = Optional.of(person); Optional
    
      nameOptionalWrapper = personOptional.map(Person::getName); Optional nameOptional = nameOptionalWrapper.orElseThrow(IllegalArgumentException::new); String name1 = nameOptional.orElse(""); assertEquals("john", name1); String name = personOptional .flatMap(Person::getName) .orElse(""); assertEquals("john", name); }
    

Here, we're trying to retrieve the name attribute of the Person object to perform an assertion.

Note how we achieve this with map() method in the third statement, and then notice how we do the same with flatMap() method afterwards.

The Person::getName method reference is similar to the String::trim call we had in the previous section for cleaning up a password.

The only difference is that getName() returns an Optional rather than a String as did the trim() operation. This, coupled with the fact that a map transformation wraps the result in an Optional object, leads to a nested Optional.

While using map() method, therefore, we need to add an extra call to retrieve the value before using the transformed value. This way, the Optional wrapper will be removed. This operation is performed implicitly when using flatMap.

13. Chaining Optionals in Java 8

Sometimes, we may need to get the first non-empty Optional object from a number of Optionals. In such cases, it would be very convenient to use a method like orElseOptional(). Unfortunately, such operation is not directly supported in Java 8.

Let's first introduce a few methods that we'll be using throughout this section:

private Optional getEmpty() { return Optional.empty(); } private Optional getHello() { return Optional.of("hello"); } private Optional getBye() { return Optional.of("bye"); } private Optional createOptional(String input) { if (input == null || "".equals(input) || "empty".equals(input)) { return Optional.empty(); } return Optional.of(input); }

In order to chain several Optional objects and get the first non-empty one in Java 8, we can use the Stream API:

@Test public void givenThreeOptionals_whenChaining_thenFirstNonEmptyIsReturned() { Optional found = Stream.of(getEmpty(), getHello(), getBye()) .filter(Optional::isPresent) .map(Optional::get) .findFirst(); assertEquals(getHello(), found); }

The downside of this approach is that all of our get methods are always executed, regardless of where a non-empty Optional appears in the Stream.

If we want to lazily evaluate the methods passed to Stream.of(), we need to use the method reference and the Supplier interface:

@Test public void givenThreeOptionals_whenChaining_thenFirstNonEmptyIsReturnedAndRestNotEvaluated() { Optional found = Stream.
    
     >of(this::getEmpty, this::getHello, this::getBye) .map(Supplier::get) .filter(Optional::isPresent) .map(Optional::get) .findFirst(); assertEquals(getHello(), found); }
    

In case we need to use methods that take arguments, we have to resort to lambda expressions:

@Test public void givenTwoOptionalsReturnedByOneArgMethod_whenChaining_thenFirstNonEmptyIsReturned() { Optional found = Stream.
    
     >of( () -> createOptional("empty"), () -> createOptional("hello") ) .map(Supplier::get) .filter(Optional::isPresent) .map(Optional::get) .findFirst(); assertEquals(createOptional("hello"), found); }
    

Often, we'll want to return a default value in case all of the chained Optionals are empty. We can do so just by adding a call to orElse() or orElseGet():

@Test public void givenTwoEmptyOptionals_whenChaining_thenDefaultIsReturned() { String found = Stream.
    
     >of( () -> createOptional("empty"), () -> createOptional("empty") ) .map(Supplier::get) .filter(Optional::isPresent) .map(Optional::get) .findFirst() .orElseGet(() -> "default"); assertEquals("default", found); }
    

14. JDK 9 Optional API

The release of Java 9 added even more new methods to the Optional API:

  • or() method for providing a supplier that creates an alternative Optional
  • ifPresentOrElse() method that allows executing an action if the Optional is present or another action if not
  • stream() method for converting an Optional to a Stream

Here is the complete article for further reading.

15. Misuse of Optionals

Finally, let's see a tempting, however dangerous, way to use Optionals: passing an Optional parameter to a method.

Imagine we have a list of Person and we want a method to search through that list for people with a given name. Also, we would like that method to match entries with at least a certain age, if it's specified.

With this parameter being optional, we come with this method:

public static List search(List people, String name, Optional age) { // Null checks for people and name return people.stream() .filter(p -> p.getName().equals(name)) .filter(p -> p.getAge().get() >= age.orElse(0)) .collect(Collectors.toList()); }

Then we release our method, and another developer tries to use it:

someObject.search(people, "Peter", null);

Now the developer executes its code and gets a NullPointerException.There we are, having to null check our optional parameter, which defeats our initial purpose in wanting to avoid this kind of situation.

Here are some possibilities we could have done to handle it better:

public static List search(List people, String name, Integer age) { // Null checks for people and name final Integer ageFilter = age != null ? age : 0; return people.stream() .filter(p -> p.getName().equals(name)) .filter(p -> p.getAge().get() >= ageFilter) .collect(Collectors.toList()); }

There, the parameter's still optional, but we handle it in only one check.

Another possibility would have been to create two overloaded methods:

public static List search(List people, String name) { return doSearch(people, name, 0); } public static List search(List people, String name, int age) { return doSearch(people, name, age); } private static List doSearch(List people, String name, int age) { // Null checks for people and name return people.stream() .filter(p -> p.getName().equals(name)) .filter(p -> p.getAge().get().intValue() >= age) .collect(Collectors.toList()); }

That way we offer a clear API with two methods doing different things (though they share the implementation).

So, there are solutions to avoid using Optionals as method parameters. The intent of Java when releasing Optional was to use it as a return type, thus indicating that a method could return an empty value. As a matter of fact, the practice of using Optional as a method parameter is even discouraged by some code inspectors.

16. Optional and Serialization

As discussed above, Optional is meant to be used as a return type. Trying to use it as a field type is not recommended.

Additionally, using Optional in a serializable class will result in a NotSerializableException. Our article Java Optional as Return Type further addresses the issues with serialization.

And, in Using Optional With Jackson, we explain what happens when Optional fields are serialized, along with a few workarounds to achieve the desired results.

17. Conclusion

In this article, we covered most of the important features of Java 8 Optional class.

We briefly explored some reasons why we would choose to use Optional instead of explicit null checking and input validation.

นอกจากนี้เรายังได้เรียนรู้วิธีที่จะได้รับค่าของนั้นไม่บังคับหรือหนึ่งเริ่มต้นถ้าว่างกับได้รับ () , orElse ()และorElseGet ()วิธีการ (และเห็นความแตกต่างที่สำคัญระหว่างสองคนสุดท้าย)

จากนั้นเราก็เห็นว่าจะเปลี่ยนหรือกรองของเราเป็นตัวเลือกที่มีต่อแผนที่ () flatMap ()และตัวกรอง () เราได้พูดคุยถึงข้อเสนอที่เป็นตัวเลือกAPI ที่คล่องแคล่วเนื่องจากช่วยให้เราเชื่อมโยงวิธีการต่างๆได้อย่างง่ายดาย

ในที่สุดเราก็พบว่าเหตุใดการใช้ตัวเลือก s เป็นพารามิเตอร์วิธีการเป็นความคิดที่ไม่ดีและจะหลีกเลี่ยงได้อย่างไร

ซอร์สโค้ดสำหรับตัวอย่างทั้งหมดในบทความมีอยู่บน GitHub