java – 返回值的反转
发布时间:2020-12-15 04:13:06 所属栏目:Java 来源:网络整理
导读:我有一个标志,我想传递给一个函数,该函数根据map中的值返回true或false: // userList is a ListString and is stored as the value field in a map// user is a Stringif(flag){ if (userList == null) return false; else if(userList.size() == 0) return
我有一个标志,我想传递给一个函数,该函数根据map中的值返回true或false:
// userList is a List<String> and is stored as the value field in a map // user is a String if(flag) { if (userList == null) return false; else if(userList.size() == 0) return true; return userList.contains(user); } else { if (userList == null) return true; else if(userList.size() == 0) return false; return !userList.contains(user); } 我的问题是:无论如何要整理这个代码,有很多复制(if和else块是相同的,除了它们的返回值是彼此相反的). 我不是一个非常有经验的代码,我真的很感激一些指导! 解决方法
使用标志值而不是常量.
if (userList == null) return !flag; else if(userList.size() == 0) return flag; XOR将用于最后的陈述(留给读者的练习:-p) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- java – 使用HttpURLConnection下载html时的奇怪行为
- 多线程--协程
- java – 如何使用RESTlet在RESTful Web服务中强制执行“会话
- 如何在JAVA中的HttpURLConnection中发送PUT,DELETE HTTP请求
- Spring boot @ModelAttribute标注的实现
- 是否有相当于Colander / DictShield for Java / Scala?
- 通过淘宝IP地址库获取IP位置
- java – @Before在TestRule中的方法不被调用
- java – OutOfMemoryError – 如何在创建hprof后终止JVM
- 做一个“内存转储”的java应用程序?