Computer >> Computer tutorials >  >> Programming >> Java

How can we implement a Custom HashSet in Java?


A HashSet implements Set interface which does not allow duplicate values. A HashSet is not synchronized and is not thread-safe. When we can add any duplicate element to a HashSet, the add() method returns false and does not allow to add a duplicate element to HashSet.

Syntax

public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, Serializable

In the below example, we can implement a Custom HashSet.

Example

import java.util.*;
public class CustomHashSetTest extends AbstractSet

Output

England
Australia
India