Java Set Tutorial with Examples – POFTUT

Java Set Tutorial with Examples

Java programming language provides the Set structure which implements array or list like collections. It is an unordered collection of objects where duplicate values cannot be stored. Objects can be generally string, integer or complex objects like classes. HashSet, LinkedHashSet and TreeSet structures implement the Set as base or ancestor.

Create Set

We will start by creating a set. We need to import the java.util.* in order to use Set. We will create a string set which can be added with add() function.

Create Set

Create Set

When we run the code given the added hash elements like below. We can see in the code that Ali is added twice but printed once because duplicate entries cannot be stored as set logic.

Create Set

Create Set

Create Integer Set

We can create an integer set. In this example, we will use digits but some of the multiple times. Multiply added digits will be stored as single. We need to create Integer sets like below.

LEARN MORE  How To Define Array In Programming Languages Java, JavaScript, PHP, C, C++, Python, C#, PowerShell?

We have added 1 and 2 multiple times but they are store only one time.

Create Integer Set

Create Integer Set

Create a String Set

We can also create a string set. Actually, we have created a string set in the previous examples but I wanted to express string set explicitly. Keep in mind that we need to set the set type as a string like below.

Remove Element From Set

After adding some elements we may need to remove some of them. We can use the remove() function by providing the object like string, integer etc. In this example, we will remove the element Ali from the set mySet.

Clear Set or Remove All Elements

We can also remove all elements from the given set with a single function. We can use clear() function which will clear the set. In this example, we will clear all elements of mySet.

Get Size of Set

A set can store a lot of elements. We can get the size of the set or the element count of the set with the size() function like below.

LEARN MORE  How To Define Array In Programming Languages Java, JavaScript, PHP, C, C++, Python, C#, PowerShell?

Union of Set

Union means joining multiple sets together. As sets cannot hold duplicate items we can union sets by adding them together. This will not create duplicate items in the new union set. We will use the addAll() function and the set we want to add. We will create a set named unionSet and add two sets named mySet1, mySet2.

The Intersection of Two Sets

The intersection of sets is a very popular and useful function to get common elements of two sets. We can use retainAll() function with the first set and provide the second set. In this example, we will see the intersection of sets mySet1 and mySet2 into set named intersectionSet.

The Difference Between  Two Sets

The difference is the elements that do not exist in the other set. We can find the difference from mySet1 from mySet2 by removing all elements of the mySet1 of mySet2 like below.

Check If Set Is Empty

We can check if the set is empty in different ways. But in this case, we will use the size() function if the set is empty. If the size is 0 this means the set is empty.

Check If Set Contains Given Element

Another important function checks if the given set contains the given element. We can use contains() function by providing the element or object we want to check. This function will return true if the given element or object exists in the given set and return false if not.

LEARN MORE  How To Define Array In Programming Languages Java, JavaScript, PHP, C, C++, Python, C#, PowerShell?

Iterate Over Set

In Java programming language Set base on Collections. Collections provide an array or list like features for example iteration. We can use iteration in Set too. we can use different loop statements like for, while, foreach etc.

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)