File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Parsing a List of Pojos Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Parsing a List of Pojos" Watch "Parsing a List of Pojos" New topic

Parsing a List of Pojos

Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
Hello Java experts,

A questions for you regarding pojos in a List.
I have a pojo called 'Role' which has 2 instances variables - int Count and String RoleType. I have a List of Roles, which I want to parse to find out if a particular RoleType is present in my list.

List<Roles> roleList = new ArrayList<Roles>();

What method if any can I use to find out if the RoleType, say 'Admin' is present in my roleList? If this was a List<String> I could have used the
contains() or indexOf() method to do this. I am stumped as it is a list of pojos.

Any ideas will be much appreciated!

Thanks in advance.
[ June 03, 2008: Message edited by: Meghna Bhardwaj ]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46352
I think you will have to go through the List with an iterator or for-each and check whether the String is equal.

Unless you have got a Role#equals() method which only uses the String.
Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230

One thing I might suggest here is to take the Count attribute out of the Roles object, since Count is not really part of the role. Once you do this, you could then create a HashMap whose key is a Roles. If you then define an equals and hashcode method for the Roles object (possibly based on the name), you'd have a pretty efficient lookup rather than a for-each iterator.

Also, just another point. If you do need to use a list, I would also suggest using a LinkedList rather than an array list. Linked lists are more efficient when not randomly accessing items in the list since they only need to store a pointer to the next item rather than being backed by an array which can be costly in terms of speed to resize dynamically.

Feel free to follow-up with any additional questions.

[ June 03, 2008: Message edited by: Jeff Storey ]

Jeff Storey
Software Developer
Nitesh Kant

Joined: Feb 25, 2007
Posts: 1638

Firstly, "Parsing a pojo" is really a wrong terminology. You are just accessing a POJO and not parsing it.

Megha: If this was a List<String> I could have used the
contains() or indexOf() method to do this. I am stumped as it is a list of pojos.

I would ask a question. Why do you think, you can only use the contains() and indexOf() methods for List<String> and not for List<RoleType>?
Both of these methods use equals() method to determine whether the object requested matches with any of those present in the list.
As Campbell has pointed out, if you have equals() method implemented in RoleType that checks for the String RoleType for equality, then you might as well use contains() for RoleType too. But the lob side will be that you have to have a RoleType instance to pass to contains(). If you do not already have one, then it is not really suggested to use contains() as you have to create a new instance for every search.
In such a case, you can follow Jeff's suggestion(Use HashMap) for a faster search.

apigee, a better way to API!
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Alternatively, you could use this method from Apache Commons Collections:

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Rob Spoor

Joined: Oct 27, 2005
Posts: 20273

I don't know. Sure that's a good method, but all it does is iterate over the collection and call evaluate on each object, then return true if evaluate returns true. Kind of like

You could write this yourself quite easily too, without the use of the Predicate object and the entire Commons Collections library. You can also use Generics, something none of the Commons libraries support:

How To Ask Questions How To Answer Questions
Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
Alright guys thank you very much. I have gotten enough pointers on how to go about this.

Appreciate it!

Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
also note (re: Jeff's suggestion) you need to guarantee that keys in a Map are non-mutable

Bill Shirley - bshirley -
if (Posts < 30) JavaRanchFAQ);
I agree. Here's the link:
subject: Parsing a List of Pojos
It's not a secret anymore!