Pages

Friday, November 19, 2021

Java Set Add - Set.add() Adding values

1. Overview

In this tutorial, We'll learn how to add values to set in java. Set implementations are HashSet and LinkedHashSet and TreeSet.

In the next sections, how add() method is used from set class.
Java Set Add - Set.add() Adding values



Syntax:
boolean add(E e)

2. Java Set Add - HashSet.add() example


add() method of Set interface is used to add the given element into the collection HashSet. add() method returns a boolean value. If the value is already present in the map then it returns false.
true is returned if the value is not present in the set and value is added successfully to the set.

Example 1:
package com.javaprogramto.collections.set.add;

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

public class HashSetAddExample {

	public static void main(String[] args) {

		Set<Integer> set = new HashSet<>();

		boolean isAdded = set.add(10);
		System.out.println("Is 10 added? " + isAdded);

		isAdded = set.add(10);
		System.out.println("Is 10 added again? " + isAdded);
		
		isAdded = set.add(20);
		System.out.println("Is 20 added? " + isAdded);
		
		System.out.println("Set values : "+set);

	}
}

Output:
Is 10 added? true
Is 10 added again? false
Is 20 added? true
Set values : [20, 10]

Value 10 is added twice and add() method returned true when the first time 10 is added but when 10 is added the second time, add() is returned false.

The final set has only two values without duplicates.

3. Java Set Add - LinkedHashSet.add() example


Next example is using LinkedHashSet add() method. LinkedHashSet preserves the insertion order into the set.

Example 2:
package com.javaprogramto.collections.set.add;

import java.util.LinkedHashSet;
import java.util.Set;

public class LinkedHashSetAddExample {

	public static void main(String[] args) {

		Set<Integer> set = new LinkedHashSet<>();

		boolean isAdded = set.add(10);
		System.out.println("Is 10 added? " + isAdded);

		isAdded = set.add(10);
		System.out.println("Is 10 added again? " + isAdded);

		isAdded = set.add(20);
		isAdded = set.add(30);
		isAdded = set.add(40);

		System.out.println("LinkedHashSet values : " + set);

	}
}
Output:
Is 10 added? true
Is 10 added again? false
LinkedHashSet values : [10, 20, 30, 40]

4. Java Set Add - TreeSet.add() example


Last example using TreeSet.add() method. This add() method also works as same but when the values are added to the treeset, values are sorted in ascending order by default.

Look at the below example where added the integer values random.

Example 3:
import java.util.Set;
import java.util.TreeSet;

public class TreeSetAddExample {

	public static void main(String[] args) {

		Set<Integer> set = new TreeSet<>();

		set.add(20);
		set.add(10);
		set.add(90);
		set.add(50);
		set.add(300);

		System.out.println("TreeSet values are sorted : " + set);
	}
}
Output:
TreeSet values : [10, 20, 50, 90, 300]

5. Conclusion


In this article, we have seen the java set add() method on Set implementations.



No comments:

Post a Comment

Please do not add any spam links in the comments section.