Adding Object to HashSet

hashset add java
hashset get
hashset java 8
set and hashset
how hashset avoid duplicates
why hashset is not synchronized
hashset for custom class

I am trying to add an Object (Exception) to a Set, however it adds every Exception, eventhough some are duplicates. debug

In my case duplicates are Exceptions which have the same Detail message.

How do I properly add the Exceptions to the HashSet only if the Exception.getDetails() doesn't already exist?

Is there another approach than a HashSet?

Performance is a criteria here, quadratic solutions (O(n^2))are not an option.

Adding Object to HashSet, HashSet.add() method. public boolean add(E e). Parameters. e − This is the element to be added to this set. If Count already equals the capacity of the HashSet<T> object, the capacity is automatically adjusted to accommodate the new item. If Count is less than the capacity of the internal array, this method is an O(1) operation. If the HashSet<T> object must be resized, this method becomes an O(n) operation, where n is Count. Applies to

You need to override how the Execptions are compared so it recognises duplicates the way you want. You can't do this for a HashSet but you can for TreeSet e.g.

Set<Exception> exceptions = new TreeSet<>(Comparator.comparing(Object::toString));

This example compares the toString which is the exception type and message in most cases.

If you really want to use a HashSet you need to wrap the Exception in a class which implements hashCode and equals the way you want.

If all you care about is the type and message you can store just the toString of each exception

final Set<String> exceptions = new HashSet<>();

public void addException(Exception e) {

java.util.HashSet.add() Method, What happens if you add duplicate elements to a set? The Java.util.HashSet.add() method in Java HashSet is used to add a specific element into a HashSet. This method will add the element only if the specified element is not present in the HashSet else the function will return False if the element is already present in the HashSet. Syntax: Hash_Set.add(Object element)

You need to redefine equals and hashCode methods.

If the detail is a String you can redefine them as follow

public boolean equals(Object obj) {
   if (!(obj instanceof YourException)) {
     return false;
   return getDetail().equals(((YourException) obj).getDetail());

public int hashCode() {
   return getDetail().hashCode();

Consider this code as a base to program. You have to check for null values for example.

Once redefined equals and hashCode inserting YourException in a TreeSet is an operation done in O(log(n)) where n is the size of the set, from javadoc:

This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains).

If we try to insert duplicate values in a “Set”, what will happen? Do , implements the Set interface. The Set is a collection with no duplicates. This interface models the mathematical set abstraction. This hashset object can contain only unique integers. Adding Element in a HashSet. To add an element in a HashSet we will use its following member function,

Top 10 HashSet Java Interview Questions : Collections, Basically this method provide a facility to add elements to the HashSet object. A boolean returned value will indicate if the addition of element  The add method adds the specified element only if it is not already present in the set object. It returns true if the specified element was added to the set and returns false if the element was already present in the set object and was not added. 1 2

Set add() method in Java with Examples, We can add an element to a HashSet like: ? Whenever an object is passed to this method, the hash value gets calculated. Then, the  In hashset whenever we add the objects, before adding the object it will check the hashcode value of an object by overridding the hashcode If the calculated hashcode value is already available in the set, then it will call equals method and check the object which we are going to add is equal with the already available object in set.

Java HashSet tutorial, Java HashSet class is a member of Java collections framework. It implements the Set Creating a HashSet and adding new elements to it. The example This example shows how to create a HashSet of user defined objects. var uniqueObjs = new HashSet<SomeType>(yourOriginalList); Otherwise you could implement you own IEqualityComnparer-class and pass it to distinct. Note that with the Distinct() approach, you can also look for distinct property values of the object in the list: var uniqueNames = yourlist.Select(obj => obj.Name).Distinct(); and some more

  • implement correct hashCode() and equals() for your exceptions
  • Can you tell us why you are doing this?
  • To properly display on the frontend with my messageFactory
  • Actually, the to string is not enough, since I'll do further processing with it later. But your TreeSet solution with log(n) works like a charm.
  • @0x2E5 in that case you might consider assylias' suggestion of using Map<String, Exception> which is O(1) per add.