Site icon JavaGoal

How to add element in HashSet?

In this post, we will read how to add an element in HashSet. We have an add(E e) method that is used to add the element in HashSet. We already know, A HashSet can contain only unique elements. So let’s see how we can work with java hashset add() method.

add(E e) method

This add(E e) method is used to add the given element in HashSet. It adds the element if the element does not already exist. It returns a boolean value either true or false.

access modifier, The access modifier is public, So it can be accessible from everywhere.
return type, Its return type is boolean either returns true or false.
E, represents the type of elements in HashSet.
e, the element that wants to add-in HashSet.

 public boolean add(E e) 
{
     return map.put(e, PRESENT)==null;
}

The add(E e) method checks whether the element is already exists or not?
If the element already exists in HashSet, it doesn’t add the element and return false.
If the element is not existing then it adds the element in HashSet and returns true.

Let’s see the example of java hashset add() method in java.

import java.util.HashSet;
public class HashSetExample 
{
	public static void main(String[] args) 
	{
		HashSet<String> setOfNames = new HashSet<String>();
		setOfNames.add("Java");
		setOfNames.add("Goal");
		setOfNames.add("Website");
		
		// Adding Same element again
		boolean isSuccessFullyAdded = setOfNames.add("Java");
		System.out.println("Element added again = "+ isSuccessFullyAdded);
		
		System.out.println("Names from hashSet = "+setOfNames);
	}
}

Output: Element added again = false
Names from hashSet = [Java, Goal, Website]

We have learned in a recent post how does HashSet works internally. A HashSet internally uses HashMap to stores the elements. When we call add method of HashSet class it internally invokes the put(E e) method of HashMap. It compares the value based on equals() and hashCode() method. If we want to store an object of user-defined class then we must override the equals() and hashCode() method. If you are a beginner then you should read Why we override the hashCode and equals() method.

import java.util.HashSet;

class Student
{
	int rollNo;
	String className;
	String name;
	public int getRollNo() {
		return rollNo;
	}
	public void setRollNo(int rollNo) {
		this.rollNo = rollNo;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Student(int rollNo, String className, String name) {
		this.rollNo = rollNo;
		this.className = className;
		this.name = name;
	}
	
	@Override
    public boolean equals(Object obj) 
    {
        return this.getRollNo() == ((Student) obj).getRollNo();
    }
    
    @Override
    public int hashCode()
    {
        return rollNo;
    }
}

public class HashSetExample 
{
	public static void main(String[] args) 
	{
		HashSet<Student> hashSetOfStudent = new HashSet<Student>();
		hashSetOfStudent.add(new Student(1, "MCA", "Ram"));
		hashSetOfStudent.add(new Student(2, "MCA", "Krishana"));
		hashSetOfStudent.add(new Student(3, "MCA", "John"));
		hashSetOfStudent.add(new Student(2, "MCA", "Lie"));
		
		System.out.println("Size of HashSet: "+ hashSetOfStudent.size());
	}
}

Output: Size of HashSet: 3

Exit mobile version