File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Finding a Specific Object in an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Finding a Specific Object in an ArrayList" Watch "Finding a Specific Object in an ArrayList" New topic
Author

Finding a Specific Object in an ArrayList

Bob Harrison
Greenhorn

Joined: Apr 18, 2006
Posts: 12
I can't quite get my head around this.

I have a userVO object that has a field, messages that is an ArrayList (Messages) of message objects (MessagesVO) that have a messageID (integer) field. Is there a way that I can extract a specific message by it's messageID?

I've tried code such as:
int index = userVO.getMessages().indexOf(messagesVO.messageID);
messagesVO = (MessagesVO)userVO.getMessages().get(index);

but this doesn't work. Can someone point me in the right direction or tell me that it can't be done??

Thanks,

Bob


"In theory there is no difference between theory and practice. In practice there is."<br />Yogi Berra
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
use a HashMap()
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

You could indeed put the objects in a HashMap, using the id as the key. Another option is to write a method whose job it is to find one object by id; inside that method, put a "for" loop which examines each object in the list, in turn, and returns the one with the right id, or null if isn't found.


[Jess in Action][AskingGoodQuestions]
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 988
    
    1
Orrrrrrr....

You could override Object.equals() in your MessagesVO class so that it returns true iff the messageID values for the two messages are the same. *

Pro:
- ArrayList already uses equals() to check for inclusion.

Cons:
- You should also override hashcode(), so that it relies on nothing but messageID. This will keep your class consistent, even if you use MessageVO objects as keys in a HashMap.
- You'll have to instantiate a new MessageVO object with a given messageId just to see if a message with that ID already exists in the ArrayList. But it looks like you already did that in your sample code.



Then...


* I just reread items 7 and 8 on overriding equals() and hashcode() in Joshua Bloch's "Effective Java". Now that I have that hammer, every question looks like a nail.
[ June 06, 2006: Message edited by: Ryan McGuire ]
Bob Harrison
Greenhorn

Joined: Apr 18, 2006
Posts: 12
Thanks.

I'll give these a try and let you know what happened.

Cheers,

Bob
 
 
subject: Finding a Specific Object in an ArrayList
 
Similar Threads
synchronization question
Correctly handling javax.resource.spi.UnavailableException in an Inbound Connector
Alternative to Hashtable
complex one-to-many
about hashmap implementation of arraylist