Java 关于Map的值在内部为Map类型
我有一节课Java 关于Map的值在内部为Map类型,java,hashmap,Java,Hashmap,我有一节课 private String patientId; private String scriptinfo; private Phone No.; Now I have the hashMap.. HashMap hm = new HashMap(); 现在我希望hashmap应该只有一个键,比如说,A是我要传递的键,但是值 又是地图类型的,像这样的东西 hm.put ("A", <<value should be of Map type>>) hm.p
private String patientId;
private String scriptinfo;
private Phone No.;
Now I have the hashMap..
HashMap hm = new HashMap();
现在我希望hashmap应该只有一个键,比如说,A是我要传递的键,但是值
又是地图类型的,像这样的东西
hm.put ("A", <<value should be of Map type>>)
hm.put(“A”)
在该映射类型值中,我保留了所有信息,如patientId、scriptinfo、电话号码和我想要的患者id
是该映射的关键,请建议如何实现该映射Map hm=new HashMap();
Map<String, Map<String, String>> hm = new HashMap<>();
Map<String, String> data = new HashMap<>();
data.put("patientId", patientId);
...
hm.put("A", data);
映射数据=新的HashMap();
data.put(“patientId”,patientId);
...
hm.put(“A”,数据);
Map hm=new HashMap();
映射数据=新的HashMap();
data.put(“patientId”,patientId);
...
hm.put(“A”,数据);
类似这样的内容:
HashMap<String, HashMap<Type1, Type2> hm = new HashMap<>();
HashMap<Type1, Type2> hm2 = new HashMap<>();
hm2.put(val1, val2);
hm.put ("A", hm2);
HashMap类似这样的内容:
HashMap<String, HashMap<Type1, Type2> hm = new HashMap<>();
HashMap<Type1, Type2> hm2 = new HashMap<>();
hm2.put(val1, val2);
hm.put ("A", hm2);
HashMap像下面这样设计类
class MyClass{
private String patientId;
private String scriptinfo;
private String phoneNumber;
}
然后在地图上使用这个
Map<String, Map<String, MyClass>> hm = new HashMap<String,Map<String,MyClass>>();
Map<String, MyClass> data = new HashMap<String, MyClass>();
data.put(patientId, new MyClass(patientId,scriptinfo,phoneNumber));
...
hm.put("A", data);
像下面这样设计你的类
class MyClass{
private String patientId;
private String scriptinfo;
private String phoneNumber;
}
然后在地图上使用这个
Map<String, Map<String, MyClass>> hm = new HashMap<String,Map<String,MyClass>>();
Map<String, MyClass> data = new HashMap<String, MyClass>();
data.put(patientId, new MyClass(patientId,scriptinfo,phoneNumber));
...
hm.put("A", data);
这不是有点过分了吗?在这种情况下,hm
应该被定义为Map
@MoritzPetersen,我同意你的观点,但poster希望采用这种方法。这不是有点过分了吗?在这种情况下,hm
应该定义为Map
@MoritzPetersen,我同意你的看法,但poster希望采用这种方法。