Java Hashmap的类型系统是什么?
我想用hashmap存储实体的属性。该值是内置的Java Hashmap的类型系统是什么?,java,hashmap,drools,Java,Hashmap,Drools,我想用hashmap存储实体的属性。该值是内置的int或字符串的列表 name : "John Smith" attributes: "seniority" : (int) 7 "tags" : List<String>("asst_prof","cs_dept") "another_attrib" : (int) 3 你的建议是一个例子。不幸的是,Java不支持这些 您需要使用Map并将值强制转换为整数(int)或自己列出。您不能。要么使用存储和返回值作为对象的
int
或字符串的列表
name : "John Smith"
attributes:
"seniority" : (int) 7
"tags" : List<String>("asst_prof","cs_dept")
"another_attrib" : (int) 3
你的建议是一个例子。不幸的是,Java不支持这些
您需要使用Map并将值强制转换为整数(int)或自己列出。您不能。要么使用存储和返回值作为对象的映射的基本形式,然后您必须自己强制转换它们:
Object value = map.get(key);
if (value instanceof List<String>) {
List<String> myList = (List<String>) value;
}
Object value=map.get(键);
if(列表的值实例){
List myList=(List)值;
}
对于int,您不能存储基元类型int,但它将自动装箱为整数。因此,您必须检查instanceof
Integer,然后在Integer
对象上调用.intValue()
要获得作为对象返回的对象,则必须使用泛型,但不能混合类型。因此,您必须创建一个列表
属性映射和另一个int属性映射。在Drools中,您可以根据需要禁用特定类型的编译时类型安全性。在这种情况下,Drools将作为一种动态类型化语言工作,并将在运行时解析给定类型的类型。例如:
declare Person
@typesafe(false)
end
rule X
when
Person( attributes["seniority"] == 7 ) // resolving seniority to Number
...
rule Y
when
Person( attributes["tags"].size() > 1 ) // resolving tags to List
...
declare Person
@typesafe(false)
end
rule X
when
Person( attributes["seniority"] == 7 ) // resolving seniority to Number
...
rule Y
when
Person( attributes["tags"].size() > 1 ) // resolving tags to List
...