-1

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().

Racco Taco
  • 15
  • 7
  • This isn't a question about Java so much as a question of how to write a program, isn't it? Can you show us what you've tried? – scottb Jan 10 '17 at 20:44
  • Reverse the List of objects and then traverse the List using for loop , `break` out of `for loop` on first occurrence of `age = 18`. If you provide some code i can give a better solution. – jack jay Jan 10 '17 at 20:45
  • @scottb Oh, I thought it was a one liner. I was looking at the lastIndexOf() to see if there's a way to go about it using that. If it's longer than just one to two lines, then I guess I need to write a whole new function or class to go about it. My bad. – Racco Taco Jan 10 '17 at 20:49
  • If what is a one liner? You've not even showed us any code for the object you're working with. – scottb Jan 10 '17 at 20:52
  • @scottb Added the small chunk of code. I'll be executing the tasks at once to add into a database. I had it where a task would be added every 20 entries, but if a name and age matched exactly, I would get a primary key error due to the threads executing at once. Hence, if I ordered them all by ages and cut the tasks to be executed at age 22, 23, etc (then went through and removed duplicates per task), the error shouldn't occur. – Racco Taco Jan 10 '17 at 21:07
  • I'm also trying to keep the minSize to 150 right now -- it can go over, but I want to try to find an efficient way since there can be over 100,000 entries. – Racco Taco Jan 10 '17 at 21:10
  • Oh. So far, only viable solution is iterating forward. Not sure if it's the best solution, though. – Racco Taco Jan 10 '17 at 21:23

2 Answers2

0

Based on this answer, if you have a list, you can start your iterator at the end of the collection and iterate backwards:

// Substitute appropriate type.
ArrayList<MyItem> a = new ArrayList<MyItem>();

// add your elements...

// Generate an iterator. Start after the last element.
ListIterator li = a.listIterator(a.size());

// Iterate in reverse.
MyItem mi = null;
while(li.hasPrevious()) {
    mi = li.previous();
    if(mi.getAge().equals("18")) { // obviously, your test will likely differ...
        break;
    }
}
Community
  • 1
  • 1
TayTay
  • 6,882
  • 4
  • 44
  • 65
0

Try something like this.

    ArrayList<Integer> a = new ArrayList<Integer>();

    // add your elements...
    for(int i = 1; i < 19; i++)
    {
        a.add(i);
    }


    while(a.size() > 0) 
    {            
        System.out.println("last item: " + a.get(a.size() - 1));//print last item
        a.remove(a.size() - 1);//remove last item
    }
SedJ601
  • 12,173
  • 3
  • 41
  • 59