Thursday, January 31, 2019

HashMapofHashMapValue example

package com.concretepage;
import java.util.*;
import java.util.Map.Entry;

public class HashMapofHashMapValue {
public static void main(String[] args) {
HashMap hmap = new HashMap();
hmap.put("One", "ABCD"); hmap.put("Two", "EFGH");
hmap.put("Three", "IJKL");  hmap.put("Four", "MNOP");
HashMap<String, HashMap> hmapObject = new HashMap<>();
hmapObject.put("One-One", hmap);
System.out.println(hmapObject.get("One-One"));
Set<Entry<String, HashMap>> reference = hmapObject.entrySet();
Iterator<Entry<String, HashMap>> referenceIterator = reference.iterator();
while(referenceIterator.hasNext()) {
Entry<String, HashMap> entry = referenceIterator.next();
System.out.println(entry.getKey());
System.out.println(entry.getValue());
HashMap hmapObjectValue = entry.getValue();
System.out.print(hmapObjectValue.get("One")+" ");
System.out.print(hmapObjectValue.get("Two")+" ");
System.out.print(hmapObjectValue.get("Three")+" ");
System.out.print(hmapObjectValue.get("Four")+" ");
}
System.out.println();
HashMap hmapSecondObject = new HashMap();
hmapSecondObject.put("One", "ABCD");
hmapSecondObject.put("Two", "EFGH");
hmapSecondObject.put("Three","IJKL");
hmapSecondObject.put("Four", "MNOP");
HashMap<String, HashMap<String, ArrayList>> hmapObjectsSecondObject = new HashMap<>();
hmapObjectsSecondObject.put("One-One", hmapSecondObject);
System.out.println("hmapObjectsSecondObject Value is " + hmapObjectsSecondObject.get("One-One"));

HashMap hmapThirdObject = new LinkedHashMap<>();
List a1 = new ArrayList();
a1.add("ABCD");a1.add("EFGH");a1.add("IJKL");a1.add("MNOP");
hmapThirdObject.put("One", a1);hmapThirdObject.put("Two", a1);
hmapThirdObject.put("Three", a1);hmapThirdObject.put("Four", a1);
HashMap<String, HashMap<String, ArrayList>> hmapObjectsThirdObject = new HashMap<>();
hmapObjectsThirdObject.put("One-One", hmapThirdObject);
System.out.println("hmapObjectsThirdObject Value is " + hmapObjectsThirdObject.get("One-One"));
HashMap hmapObjectsThirdObjectReference = hmapObjectsThirdObject.get("One-One");
Set<Entry<String, ArrayList>> set = hmapObjectsThirdObjectReference.entrySet();
Iterator<Entry<String, ArrayList>> itr = set.iterator();
while(itr.hasNext()) {
Entry<String, ArrayList> itrReference = itr.next();
System.out.println(itrReference.getKey());
System.out.println(itrReference.getValue());
Iterator objectValue = itrReference.getValue().iterator();
while(objectValue.hasNext()) {
System.out.println(objectValue.next());
}
}
}
}

No comments:

Post a Comment