How to extract specific items from an ArrayList?

Discussion in 'Java' started by =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Feb 20, 2007.

  1. Ook wrote:
    > I have an ArrayList of items, itemList. Item has a property author. I need a
    > list of items where the author is a specific author. The following code
    > works, but is there a better way to do this?
    >
    > ArrayList<Item> itemList1 = new ArrayList<Item>();
    > for( Item item: itemList)
    > {
    > if( author.equals( item.author() ))
    > {
    > itemList1.add( item );
    > }
    > }
    > return itemList1;


    Use a HashMap either instead or as supplement to the ArrayList.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Feb 20, 2007
    #1
    1. Advertising

  2. =?ISO-8859-1?Q?Arne_Vajh=F8j?=

    Ook Guest

    I have an ArrayList of items, itemList. Item has a property author. I need a
    list of items where the author is a specific author. The following code
    works, but is there a better way to do this?

    ArrayList<Item> itemList1 = new ArrayList<Item>();
    for( Item item: itemList)
    {
    if( author.equals( item.author() ))
    {
    itemList1.add( item );
    }
    }
    return itemList1;
     
    Ook, Feb 20, 2007
    #2
    1. Advertising

  3. =?ISO-8859-1?Q?Arne_Vajh=F8j?=

    Mark Rafn Guest

    Ook wrote:
    >I have an ArrayList of items, itemList. Item has a property author. I need a
    >list of items where the author is a specific author. The following code
    >works, but is there a better way to do this?


    >ArrayList<Item> itemList1 = new ArrayList<Item>();
    > for( Item item: itemList)
    > {
    > if( author.equals( item.author() ))
    > {
    > itemList1.add( item );
    > }
    > }
    >return itemList1;


    That's about all you can do if you don't want to write some code to make it
    faster. You could, instead of using ArrayList<Item>, define a class ItemList,
    which has a list of items, but also a hash of author to list of items, and any
    other cached sublists you want.
    --
    Mark Rafn <http://www.dagon.net/>
     
    Mark Rafn, Feb 20, 2007
    #3
  4. =?ISO-8859-1?Q?Arne_Vajh=F8j?=

    Daniel Pitts Guest

    On Feb 19, 5:49 pm, "Ook" <Ook Don't send me any freakin' spam at
    zootal dot com delete the Don't send me any freakin' spam> wrote:
    > I have an ArrayList of items, itemList. Item has a property author. I need a
    > list of items where the author is a specific author. The following code
    > works, but is there a better way to do this?
    >
    > ArrayList<Item> itemList1 = new ArrayList<Item>();
    > for( Item item: itemList)
    > {
    > if( author.equals( item.author() ))
    > {
    > itemList1.add( item );
    > }
    > }
    > return itemList1;


    That is one way to do it unless you build an index before hand:

    /* Initialize in the constructor.
    Probably to a HashMap, depending on your Author class. */
    private final Map<Author, Collection<Item>> itemsByAuthor;

    public void buildAuthorIndex(Collection<Item> items) {
    for (Item item: items) {
    Collection<Item> forAuthor = itemsByAuthor.get(item.author());
    if (forAuthor == null) {
    forAuthor = new ArrayList<Item>();
    itemsByAuthor.put(item.author(), forAuthor);
    }
    forAuthor.add(item);
    }
    }

    public Collection<Item> getItemsForAuthor(Author author) {
    Collection<Item> items = itemsByAuthor.get(author);
    if (itemsByAuthor == null) {
    return java.util.Collections.emptyList();
    /* You might also consider returning a new ArrayList instead.
    */
    }
    return items;
    }

    As you can see, this approach is a little bit more verbose, and
    requires more memory. It also assumes that once you've built your
    Item list, you aren't updating it (although, this can be easily
    accounted for).

    Use this approach only when: You find that your application is too
    slow, and a profiler points to your existing approach as being the
    culprit.

    Remember this basic tenet of Software Engineering: Make it work
    right, then make it work fast-enough.
     
    Daniel Pitts, Feb 20, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,744
    Natty Gur
    Aug 19, 2003
  2. Kaidi
    Replies:
    4
    Views:
    2,388
    Kaidi
    Jan 3, 2004
  3. xz
    Replies:
    16
    Views:
    2,380
  4. Philipp
    Replies:
    6
    Views:
    928
    Arne Vajhøj
    May 28, 2008
  5. Alessandro
    Replies:
    20
    Views:
    2,251
    Joshua Cranmer
    Apr 30, 2009
Loading...

Share This Page