I am trying to build a program where I add running times to a specific location. I am then storing the location and time in a hashmap. As I get the running time, I add it to a LinkedList, and then try to put the newly-updated LinkedList at the hashmap's Key value. However, once I move onto a new location, the running times don't stay with their designed location, so all locations end up having the same running time. I'm not quite sure what I am doing wrong. Thank you for any help.
Example data: Location A: 45 sec, 43 secs, 36 secs Location B: 51 sec, 39 secs
Incorrect Output: Location A: 39 secs, 51 secs Location B: 39 secs, 51 secs
Correct Output: Location A: 36 secs, 43 secs, 45 secs Location B: 39 secs, 51 secs
HashMap h = new HashMap();
LinkedList times = new LinkedList();
LinkedList newTimes = new LinkedList();
public static void addInformation(HashMap h, LinkedList times, LinkedList
newTimes) {
String location = scanner.next();
Double time = scanner.nextDouble();
if (h.containsKey(location)){
for (int i = 0; i < newTimes.size(); i++){
times.add(newTimes.get(i));
}
times.add(time);
getFastTime(times);
h.get(location).add(location, times); // cannot resolve add method
}else{
newTimes.clear();
newTimes.add(time);
getFastTime(newTimes);
h.put(location, newTimes);
}
}
public static void printInformation(HashMap h) {
Set keySet = h.keySet();
for ( Object locationName : keySet) {
//Use the key to get each value. Repeat for each key.
System.out.println("Location =" + locationName + " Time =" +
h.get(locationName));
}
}
public static void getFastTime(LinkedList times){
times.sort(null);
}