How to find the intersection of two sets in Java

Source: Internet
Author: User

In Python, perhaps we don't have this annoyance, because Python has provided us with a way to intersection.

But in Java, we need to move our brains. The Collectionutils tool class of Apache is highly recommended here.

The method signature is as follows:

org.apache.commons.collections.intersection (Final Collection A, final Collection b)

So how does this approach come true? Here's an example of list

Public class Testintersection {

private static final Integer one = new in  Teger (1);

  1. Public static void main (string[] args) {

  2. //A set [A,a,b,b,b,c]   

  3.         list<string> a  = arrays.aslist ( "a" ,&NBSP; "a" ,&NBSP; "B" Span style= "Color:black" >,&NBSP; "B" ,  "B" ,&NBSP; "C"

  4. //b Set [A,b,b,c,c]   

  5. List<string> B = arrays.aslist ("A", "B" , "B", "C"   , "C");

  6. Map Mapa = Mapforcollection (a);

  7. Map MAPB = mapforcollection (b);

  8. //Add the elements that are not duplicated in the two collection, and then loop through the occurrences of the element in turn   

  9. Set set = new HashSet (a);

  10. Set.addall (b);

  11. list<string> result = new arraylist<string> ();

  12. for (Object obj:set) {

  13. for (int i = 0, m = math.min (Getcountsfrommap (obj, Mapa), Getcountsfrommap (obj, MAPB)); I < m; i++) {

  14. Result.add ((String) obj);

  15. }

  16. }

  17. //Look at the expected result is not [a,b,b,c]   

  18. SYSTEM.OUT.PRINTLN (result);

  19. }

  20. /**

  21. * Loop through the collection and count the number of occurrences of each element <br/>

  22. * Eventually return a map similar to {a:1,b:3,c:3}

  23.      * 

  24. * @param a

  25. * @return

  26.      */   

  27. Private static Map mapforcollection (Collection a) {

  28. Map map = new HashMap ();

  29. Iterator it = A.iterator ();

  30. while (It.hasnext ()) {

  31. Object obj = It.next ();

  32. Integer count = (integer) map.get (obj);

  33. if (count = = null) {

  34. //indicates that the element first appears   

  35. Map.put (obj, one);

  36. } Else {

  37. Map.put (obj, new Integer (count.intvalue () + 1));

  38. }

  39. }

  40. return  map;

  41. }

  42. Private static int getcountsfrommap (Object obj, map map) {

  43. Integer count = (integer) map.get (obj);

  44. return count! = NULL ? Count.intvalue (): 0  ;

  45. }

  46. }

 

As you can see, the elements are marked on two different sets, the number of occurrences of each element is noted, and the elements that are not duplicated in the two collection are extracted.

Take the least number of elements in both, and loop to add

How to find the intersection of two sets in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.