This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes List of common elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "List of common elements" Watch "List of common elements" New topic
Author

List of common elements

Shruthi Babu
Ranch Hand

Joined: May 04, 2007
Posts: 54
I have a hash map

with values as a arraylist..

I need to iterate the hashmap and find list of common elements among all the list available..

What is the best way to handle them

I need to store the common element in a list and remove the common elements from the original list..

I have no clue how to handle this..

any help is highly appreciated..
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
I would suggest that you keep all the elements in one list. Then check if the list contains the value already(a HashSet would be a better option). If it does contain the value copy that element into the common list. Meanwhile carry on the procedure with HashMap it's just that you would not have to iterate through all the pairs.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I like to do problems like this in sorted order, a holdover from my days when the data was all on tape with sequential reads only. If you can copy the list data into TreeSets and not mind losing duplicates you might be in business. Yesterday I posted my old favorite Master-Update-Merge algorithm. I have a variation for any number of inputs instead of just two and even built it into a little utility so I never have to write it again. Let me know if that sounds interesting.
[ June 28, 2007: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Shruthi Babu
Ranch Hand

Joined: May 04, 2007
Posts: 54
Thanks for the reply. Now I got into a different situation

I have a list of Objects and I am trying to overide the equals method in my object class to check for equality..

public boolean equals(Object o) {
System.out.println("Inside Resource Type");
ResourceType resourceType = null;

if (o instanceof ResourceType) {
resourceType = (ResourceType) o;

return ((resourceType != null) &&
((getId() == resourceType.getId())));
} else {
return false;
}

When I use resourcelist.contains(object) it is not calling the quals method in the resourceTypeImpl classe..

I see that since these classes where generated using EMF classes when the contains is invoked the

EObjectContainmentEList(EcoreEList).contains(Object) but it never invoked the equals which i have overidden in the object class


so it always returns false..

How to handle this scenario?

Basically my requirement is to get a common list of elements from various list.. the list of objects are generated by EMF classes ..

Any help is highly appreciated
 
 
subject: List of common elements
 
Similar Threads
Exceptions
Collections problem - reference by index and key?
PROCESSING ATTRIBUTES
creating my own select list
Linked List - inserting value to correct position