Sunday, December 8, 2019

What will be the output when you add custom elements in HashSet?

Problem statement: Given code is
  1. import java.util.HashSet;
  2. class A {}
  3. class B {}
  4. class C {}
  5. public class Test {
  6. public static void main(String[] args) {
  7. HashSet set = new HashSet();
  8. set.add(new A());
  9. set.add(new B());
  10. set.add(new C());
  11. set.add(new A());
  12. System.out.println("Elements: \n" + set);
  13.   System.out.println("Size: " + set.size());
  14. }
  15. }
Answer:
Elements: 
[com.practice.core.C@7852e922, com.practice.core.A@15db9742, com.practice.core.B@6d06d69c, com.practice.core.A@4e25154f]
Size: 4

No comments:

Post a Comment

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...