Java is a very popular programming language that provides many built-in functions to improve development efficiency. Among them, the Optional function can help programmers deal with null value problems more conveniently. In this article, we will explain how to use the Optional function in Java for null value handling.
What is Optional function?
In Java8, Optional is a class that represents a possibly empty container. If an Optional object contains a non-null value, the isPresent() method will return true, otherwise it will return false. The Optional class provides many utility methods and functional interfaces to make null value processing more convenient.
How to use Optional function?
First, we need to understand how to create an Optional object. We can use any of the three static methods of(), ofNullable(), and empty() to create Optional objects. Among them, the of() method can only receive non-null values. If a null value is passed in, a NullPointerException will be thrown; the ofNullable() method can accept any type of value, including null values. If a null value is passed in, it will return An empty Optional object; the empty() method directly returns an empty Optional object.
Optional<String> name = Optional.of("张三"); //创建一个包含非空值的Optional对象 Optional<String> emptyName = Optional.empty();//创建一个空的Optional对象 Optional<String> anotherName = Optional.ofNullable(null);//创建一个包含null值的Optional对象
We can use the get() method to get the value in the Optional object. If the value in the Optional object is empty, the get() method will throw NoSuchElementException. Therefore, before using the get() method, it is best to use the isPresent() method to make a judgment to ensure that the value in the Optional object is not empty.
Optional<String> name = Optional.of("张三"); if(name.isPresent()){ String n = name.get(); System.out.println(n); //输出"张三" }
If the value in the Optional object is empty, we can also use the orElse() method to set the default value. When the value in the Optional object is empty, the orElse() method will return the default value we set.
Optional<String> name = Optional.empty(); String n = name.orElse("未知"); //设置默认值为"未知" System.out.println(n); //输出"未知"
In addition to the orElse() method, we can also use the orElseGet() method and the orElseThrow() method. These methods are also used to deal with the null value problem in Optional objects.
The orElseGet() method receives a Supplier interface to generate a default value, instead of passing an already generated default value like the orElse() method.
Optional<String> name = Optional.empty(); String n = name.orElseGet(() -> "未知"); //使用Supplier接口生成默认值 System.out.println(n); //输出"未知"
orElseThrow() method is used to handle the exception to be thrown when the value in the Optional object is empty.
Optional<String> name = Optional.empty(); String n = name.orElseThrow(() -> new RuntimeException("值为空")); //抛出RuntimeException异常
In addition to the above methods, the Optional class also provides many other methods, such as map(), flatMap(), filter(), etc., which can help us handle null values in Optional objects more conveniently.
Optional<String> name = Optional.of("张三"); Optional<String> capitalizedName = name.map((value) -> value.toUpperCase()); //使用map()方法将值转换为大写 System.out.println(capitalizedName.get()); //输出"张三"
Summary
In this article, we introduced the Optional class in Java and how to use the Optional function for null value handling. Using Optional functions can make our code more defensive programming and avoid the uncertainty caused by null values. Of course, we also need to be careful when using the Optional class to avoid using it as a universal data type, otherwise the code will be unnecessarily bloated.
The above is the detailed content of How to use Optional function in Java for null value handling. For more information, please follow other related articles on the PHP Chinese website!