Java 8 之 Optional
背景介紹:在我們實際開發中,經常會遇到各種 NullPointerException 報錯,為了避免此類問題,需要在業務程式碼上新增各種if判斷,使程式碼看起來很臃腫,而且很不美觀
使用Optional 提供的各種方法,能夠很好的解決上述的問題,下面介紹一下Optional的基礎API
準備工作:新建一個User類及Address類
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String id;
private String name;
private Address address;
}
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Address {
private String province;
private String city;
}
1.orElse
當value為null時,取value1,否則取value
User user = null;
// 1.user為null時
User user1 = new User("001", "user1", null);
User result = Optional.ofNullable(user).orElse(user1);
System.out.println(result); // 輸出 User(id=001, name=user1, address=null)
// 2.user 不為null時
user = new User("000","user",null);
result = Optional.ofNullable(user).orElse(user1);
System.out.println(result); //輸出 User(id=000, name=user, address=null)
2.orElseThrow
當value為null時,丟擲引數中指定的異常
User user = null;
// 寫法1
User result = Optional.ofNullable(user).orElseThrow(NullPointerException::new);
// 寫法2
User result = Optional.ofNullable(user).orElseThrow(()->new Exception("user為空"));
3.orElseGet
如果存在則返回該值,否則呼叫other值並返回該呼叫的結果。
User user = null;
User result = Optional.ofNullable(user).orElseGet(()->new User("1111","other",null));
System.out.println(result); // 輸出 User(id=1111, name=other, address=null)
4.isPresent
如果存在值,則返回true ,否則返回false
User user = null;
boolean present = Optional.ofNullable(user).isPresent();
System.out.println(present); // false
User user1 = new User();
boolean present1 = Optional.ofNullable(user1).isPresent();
System.out.println(present1); // true
5.ifPresent
如果 存在值,則執行定義程式碼,否則什麼也不做。
User user = null;
Optional<User> user1 = Optional.ofNullable(user);
user1.ifPresent(u -> System.out.println(u)); // 什麼都不輸出
User user = new User();
Optional<User> user1 = Optional.ofNullable(user);
user1.ifPresent(u -> System.out.println(u)); // User(id=null, name=null, address=null)
6.map
如果存在值,則對其應用提供的對映函式,如果結果為非 null,則返回描述結果的Optional 。否則返回一個空的Optional
Address address = new Address("浙江","杭州");
User user = new User();
user.setId("001");
user.setName("ll");
user.setAddress(address);
String s = Optional.ofNullable(user)
.map(u -> u.getAddress())
.map(address1 -> address1.getCity())
.orElseThrow(() -> new Exception("111"));
System.out.println(s); // 杭州
7.flatMap
如果存在值,則將提供的Optional函式應用於它,返回該結果,否則返回空Optional 。與map的不同是 flatMap提供的函式必需是Optional的
Address address = new Address("浙江","杭州");
User user = new User();
user.setId("001");
user.setName("ll");
user.setAddress(address)
Optional<String> s1 = Optional.ofNullable(user)
.flatMap(user1 -> Optional.of(user1.getAddress()))
.flatMap(address1 -> Optional.of(address1.getCity()));
System.out.println(s1.orElse("null")); // 杭州
8.filter
對 Optional 中包含的值進行過濾,如果包含的值滿足條件,那麼還是返回這個 Optional;否則返回 Optional.empty。
Address address = new Address("浙江","杭州");
User user = new User();
user.setId("001");
user.setName("ll");
user.setAddress(address);
String a = Optional.ofNullable(user)
.filter(user1 -> user1.getId().equals("001"))
.map(user1 -> user1.getId())
.orElse("null");
System.out.println(a); // 001
String b = Optional.ofNullable(user)
.filter(user1 -> user1.getId().equals("002"))
.map(user1 -> user1.getId())
.orElse("null");
System.out.println(b); // null