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.
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 ]
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 ]
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.
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
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: