Wednesday, April 9, 2014

Storing HashKey and Values

import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

public class HashKeyandValues{
public static void main(String[] args) {

ConcurrentHashMap<CheckPojo, Integer> hm = new ConcurrentHashMap<CheckPojo,Integer>();
hm.put(new CheckPojo("1","One"), 99);
hm.put(new CheckPojo("2","Two"), 100);
Set<CheckPojo> s = hm.keySet();

Iterator itr= s.iterator();
while(itr.hasNext()){
System.out.println("Iterator.hasNext Value Is " + hm.get(itr.next()));
}

Iterator it = hm.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
CheckPojo key = (CheckPojo)entry.getKey();
Integer val = (Integer)entry.getValue();
System.out.println("key Is : " + key.getMethod1() +", " + key.getMethod2() + " and value Is : " + val);
}

}
}

beanClass Is

public class CheckPojo {
 
private String method1;
private String method2;

public CheckPojo(String method1, String method2){
this.method1=method1;
this.method2=method2;
}
public String getMethod1() {
return method1;
}
public String getMethod2() {
return method2;
}

}

Console Result Is : 

Iterator.hasNext Value Is 99
Iterator.hasNext Value Is 100
key Is : 1, One and value Is : 99
key Is : 2, Two and value Is : 100

No comments:

Post a Comment