• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Parsing a List of Pojos

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alternatively, you could use this method from Apache Commons Collections:

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also note (re: Jeff's suggestion) you need to guarantee that keys in a Map are non-mutable
 
And then the entire population worshiped me like unto a god. Well, me and this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic