• 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

List of common elements

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Shruthi Babu
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic