if(null != obj1){
if(null != obje2){
// do something
}
}
boolean checkNotNull(Object obj){
return null == obj ? false : true;
}
void do(){
if(checkNotNull(obj1)){
if(checkNotNull(obj2)){
//do something
}
}
}
boolean checkNotBlank(Object obj){
return null != obj && !"".equals(obj) ? true : false;
}
void do(){
if(checkNotBlank(obj1)){
if(checkNotNull(obj2)){
//do something
}
}
}
class MyEntity{
int id;
String name;
String getName(){
return name;
}
}
// main
public class Test{
public static void main(String[] args)
final MyEntity myEntity = getMyEntity(false);
System.out.println(myEntity.getName());
}
private getMyEntity(boolean isSuc){
if(isSuc){
return new MyEntity();
}else{
return null;
}
}
}
// main
public class Test{
public static void main(String[] args)
final MyEntity myEntity = getMyEntity(false);
if(null != myEntity){
System.out.println(myEntity.getName());
}else{
System.out.println("ERROR");
}
}
}
if (person != null) {
Country country = person.getCountry();
if (country != null) {
Province province = country.getProvince();
if (province != null) {
address = province.getCity();
}
}
}
public class Test {
public static void main(String[] args) {
final String text = "Hallo world!";
Optional.ofNullable(text)//显示创建一个Optional壳
.map(Test::print)
.map(Test::print)
.ifPresent(System.out::println);
Optional.ofNullable(text)
.map(s ->{
System.out.println(s);
return s.substring(6);
})
.map(s -> null)//返回 null
.ifPresent(System.out::println);
}
// 打印并截取str[5]之后的字符串
private static String print(String str) {
System.out.println(str);
return str.substring(6);
}
}
//Consol 输出
//num1:Hallo world!
//num2:world!
//num3:
//num4:Hallo world!
public class Test {
public static void main(String[] args) {
final String text = "Hallo World!";
System.out.println(lowerCase(text));//方法一
lowerCase(null, System.out::println);//方法二
}
private static String lowerCase(String str) {
return Optional.ofNullable(str).map(s -> s.toLowerCase()).map(s->s.replace("world", "java")).orElse("NaN");
}
private static void lowerCase(String str, Consumer<String> consumer) {
consumer.accept(lowerCase(str));
}
}
//输出
//hallo java!
//NaN
import java.util.Optional;
public class Test {
public static void main(String[] args) {
System.out.println(Optional.ofNullable(new Person())
.map(x->x.country)
.map(x->x.provinec)
.map(x->x.city)
.map(x->x.name)
.orElse("unkonwn"));
}
}
class Person {
Country country;
}
class Country {
Province provinec;
}
class Province {
City city;
}
class City {
String name;
}
import java.util.Optional;
public class Test {
public static void main(String[] args) {
System.out.println(new Person()
.country.flatMap(x -> x.provinec)
.flatMap(Province::getCity)
.flatMap(x -> x.name)
.orElse("unkonwn"));
}
}
class Person {
Optional<Country> country = Optional.empty();
}
class Country {
Optional<Province> provinec;
}
class Province {
Optional<City> city;
Optional<City> getCity(){//用于::
return city;
}
}
class City {
Optional<String> name;
}
if(Province!= null){
City city = Province.getCity();
if(null != city && "guangzhou".equals(city.getName()){
System.out.println(city.getName());
}else{
System.out.println("unkonwn");
}
}
Optional.ofNullable(province)
.map(x->x.city)
.filter(x->"guangzhou".equals(x.getName()))
.map(x->x.name)
.orElse("unkonw");
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有