• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting specific Objects out of a collection  RSS feed

 
Tim Sparg
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

I have question about collections, and getting a particular object or objects out of a collection

if I define a class of type Column, which had a Boolean Primary Key indicator.

I have a table user ( usercode, name, password, contactdetails ) of which usercode and name are primary keys.

Now if I populate a collection of Columns with the Columns from the table, (Keeping in mind that i have indicated which Columns are primary key columns) is there anyway that i could get only the primary key containing columns out of my collections.

My best guess would be implementing the Iterator interface, and then checking each Column object for the PrimaryKey indicator to be true, and then returning the next true value.

Is that the best answer?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, not implementing Iterator yourself; the Collection (a List?) would have an iterator() method to hand you an Iterator object, and you'd just use that, something like

 
Tim Sparg
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry i was being specifically abstract about which type of collection i would use, as i wanted to hear what people had to suggest.

What you have suggested is similar to what i was thinking, except it moves the iteration from the behind the scenes (ie an Iterator class) to out in the open.
I suppose there is no way of getting round the fact that you would have to iterate through the list to get the Objects matching through the criteria that you want.

In that case would it not then be more efficient to to create a separate collection for the primary Keys.
As i understand it there would be little additional memory cost in doing this. and you would subsequently save on actually iterating through the main collection looking for those specific entries.

any thoughts?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether it's more efficient would depend on how often the collection gets updated, and how often you need to extract those specific elements.

Also keep in mind that maintaining two collections and keeping them in synch requires more complex code, with a higher likeliness of a bug being introduced. I would only do it if I was very sure that this is a critical performance bottleneck.

By the way, Jakarta Commons Collections has a nice utility method for what you want to do: http://commons.apache.org/collections/api-release/org/apache/commons/collections/CollectionUtils.html#select(java.util.Collection,%20org.apache.commons.collections.Predicate)
 
Tim Sparg
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ilja

This was definitely the kind of thing i was looking for
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!