首页 > 编程知识 正文

java创建空字符串,java定义一个空字符串

时间:2023-05-05 10:22:20 阅读:178560 作者:527

为了解决你的目标我尝试了一些事情,然后我意识到我会从VGR那里接受想法。 这是因为与使用现有方法相比,实现这个用例是更多的额外工作。

但是,在花了很长时间确认实现后,可以添加的细节很少-

作为实用程序,可以实现静态实现,以验证在字符串中输入的null和isEmpty条件,并相应地返回Optional。 这个代码是-

privatestaticoptionalofemptyable

returnisnullorempty(string )? optional.empty (: optional.of )字符串;

() ) ) ) )。

privatestaticbooleanisnullorempty {

return target==null|| target.isempty (;

() ) ) ) )。

然后,这可以替代ofNullable的用法,后者专门检查空(optional的主要目的)。

因为问句期望和可选方法一样实际处理每个方法(map/or /或Else )的调用,所以作为和OptionalInt同样的方法之一,可以将自定义OptionalString

publicfinalclassoptionalstring {

privatestaticfinaloptionalstringempty=newoptionalstring (;

私有文件布尔is present;

private final String value;

私有选项字符串

this.isPresent=false;

this.value=' ';

() ) ) ) )。

privatestaticoptionalstringempty {

return EMPTY;

() ) ) ) )。

私有布尔为present

返回is present;

() ) ) ) )。

私有选项字符串(字符串值)。

this.isPresent=true;

this.value=value;

() ) ) ) )。

publicstaticoptionalstringof (string value ) {

return value==null|| value.isempty (? 选项字符串. empty (: newoptionalstring ) value;

() ) ) ) )。

publicoptionalstringmap (functionsuperstring? extends String mapper ) {

返回! isPresent (? optional string.empty (: optional string.of ) mapper.apply (this.value );

() ) ) ) )。

publicoptionalstringor (supplier supplier ) {

return isPresent (? this :选项字符串. of (supplier.get );

() ) ) ) )。

stringorelse(stringother

return isPresent? 值: other;

() ) ) ) )。

公共字符串获取辅助(

returnoptional.of(value ).orElseThrow ()-newnosuchelementexception (' novaluepresent ' ) );

() ) ) ) )。

() ) ) ) )。

可以通过以下方式进一步实现用例-

stringcustomimpl=optional string.of (entity.getname () )

. map (可选字符空间) /可选字符空间(可选字符空间)

. or (服务:3360 getmostcommonname ) )。

. orelse(learning );

system.out.println (string.format (customimplementation-% s (,customimpl ) );

在哪里

privatestaticstringtrimwhitespaces {

return x.trim (;

() ) ) ) )。

注意-老实说,我找不到JDK没有OptionalString类背后的原因。 (我这么说是因为我怀疑背后一定有想法。 )。 这是因为我的触角半径小得多。 我希望有人能在这里增加更多细节。 对不起,你想要的东西看起来几乎都是用OptionalString的。 这会让我们回到循环的开始

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。