Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to extract specific items from an ArrayList?

Reply
Thread Tools

How to extract specific items from an ArrayList?

 
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      02-20-2007
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
 
Reply With Quote
 
 
 
 
Ook
Guest
Posts: n/a
 
      02-20-2007
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;


 
Reply With Quote
 
 
 
 
Mark Rafn
Guest
Posts: n/a
 
      02-20-2007
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.velocityreviews.com/forums/(E-Mail Removed) <http://www.dagon.net/>
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      02-20-2007
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
need help on how to extract specific data from access base into java script Shomy Java 0 02-02-2012 06:52 PM
Extract a specific function in C source code kittywindz2 C Programming 0 12-04-2010 11:25 AM
How do i extract vidios when winrar wont extract them??? help plzzzzzzzz smuttdog@sc.rr.com Computer Support 2 12-23-2007 07:03 AM
extract a random number of items from an array Josselin Ruby 9 02-26-2007 09:00 PM



Advertisments