• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parsing a List of Pojos

 
Meghna Bhardwaj
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Meghna,

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.

Jeff
[ June 03, 2008: Message edited by: Jeff Storey ]
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively, you could use this method from Apache Commons Collections:

 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Meghna Bhardwaj
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright guys thank you very much. I have gotten enough pointers on how to go about this.

Appreciate it!

Regards.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also note (re: Jeff's suggestion) you need to guarantee that keys in a Map are non-mutable
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic