• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need to know which collection to use

 
Celina Joseph
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is the scenario -
I have to store old batch ids(of type integer/long). And then I have to store new batch ids.
And then I have to compare the old and the new batch ids to come up with a list of batch ids found in new but not existing in the old.
I am not sure if I need to use collection to do this. But I thought Collection framework might have some APIs to achieve this without writing comparison code.
Please let me know a better way of coding this functionality.
Thanks.
 
Nilesh Pereira
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Collection would definitely help you here. All implementations will give you the ability to add items, traverse items, remove items, and check if an item already exists. The simplest Collection implementation is a Vector. You will have to wrap your primitive ids into Objects of type Integer/Long as java collections only deal with Objects.
If you don't need the old ids or the new ids but only need new ids not in old ids you could use only one Vector called newIdsNotInOldIds:
Everytime someone gives you a new id, put it in newIdsNotInOldIds.
Everytime someone gives you an old id, remove it from newIdsNotInOldIds.
This saves you the trouble of looping.
I hope this helps
 
Philip Shanks
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would use an approach that I learned in Perl scripting; use a data structure that represents a set of unique values. This allows you to check for existence without looping through the structure.
There are a few data structures that implement the java.util.Set interface; I would probably use the java.util.HashSet class.
I would put each collection into its own Set, and then identify those elements that exist outside of the intersection of the two sets; the Set interface supplies the containsAll(), removeAll() and retainAll() methods to aid in this.
Hope this helps.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!