【发布时间】:2021-03-05 01:50:20
【问题描述】:
我有一个需要在函数中过滤的 HashMap:
public void filter(filterOption1, filterOption2, filterOption3, filterOption4) {
//Filter map in here
...
}
过滤器选项filterOption1,filterOption2,filterOption3,filterOption4 在运行时可能为空,而我希望避免的是:
public void filter(filterOption1, filterOption2, filterOption3, filterOption4) {
if(filterOption1 != null && filterOption2 == null && filterOption3 == null && filterOption4 == null) {
// Filter map values on filterOption1
} else if(filterOption1 != null && filterOption2 != null && filterOption3 == null && filterOption4 == null) {
// Filter map values on filterOption1 and filterOption2
} else if ... // And so on
}
有没有办法通过map.stream() 的巧妙过滤来避免链接 16 个 if 语句?
【问题讨论】:
标签: java collections hashmap java-stream filtering