Java: autofiltering list?

Posted by Jason S on Stack Overflow See other posts from Stack Overflow or by Jason S
Published on 2010-06-03T14:08:58Z Indexed on 2010/06/03 14:24 UTC
Read the original article Hit count: 272

Filed under:
|

I have a series of items arriving which are used in one of my data structures, and I need a way to keep track of those items that are retained.

interface Item {}
class Foo implements Item { ... }
class Baz implements Item { ... }

class StateManager
{
    List<Foo> fooList;
    Map<Integer, Baz> bazMap;

    public List<Item> getItems();
}

What I want is that if I do the following:

for (int i = 0; i < SOME_LARGE_NUMBER; ++i)
{
    /* randomly do one of the following:
     * 1) put a new Foo somewhere in the fooList
     * 2) delete one or more members from the fooList
     * 3) put a new Baz somewhere in the bazMap
     * 4) delete one or more members from the bazMap
     */
}

then if I make a call to StateManager.getItems(), I want to return a list of those Foo and Baz items, which are found in the fooList and the bazMap, in the order they were added. Items that were deleted or displaced from fooList and bazMap should not be in the returned list.

How could I implement this? SOME_LARGE_NUMBER is large enough that I don't have the memory available to retain all the Foo and Baz items, and then filter them.

© Stack Overflow or respective owner

Related posts about java

Related posts about data-structures