In this tutorial we are going to use Set in Java for storing unique values. Set interface in Java can be used to store unique values within you program.
In this tutorial we are going to use Set in Java for storing unique values. Set interface in Java can be used to store unique values within you program.In this section we will discuss about the Set interface in Java which is designed to store unique values. We will show you how to store unique values in Java using Set? The Set interface in Java allows allows maintaining unique list in Java. Here in this example we will use HashSet to maintain list of of unique values in our program. First of all we will initialize the HashSet object and then add few values including duplicates. When we add duplicates the Set interface checks if the elements already exists if it does not already presents then it adds in the list. If value is already present then it skips and value is not added to the list. This way it maintains the unique values list in Java.
As explained earlier Set interface in Java is used to store the unique values and HashSet is one of the most popular implementation of this interface. The HashSet is part of core Java API and it provides many methods to perform operation on the values stored in the HashSet object.
HashSet is one of the fundamental data structure in Java and its main features are:
HashSet provides following important methods to perform various operations on the HashSet object:
We have seen some of the important methods of HashSet object. Now lets write simple program to store unique values in the HashSet object and then print the data. We are going to store same values two times and then print the data. When we iterate and print the data it only prints the unique values.
Here is the example program to store unique values in Java using HashSet:
package net.roseindia; // Web: https://www.roseindia.net import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class UniqueValues { public static void main(String[] args) { Setset = new HashSet (); set.add("One"); set.add("Two"); set.add("Three"); set.add("One"); set.add("Two"); set.add("Three"); Iterator itr = set.iterator(); while(itr.hasNext()){ String value = itr.next(); System.out.println(value); } } }
In the above tutorial we are instantiating the object of HashSet:
Setset = new HashSet ();
Then adding following values two times:
set.add("One"); set.add("Two"); set.add("Three");
Then printing the values stored in the HashSet object with the help of following code:
Iterator itr = set.iterator(); while(itr.hasNext()){ String value = itr.next(); System.out.println(value); }
Following is the output of the program:
One Two Three
From the above output of the program it is clear that the HashSet is stores unique values. If duplicate value is added multiple times, then it just ignores.
In this tutorial you have learned to store unique values in Java using the HashSet object. Keep in mind that the HashSet is not thread safe and you should use it with care in multi-threading environment.
Here are more tutorials related to HashSet:
Ads