I have an Integer ArrayList with elements e.g. 60,45,60,15 - These are activity minutes. Each of these elements have an associated Activity Names. E.g. Activity A has time of 60, Activity B has time of 45, etc.
How to write a function in Java which given integer value, can return Activity Name? Initially I thought HashMap was correct, but since minutes can be duplicate, it overwrites the previous value if its same and I am not interested in implementing a custom Map similar to MultiValueMap of Guava collections.
Any clues to implement this helps.
EDIT 1:
I tried implementing as suggested by Wasi - however, I am getting IndexOutOfBoundsException. I am expecting both activities from mapping60, 1 activity from mapping15, and 1 activity from mapping45. What am I doing wrong?
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MultiMapTest {
public static void main(String[] args) {
Map<Integer, List<String>> map = new HashMap<Integer, List<String>>();
List<String> activityMappings60 = new ArrayList<String>();
activityMappings60.add("Act 1 60min");
activityMappings60.add("Act 2 60min");
List<String> activityMappings15 = new ArrayList<String>();
activityMappings15.add("Act 3 sprint");
activityMappings15.add("Act 4 sprint");
List<String> activityMappings45 = new ArrayList<String>();
activityMappings45.add("Act 5 45min");
activityMappings45.add("Act 6 45min");
map.put(new Integer(60), activityMappings60 );
map.put(new Integer(15), activityMappings15 );
map.put(new Integer(45), activityMappings45 );
List<Integer> arrayInteger = Arrays.asList(60,60,15,45);
for(Integer i=0; i < arrayInteger.size();i++) {
System.out.println(map.get(arrayInteger.get(i)).get(i));
System.out.println(map.get(arrayInteger.get(i)).remove(i));
}
}
HashMap<Integer, ArrayList<String>>and it should work for you.activity and durationand fill them in an ArrayLsit, BUT, you need to solve the duplicate issue, even if you can have 2 records with same duration (say 60) and different activity names, then when giving duration 60 to the function which activity should be returned??Wasi Ahmadsol is good, you can take that concept and wrap it in a class with basic operation [add(), remove(), find()].