Java 针对布尔值测试true或针对字符串测试notnull/empty
为了获得具有True或NotNull/NotEmpty值的节的名称,我从以下Java对象创建了一个映射,然后对其进行迭代Java 针对布尔值测试true或针对字符串测试notnull/empty,java,Java,为了获得具有True或NotNull/NotEmpty值的节的名称,我从以下Java对象创建了一个映射,然后对其进行迭代 public class Assessment { private Boolean section1Checkbox1; private Boolean section1Checkbox2; private Boolean section1Comments; private Boolean section2Checkbox1; private Boolean sectio
public class Assessment {
private Boolean section1Checkbox1;
private Boolean section1Checkbox2;
private Boolean section1Comments;
private Boolean section2Checkbox1;
private Boolean section2Checkbox2;
private Boolean section2Comments;
more sections.....
我已将对象转换为贴图,然后对其进行迭代:
Map<String, Object> map = oMapper.convertValue(needsAssessment, Map.class);
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
if (pair.getValue()==true||NotNull) {
// Get Section Name
String[] sectionName = pair.getKey().toString().split("(?=\\p{Upper})");
System.out.println(sectionName[0]);
}
}
Map Map=oMapper.convertValue(needsAssessment,Map.class);
迭代器it=map.entrySet().Iterator();
while(it.hasNext()){
Map.Entry对=(Map.Entry)it.next();
if(pair.getValue()==true | | NotNull){
//获取节名
String[]sectionName=pair.getKey().toString().split((?=\\p{Upper})”;
System.out.println(sectionName[0]);
}
}
pair.getValue()测试出错:
有没有办法测试true(如果是布尔值)和NotNull或Empty(如果是布尔值)
字符串)在一个语句中?
(还是更好的方法?)
此代码有效,谢谢@Lino:
Map<String, Object> map = oMapper.convertValue(assessment, Map.class);
System.out.println(map);
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
if (pair.getValue() instanceof Boolean) {
Boolean currentCheckbox = (Boolean) pair.getValue();
// Get Section/Subject Name
System.out.println(pair.getKey());
if (currentCheckbox) {
String[] sectionName = pair.getKey().toString().split("(?=\\p{Upper})");
System.out.println(sectionName[0]);
}
}
}
Map Map=oMapper.convertValue(评估,Map.class);
系统输出打印项次(map);
迭代器it=map.entrySet().Iterator();
while(it.hasNext()){
Map.Entry对=(Map.Entry)it.next();
if(pair.getValue()实例为布尔值){
Boolean currentCheckbox=(布尔)pair.getValue();
//获取节/主题名称
System.out.println(pair.getKey());
如果(当前复选框){
String[]sectionName=pair.getKey().toString().split((?=\\p{Upper})”;
System.out.println(sectionName[0]);
}
}
}
以下是一些代码,展示了使用lambdas和streams进行集合过滤和转换的更为惯用的Java 8+方法:
Map<String, Object> map = oMapper.convertValue(assessment, Map.class);
map.entrySet()
// stream all entries
.stream()
// filter by value being TRUE (this is null safe)
.filter((e) -> Boolean.TRUE.equals(e.getValue()))
// transform entry to key split by regex
.map(e -> e.getKey().split("(?=\\p{Upper})"))
// transform to first array item
.map(a -> a[0])
// print
.forEach(System.out::println);
Map Map=oMapper.convertValue(评估,Map.class);
map.entrySet()
//流式处理所有条目
.stream()
//按值筛选为TRUE(这是空安全的)
.filter((e)->Boolean.TRUE.equals(e.getValue())
//将条目转换为按正则表达式分割的键
.map(e->e.getKey().split((?=\\p{Upper})”)
//转换到第一个数组项
.map(a->a[0])
//印刷品
.forEach(System.out::println);
您的地图是否可以成为地图
?首先,不要使用原始类型。其次,使用instanceOf
检查它是否为布尔型
或字符串
,如果是,则使用(字符串)对.getValue()
或(布尔)对.getValue()
@Lino您能提供一个代码示例吗?尽可能避免直接使用迭代器,因为它们增加了不必要的复杂性。不太容易出错的备选方案是(Java5-7)和(Java8+)回答OP,请原谅这一评论@NiklasMertsch只是对您的编辑建议的一个简短评论。我理解你的建议,但我认为最好是对这个答案进行评论,而不是编辑。改进源代码比简单地改进文本要复杂一些,