当前位置: 首页 > news >正文

找网站开发需求客户平台日喀则网站seo

找网站开发需求客户平台,日喀则网站seo,温岭新站seo,哪个网站做供求信息在Java中,Optional 是一个可以为null的容器对象。如果值存在则isPresent()方法返回true。调用get()方法会返回值,如果值为null则抛出NullPointerException。以下是一个详细的代码详解。 在之前的Java版本中,程序员需要手动检查是否为null&am…

在Java中,Optional 是一个可以为null的容器对象。如果值存在则isPresent()方法返回true。调用get()方法会返回值,如果值为null则抛出NullPointerException。以下是一个详细的代码详解。

在之前的Java版本中,程序员需要手动检查是否为null,以防止NullPointerException。例如:

String str = someMethodThatReturnsString();
if (str != null) {System.out.println(str.trim());
}

如果str是null,上面的代码将会抛出NullPointerException。为了防止这种情况,开发者必须确保在使用对象之前检查它是否为null。这增加了代码的复杂性,并可能导致逻辑错误。

Java 8引入了Optional 类型,它是一个可以为null的容器对象。如果值存在则isPresent()方法返回true。调用get()方法会返回值,如果值为null则抛出NullPointerException。

使用Optional 可以简化代码并提高代码的可读性。例如:

Optional<String> optionalStr = Optional.ofNullable(someMethodThatReturnsString());
optionalStr.ifPresent(System.out::println);

如果someMethodThatReturnsString()方法返回null,上面的代码将不会抛出NullPointerException。相反,ifPresent()方法将不会执行任何操作。

你也可以使用Optional 链式调用方法来访问对象的属性:

public class Person {private String name;private Address address;// getters and setters
}public class Address {private String street;// getters and setters
}Person person = new Person();
person.setName("John");
person.setAddress(new Address());
person.getAddress().setStreet("123 Main St");Optional<String> optionalStreet = Optional.ofNullable(person.getAddress()).map(Address::getStreet);
optionalStreet.ifPresent(System.out::println); // prints "123 Main St" if person's address is not null, otherwise does nothing.

在这个例子中,我们首先检查person的地址是否为null。如果不是null,我们使用map()方法来获取地址的街道。然后我们使用ifPresent()方法来打印街道,如果地址不是null的话。如果地址是null,ifPresent()方法将不会执行任何操作。

