Search code examples
javaarraylistcollectionshashmapiterator

Retrieving values from HashMap


I have my HashMap as below:

Map<String, List<Double>> map = new HashMap<String, List<Double>>();//map
Iterator<java.util.Map.Entry<String, List<Double>>> iterator = map.entrySet().iterator();
List<Double> times = new ArrayList<Double>();//arraylist

There is a String as a key and two double values. What I want to do is when I call the key I want to assign those two double values individually into two double variables.


Solution

  • Without any error checking (such as missing keys or malshaped List):

    List<Double> times = map.get("theKey");
    if(times.size() > 1){//To avoid the java.lang.ArrayIndexOutOfBoundsException
        double t1 = times.get(0);
        double t2 = times.get(1);
    }