Showing posts with label HashSet. Show all posts
Showing posts with label HashSet. Show all posts

Sunday, December 8, 2019

What is the internal working of Set / HashSet in Java ?

Problem statement: Give an explanation for internal working of Set / HashSet in java.
Answer: Reference

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

What will be the output when you add String object in HashSet?

Problem statement: Given code is 
  1. public class Test {
  2. public static void main(String[] args) {
  3. HashSet<String> s = new HashSet<>();
  4. s.add("A");
  5. s.add("B");
  6. s.add("C");
  7. s.add("A");
  8.   System.out.println("Elements: \n" + s);
  9.   System.out.println("Size: " + s.size());
  10.   System.out.println("Objects: " + s);
  11. }
  12. }
Answer:
Elements: 
[A, B, C]
Size: 3
Objects: [A, B, C]

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...