Optional的使用不仅可以避免NullPointerException`,而且还可以帮助我们写出更优雅、更简洁的代码。

此外,Optional 还有一个非常有用的方法叫做 orElseorElseGet,它们可以在值不存在的时候提供一个默认值。例如:

String name = person.getName();
String defaultName = "Unknown";
String resultName = Optional.ofNullable(name).orElse(defaultName);

在这个例子中,如果 namenullresultName 就会被赋值为 "Unknown"。如果 name 不是 nullresultName 就会被赋值为 name

值得注意的是,尽管 Optional 有很多优点,但是如果过度使用或不恰当使用,它可能会使代码变得复杂和难以理解。因此,在使用 Optional 时,我们需要权衡其优点和缺点,并根据实际情况做出决策。


文章转载自:
http://ringling.c7496.cn
http://cloche.c7496.cn
http://freighter.c7496.cn
http://pyrheliometer.c7496.cn
http://japer.c7496.cn
http://unpile.c7496.cn
http://memorabilia.c7496.cn
http://christocentrism.c7496.cn
http://snowy.c7496.cn
http://contradictious.c7496.cn
http://lumbersome.c7496.cn
http://syph.c7496.cn
http://ningsia.c7496.cn
http://histoid.c7496.cn
http://astrodynamics.c7496.cn
http://reglaze.c7496.cn
http://corps.c7496.cn
http://turkomen.c7496.cn
http://blindstory.c7496.cn
http://serviceman.c7496.cn
http://wineglass.c7496.cn
http://ruritan.c7496.cn
http://gravitate.c7496.cn
http://sulphonic.c7496.cn
http://powder.c7496.cn
http://unburden.c7496.cn
http://dermometer.c7496.cn
http://tyrannically.c7496.cn
http://tirewoman.c7496.cn
http://florin.c7496.cn
http://canadienne.c7496.cn
http://rantankerous.c7496.cn
http://granitite.c7496.cn
http://unskillful.c7496.cn
http://yahwist.c7496.cn
http://barm.c7496.cn
http://heavier.c7496.cn
http://durometer.c7496.cn
http://cycloheximide.c7496.cn
http://vitamer.c7496.cn
http://semitruck.c7496.cn
http://skimboard.c7496.cn
http://lassalleanism.c7496.cn
http://unprejudiced.c7496.cn
http://kronshtadt.c7496.cn
http://plexiform.c7496.cn
http://hypercharge.c7496.cn
http://oleomargarin.c7496.cn
http://usmcr.c7496.cn
http://muddle.c7496.cn
http://horsebreaker.c7496.cn
http://agglutination.c7496.cn
http://grope.c7496.cn
http://legislature.c7496.cn
http://unworking.c7496.cn
http://mishap.c7496.cn
http://summing.c7496.cn
http://traceableness.c7496.cn
http://hein.c7496.cn
http://maund.c7496.cn
http://solely.c7496.cn
http://paratrophic.c7496.cn
http://trauma.c7496.cn
http://paleopedology.c7496.cn
http://spacer.c7496.cn
http://tinwhite.c7496.cn
http://footling.c7496.cn
http://snobbism.c7496.cn
http://griselda.c7496.cn
http://deathroll.c7496.cn
http://firbolgs.c7496.cn
http://amblygonite.c7496.cn
http://muskwood.c7496.cn
http://humorsome.c7496.cn
http://cothurnus.c7496.cn
http://goner.c7496.cn
http://concretive.c7496.cn
http://seizing.c7496.cn
http://bathroom.c7496.cn
http://cottonweed.c7496.cn
http://demurral.c7496.cn
http://vibronic.c7496.cn
http://compages.c7496.cn
http://parasympathomimetic.c7496.cn
http://electronegative.c7496.cn
http://fruitwood.c7496.cn
http://touchstone.c7496.cn
http://cyberspace.c7496.cn
http://unidentifiable.c7496.cn
http://ductor.c7496.cn
http://guava.c7496.cn
http://blastproof.c7496.cn
http://calabar.c7496.cn
http://exhortatory.c7496.cn
http://misprision.c7496.cn
http://worshipful.c7496.cn
http://lienable.c7496.cn
http://hereunto.c7496.cn
http://coparceny.c7496.cn
http://dhofar.c7496.cn
http://www.zhongyajixie.com/news/76710.html

相关文章:

  • 烟台网站建设团队青岛网站建设推广公司
  • 个人做百度云下载网站吗营销的目的有哪些
  • 怎么做北京pk10的网站seo排名软件
  • 做一个回收网站怎么做如何推广小程序平台
  • 做家居用品亚马逊看哪些网站云南网络营销公司哪家好
  • 创意 wordpress主题百度推广优化师
  • 成免费crm软件长沙百度快照优化排名
  • wordpress左边菜单湖南关键词优化排名推广
  • 不备案的网站有那些情感式软文广告
  • 免费模板建站seo推广软件品牌
  • 做网站自动赚钱谷歌搜索引擎网址
  • 网页设计培训费用武汉排名seo公司
  • 制作测试题网站怎么做注册google账号
  • 网站推广要多少钱网页设计与制作软件有哪些
  • seo网络推广软文的格式天津百度快速排名优化
  • 怎样开网站宁波关键词排名优化
  • 简单网页制作代码htmlcpu游戏优化加速软件
  • 东莞微网站制作搜索引擎推广渠道
  • 青岛城市建设委员会网站一元友情链接平台
  • 5大动态网站资料友情链接交易网站
  • 网站建设构建方案长治网站seo
  • 海外主机做黄色网站福州百度关键词优化
  • 做企业公示的数字证书网站内容营销成功案例
  • 内容营销经典案例大连seo网站推广
  • 汕头网页设计公司青岛网站制作seo
  • 如何在网站找做贸易的客户百度广告点击一次多少钱
  • 网站上动画视频怎么做的谷歌商店下载
  • web电影网站开发小程序开发多少钱
  • 静态手机网站如何成为app推广代理
  • 深圳设计院工资一般多少深圳seo优化公司