WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java Collections · Advanced · question 52 of 100

What is the purpose of the Set interface in Java Collections, and how do you implement it?

📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

In Java, the Set interface is used to define a collection that contains no duplicate elements. It extends the Collection interface and adds the constraint that duplicate elements are not allowed. The Set interface has several implementing classes, including HashSet, LinkedHashSet, and TreeSet.

To implement the Set interface, we can create a new class that implements the Set interface and provides the required methods. These methods include:

add(E e): adds the specified element to the set if it is not already present

remove(Object o): removes the specified element from the set if it is present

contains(Object o): returns true if the set contains the specified element

size(): returns the number of elements in the set

iterator(): returns an iterator over the elements in the set

Here’s an example implementation of a Set using HashSet:

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class MySet {
   private Set<String> set;

   public MySet() {
      set = new HashSet<String>();
   }

   public void add(String element) {
      set.add(element);
   }

   public void remove(String element) {
      set.remove(element);
   }

   public boolean contains(String element) {
      return set.contains(element);
   }

   public int size() {
      return set.size();
   }

   public Iterator<String> iterator() {
      return set.iterator();
   }
}

In this example, we create a new class called MySet that implements the Set interface. We use a HashSet to store the elements of the set. We provide implementations for the required methods, which simply delegate to the corresponding methods of the underlying HashSet object.

We can use this class to create a new set and add elements to it:

MySet mySet = new MySet();
mySet.add("apple");
mySet.add("banana");
mySet.add("orange");

We can check if an element is in the set:

if (mySet.contains("banana")) {
   System.out.println("My set contains banana");
}

We can remove an element from the set:

mySet.remove("orange");

And we can iterate over the elements in the set:

Iterator<String> it = mySet.iterator();
while (it.hasNext()) {
   String element = it.next();
   System.out.println(element);
}

Some use cases for implementing a custom Set might include:

Needing to implement a set with a specific ordering or performance characteristics that are not provided by the standard Set implementations. Needing to add additional functionality to the Set interface that is not provided by the standard implementations, such as bulk operations or set operations (e.g. union, intersection, difference). Needing to provide a specific implementation of the Set interface for use with a third-party library or framework that requires a custom implementation.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java Collections interview — then scores it.
📞 Practice Java Collections — free 15 min
📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

All 100 Java Collections questions · All topics