java优化嵌套ifelse_使用Java 8流处理嵌套的if / else语句

本文介绍了如何使用Java 8的Stream API重构传统的嵌套if-else语句,以处理HashMap对象中根据'userid', 'userrole', 'username'优先级生成输出的场景。给出了两种解决方案,一种适用于至少有一个值非空的情况,另一种处理所有值可能为空的场景。" 132704288,19673616,VTK隐式数据集剪辑编程教程,"['VTK库', '数据剪辑', '图形处理', '计算机视觉', '可视化工具']
摘要由CSDN通过智能技术生成

我有一个条件,有一些检查要创建一个对象.

我正在使用Streams,我很难完成这项工作.

输入是具有键/值对的HashMap对象,输出应在下面.

| userrole | userid | username | output |

|------------|--------|----------|----------|

| "" (blank) | 111 | amathews | 111 |

| "" | | amathews | amathews |

| Admin | 111 | amathews | 111 |

| Admin | 111 | "" | 111 |

| Admin | | amathews | Admin |

优先级如下:userid> userrole> username.

每个HashMap对象都将包含userrole / username / userid作为键及其值以及其他键/值对.

在Java的早期版本中,我们将有一堆嵌套的if / else语句来完成此任务.

这是我的代码:

map.entrySet().stream()

.filter(e -> e.getValue() instanceof String || e.getValue() instanceof Integer)

.filter(e -> e.getKey().contains("userrole") || e.getKey().contains("userid") || e.getKey().contains("username") )

.map(e -> e.getValue())

.collect(Collectors.toList());

我知道我在Stream中编写map函数的方式也不正确.

如何在java 8中完成此操作?我不知道如何在这里添加嵌套的if / else部分.

编辑:对不起如果我没有准确说明问题.这是代码片段:

public List getUserActionList(Map map)

{

String userRole = map.get("userrole");

String userName = map.get("username");

String userId = map.get("userid");

String output = null;

// if userrole, userid and username are not null/empty, then output is userid

if(!checkForNullEmpty(userRole) && !checkForNullEmpty(userId) && !checkForNullEmpty(userName))

output = userId;

// if userrole and userid are null/empty and username is not empty/null, then output is username

else if(checkForNullEmpty(userRole) && checkForNullEmpty(userId) && !checkForNullEmpty(userName))

output = userName;

// if userid and username are null/empty and userrole is not empty/null, then output is userrole

else if(!checkForNullEmpty(userRole) && checkForNullEmpty(userId) && checkForNullEmpty(userName))

output = userRole;

List udList = new ArrayList<>();

// Add the map and output into a UserAction object

udList.add(new UserAction(map, output));

return udList;

}

根据表格,我在这里只处理了3个条件.所以这必须重构为使用java 8 Streams.我希望它现在有意义.

解决方法:

如果保证至少有一个值,你可以像这样重构它:

public List getUserActionList(Map map) {

return Stream.of("userid", "username", "userrole")

.map(map::get)

.filter(s -> !checkForNullEmpty(s))

.limit(1)

.map(output -> new UserAction(map, output))

.collect(Collectors.toList());

}

如果不能保证至少有一个值是非空的,那就有点丑陋,但也不是太糟糕:

public List getUserActionList(Map map) {

return Stream.of("userid", "username", "userrole")

.map(map::get)

.filter(s -> !checkForNullEmpty(s))

.limit(1)

.map(output -> new UserAction(map, output))

.map(Collections::singletonList)

.findFirst()

.orElseGet(() -> Arrays.asList(new UserAction(map, null)));

}

标签:java,java-8,java-stream

来源: https://codeday.me/bug/20190828/1755739.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值