查看“Java 8 Optional”的源代码
←
Java 8 Optional
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
=== 示例1 === <syntaxhighlight lang="java"> public class OptionalTest { private User getUser() { return null; } @Test public void test1() { User user = getUser(); String city = "未知"; if (user!=null) { Address address = user.getAddress(); if (address!=null) { city = address.getCity(); } } } @Test public void test2() { Optional<User> optionalUser = Optional.ofNullable(getUser()); String city = optionalUser .map(User::getAddress) .map(Address::getCity) .orElse("未知"); } } </syntaxhighlight> === 示例2 === <syntaxhighlight lang="java"> package org.example.converter.excel; import cn.hutool.core.date.DatePattern; import cn.hutool.core.date.LocalDateTimeUtil; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.GlobalConfiguration; import com.alibaba.excel.metadata.data.ReadCellData; import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.property.DateTimeFormatProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty; import org.apache.commons.lang3.StringUtils; import java.time.LocalDate; import java.util.Optional; public class LocalDateConverter implements Converter<LocalDate> { @Override public CellDataTypeEnum supportExcelTypeKey() { return CellDataTypeEnum.STRING; } @Override public Class<?> supportJavaTypeKey() { return LocalDate.class; } @Override public WriteCellData<?> convertToExcelData(LocalDate value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception { if (value == null) { return new WriteCellData<>(); } String format = Optional.ofNullable(contentProperty) .map(ExcelContentProperty::getDateTimeFormatProperty) .map(DateTimeFormatProperty::getFormat) .orElse(DatePattern.NORM_DATE_PATTERN); return new WriteCellData<>(LocalDateTimeUtil.format(value, format)); } @Override public LocalDate convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception { String stringValue = Optional.ofNullable(cellData) .map(ReadCellData::getStringValue) .orElse(null); if (StringUtils.isBlank(stringValue)) { return null; } return Optional.ofNullable(contentProperty) .map(ExcelContentProperty::getDateTimeFormatProperty) .map(DateTimeFormatProperty::getFormat) .filter(StringUtils::isNotBlank) .map(format -> LocalDateTimeUtil.parseDate(stringValue, format)) .orElse(null); } } </syntaxhighlight> === 示例3 === <syntaxhighlight lang="java"> package org.example.converter.excel; import cn.hutool.core.date.LocalDateTimeUtil; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.GlobalConfiguration; import com.alibaba.excel.metadata.data.ReadCellData; import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.property.DateTimeFormatProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty; import org.apache.commons.lang3.StringUtils; import java.time.LocalDate; import java.util.Optional; public class LocalDateConverter implements Converter<LocalDate> { @Override public CellDataTypeEnum supportExcelTypeKey() { return CellDataTypeEnum.STRING; } @Override public Class<?> supportJavaTypeKey() { return LocalDate.class; } @Override public WriteCellData<?> convertToExcelData(LocalDate value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception { return Optional.ofNullable(value) .flatMap(localDate -> Optional.ofNullable(contentProperty) .map(ExcelContentProperty::getDateTimeFormatProperty) .map(DateTimeFormatProperty::getFormat) .filter(StringUtils::isNotBlank) .map(format -> new WriteCellData<>(LocalDateTimeUtil.format(localDate, format)))) .orElse(new WriteCellData<>()); } @Override public LocalDate convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception { return Optional.ofNullable(cellData) .map(ReadCellData::getStringValue) .filter(StringUtils::isNotBlank) .flatMap(str -> Optional.ofNullable(contentProperty) .map(ExcelContentProperty::getDateTimeFormatProperty) .map(DateTimeFormatProperty::getFormat) .filter(StringUtils::isNotBlank) .map(format -> LocalDateTimeUtil.parseDate(str, format))) .orElse(null); } } </syntaxhighlight>
返回至
Java 8 Optional
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
Spring Boot 2 零基础入门
Spring Cloud
Spring Boot
设计模式之禅
VUE
Vuex
Maven
算法
技能树
Wireshark
IntelliJ IDEA
ElasticSearch
VirtualBox
软考
正则表达式
程序员精讲
软件设计师精讲
初级程序员 历年真题
C
SQL
Java
FFmpeg
Redis
Kafka
MySQL
Spring
Docker
JMeter
Apache
Linux
Windows
Git
ZooKeeper
设计模式
Python
MyBatis
软件
数学
PHP
IntelliJ IDEA
CS基础知识
网络
项目
未分类
MediaWiki
镜像
问题
健身
国债
英语
烹饪
常见术语
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息