This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

List of common elements  RSS feed

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • 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
  • 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)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Would you like to try a free sample? Today we are featuring tiny ads:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!