• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to find the common part of two sets ?  RSS feed

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have two HashSet, how to find the common part and return it as a new HashSet ? IS there any such API method ?
 
Sheriff
Posts: 9082
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that the containsAll() method may be what you're looking for.
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no single API function, but you can write a trivial function yourself to do it:

If you remove the requirement that the intersection be a brand new HashSet, you can do it in one line by just calling s1.retainAll(s2) .
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!