user1924563
user1924563

Reputation: 41

Regarding value of Map to be of type Map internally

I have a class

private String patientId;
private String scriptinfo;
private Phone No.;


Now I have the hashMap..
HashMap hm = new HashMap(); 

Now I want that there should be single key of hashmap let say 'A' is the key which I will pass but the value is of again Map type, some thing like this..

hm.put ("A", <<value should be of Map type>>)

in that Map type value again I keep all the information like patientId,scriptinfo,Phone No. and I want patient id is to be the key of that Map, Please advise how to achieve this

Upvotes: 0

Views: 58

Answers (3)

amicngh
amicngh

Reputation: 7899

Design your class like below.

class MyClass{

private String patientId;
private String scriptinfo;
private String phoneNumber;
}

Then use this in Map.

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);

While getting MyClass information you can use something like this.

MyClass mc=hm.get("A").get("patientId");

Upvotes: 3

Nikolay Kuznetsov
Nikolay Kuznetsov

Reputation: 9579

Something like this:

HashMap<String, HashMap<Type1, Type2> hm = new HashMap<>(); 
HashMap<Type1, Type2> hm2 = new HashMap<>();
hm2.put(val1, val2);
hm.put ("A", hm2);

Upvotes: 0

Moritz Petersen
Moritz Petersen

Reputation: 13057

Map<String, Map<String, String>> hm = new HashMap<>();

Map<String, String> data = new HashMap<>();
data.put("patientId", patientId);
...
hm.put("A", data);

Upvotes: 0

Related Questions