fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.util.stream.*;
  5. import java.lang.*;
  6. import java.io.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. HashSet<A> ha1 = new HashSet<A>();
  14. ha1.add(new A(1));
  15. ha1.add(new A(2));
  16. ha1.add(new A(3));
  17. HashSet<A> ha2 = new HashSet<A>();
  18. ha2.add(new A(2));
  19. ha2.add(new A(3));
  20. ha2.add(new A(4));
  21. HashSet<B> hb =
  22. Stream
  23. .concat(ha1.stream(), ha2.stream())
  24. .map(a -> new B(a.x))
  25. .collect(Collectors.toCollection(HashSet::new));
  26. System.out.println(hb.size());
  27. }
  28. }
  29.  
  30.  
  31.  
  32. class A
  33. {
  34. int x;
  35. String y;
  36. A(int x) {this.x=x;}
  37. }
  38.  
  39. class B
  40. {
  41. int x;
  42. B(int x) {this.x=x;}
  43. public int hashCode() {return x;}
  44. public boolean equals(Object other) {
  45. if (other == null) return false;
  46. B b = (B)other;
  47. return b.x == x;
  48. }
  49. }
Success #stdin #stdout 0.09s 711680KB
stdin
Standard input is empty
stdout
4