Not sure if the title makes sense. but I am currently stuck trying to get the last occurrence of an object in a list. Let's say the object is compromised of ages (integers) and names (strings). I'm trying to get the last occurrence of the age "18" within the list, and retrieve the name. How would I go about this?
Current block of code I'm working with (Sorry, had to water it down a lot):
private List<Callable<Void>> createTasks(ArrayList<NamesAndAges> namesAndAges) {
List<Callable<Void>> tasks = new ArrayList<Callable<Void>>();
int endOfIndex = 0, beginningOfIndex = 0;
for (NamesAndAges nameAndAge : namesAndAges)
{
if (endOfIndex >= minSize && endOfIndex == namesAndAges.lastIndexOf(???))
{
tasks.add(new addIntoDB(namesAndAges.subList(beginningOfIndex, endOfIndex)));
beginningofIndex = endOfIndex+1;
}
endOfIndex++;
}
return tasks;
}
I'm basically stuck on where the ??? are in the code. I thought I could go about it using lastIndexOf().