Mon Dec 28, 2009 7:58 pm
import java.util.HashMap;
import java.util.Map;
public class TestHashMap {
public static void main(String args[])
{
TestHashMap hashMap = new TestHashMap();
HashMap<String,Integer> oldhashMap = new HashMap<String, Integer>();
oldhashMap.put("a",1);
oldhashMap.put("2", 2);
HashMap<Integer,String> map = hashMap.copyHashMap(oldhashMap);
System.out.println("Value of key =1 is : "+map.get(1));
}
public <V, K> HashMap<V, K> copyHashMap(HashMap<K,V> oldhashMap)
{
HashMap<V, K> newhashMap = new HashMap<V,K>();
for(Map.Entry<K, V> entry :oldhashMap.entrySet())
{
newhashMap.put(entry.getValue(),entry.getKey());
}
return newhashMap;
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.