Hello Preethi
I've derived the following class from HashMap. It's constructor might be useful to you:
public class HashMapAuto <K, V> extends HashMap{
@SuppressWarnings("unchecked")
public HashMapAuto(List<K> keys, List<V> values) throws Exception{
super();
if (keys.size() != values.size()) throw new Exception("'keys' list and 'values' list differs in size");
for (int i = 0; i < keys.size(); i++){
Object newKey = keys.get(i);
Object newValue = values.get(i);
this.put(newKey, newValue);
}
}
}
You can use it as a normal HashMap and put it in HashMap variables. The constructor is the nice part. If you have some lists, say ArrayList instances, for the keys and values respectively, then you can pass them to the constructor of HashMapAuto.

For example:
ArrayList<String> keys = createKeys();
ArrayList<String> values = createValues();
try{
HashMap<String, String> test = new HashMapAuto<String, String>(keys, values);
} catch (Exception e){
System.out.println(e.getMessage());
}
Now, you can use test as a normal HashMap.
I hope this helps. This is the first time that I used generics and I used the SuppressWarnings annotation. I'm not very happy with this. Maybe someone can show me a better may of using generics when extending classes.
However, you are not limited to strings. You can use any class that you desire. Good luck Preethi.
