Java Set is a collection of elements (Or objects) that contains no duplicate elements. Java Set is an interface that extends Collection interface. Unlike List, Java Set is NOT an ordered collection, it’s elements does NOT have a particular order. Java Set does NOT provide a control over the position where you can insert an element. You cannot access elements by their index and also search elements in the list.
In this section, we will discuss some of the important points about Java Set:
Java Set interface extends Collection interface. Collection interface extends Iterable interface. Some of the frequently used Set implementation classes are HashSet, LinkedHashSet, TreeSet, CopyOnWriteArraySet and ConcurrentSkipListSet. AbstractSet provides a skeletal implementation of the Set interface to reduce the effort in implementing Set.
In this section we will discuss some of the useful Java Set methods:
Unlike List, We cannot convert a Java Set into an array directly as it’s NOT implemented using an Array. So We cannot use Arrays class to get the view of array as set. We can follow another approach. We can convert an array into List using Arrays.asList() method, then use it to create a Set. By using this approach, we can covert a Java Array to Set in two ways. Let us discuss them one by one using one simple example. Approach-1 In this approach, first We need to create a List using given array and use it to create a Set as shown below.
import java.util.*;
public class ArrayToSet {
public static void main(String[] args) {
String[] vowels = {"a","e","i","o","u"};
Set<String> vowelsSet = new HashSet>(Arrays.asList(vowels));
System.out.println(vowelsSet);
/**
* Unlike List, Set is NOt backed by array,
* so we can do structural modification without any issues.
*/
vowelsSet.remove("e");
System.out.println(vowelsSet);
vowelsSet.clear();
System.out.println(vowelsSet);
}
}
Approach-2 In this approach, we do NOT use intermediate List to create a Set from an Array. First create an empty HashSet, then use Collections.addAll() to copy array elements into the given Set as shown below.
import java.util.*;
public class ArrayToSet2 {
public static void main(String[] args) {
String[] vowels = {"a","e","i","o","u"};
Set<String> vowelsSet = new HashSet<>();
Collections.addAll(vowelsSet, vowels);
System.out.println(vowelsSet);
/**
* Unlike List, Set is NOt backed by array,
* so we can do structural modification without any issues.
*/
vowelsSet.remove("e");
System.out.println(vowelsSet);
vowelsSet.clear();
System.out.println(vowelsSet);
}
}
Output:- When we run above two programs, we will get the same output as shown below.
[a, e, u, i, o]
[a, u, i, o]
[]
In this section, we will write a program to convert a Set of Strings into an Array of String using Set.toArray() method as shown below.
import java.util.*;
public class SetToArray {
public static void main(String[] args) {
Set<String< vowelsSet = new HashSet<>();
// add example
vowelsSet.add("a");
vowelsSet.add("e");
vowelsSet.add("i");
vowelsSet.add("o");
vowelsSet.add("u");
//convert Set to Array
String strArray[] = vowelsSet.toArray(new String[vowelsSet.size()]);
System.out.println(Arrays.toString(strArray));
}
}
Output:- When we run above program, we will get the following output as shown below.
[a, e, u, i, o]
As we know, Set (HashSet) does NOT support sorting elements directly. It stores and display it’s elements in random order. However, we have some approaches to sort it’s elements as shown below:
import java.util.*;
public class SetSortingExample {
public static void main(String[] args) {
Set<Integer> intsSet = new HashSet<>();
Random random = new Random();
for (int i = 0; i {return (o2-o1);});
System.out.println("Reverse Sorting: " + intsList2);
// Approach-3
Set<Integer> sortedSet = new TreeSet<>(intsSet);
System.out.println("Sorted Set: " + sortedSet);
}
}
Output:- When we run above program, we will see the following output.
[560, 864, 176, 657, 135, 103, 40, 123, 555, 589]
Natural Sorting: [40, 103, 123, 135, 176, 555, 560, 589, 657, 864]
Before Sorting: [560, 864, 176, 657, 135, 103, 40, 123, 555, 589]
Reverse Sorting: [864, 657, 589, 560, 555, 176, 135, 123, 103, 40]
Sorted Set: [40, 103, 123, 135, 176, 555, 560, 589, 657, 864]
Most common operations performed on Java Set are add, addAll, clear, size etc. Below is a simple Java Set example showing common method usage.
import java.util.*;
public class SetCommonOperations
{
public static void main(String args[])
{
Set<String> vowels= new HashSet<>();
//add example
vowels.add("A");
vowels.add("E");
vowels.add("I");
//We cannot insert elements based on index to a Set
System.out.println(vowels);
Set<String> set = new HashSet<>();
set.add("O");
set.add("U");
//appending set elements to letters
vowels.addAll(set);
System.out.println(vowels);
//clear example to empty the set
set.clear();
//size example
System.out.println("letters set size = " + vowels.size());
vowels.clear();
vowels.add("E"); vowels.add("E");vowels.add("I"); vowels.add("O");
System.out.println("Given set contains E element or not? = " + vowels.contains("E"));
}
}
Output:-
[A, E, I]
[A, E, U, I, O]
letters set size = 5
Given set contains E element or not? = true
Below is a simple example showing how to iterate over Java Set.
import java.util.*;
public class SetIteratorExample
{
public static void main(String[] args)
{
Set<Integer> set = new HashSet<>();
for(int i=0; i<5; i++)
set.add(i);
Iterator iterator = set.iterator();
//simple iteration
while(iterator.hasNext()){
int i = (int) iterator.next();
System.out.print(i + ", ");
}
System.out.println("\n" + set);
//modification of set using iterator
iterator = set.iterator();
while(iterator.hasNext()){
int x = (int) iterator.next();
if(x%2 ==0) iterator.remove();
}
System.out.println(set);
//changing set structure while iterating
iterator = set.iterator();
while(iterator.hasNext()){
//ConcurrentModificationException here
int x = (int) iterator.next();
if(x==1) set.add(10);
}
}
}
Below is a simple example showing how to convert a Java Set to Stream and perform some operations as per our requirements.
import java.util.*;
public class SetToStream {
public static void main(String[] args) {
Set<String> vowelsSet = new HashSet<>();
// add example
vowelsSet.add("a");
vowelsSet.add("e");
vowelsSet.add("i");
vowelsSet.add("o");
vowelsSet.add("u");
//convert set to stream
vowelsSet.stream().forEach(System.out::println);
}
}
Output:-
a
e
u
i
o
In Java SE 9 release, Oracle Corp is going to add some useful utility methods to Set interface. It’s better to understand them with some simple and useful examples. Please go through my tutorial at “Java SE 9: Set Factory Methods” to learn them. That’s all of a quick roundup on Set in Java. I hope these Java Set examples will help you in getting started with Set collection programming. Thank you for reading my tutorials. Please drop me a comment if you like my tutorials or have any issues or suggestions or any type errors.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
it looks sorting program is not proper. import java.util.*; public class SetSortingExample { public static void main(String[] args) { Set intsSet = new HashSet(); Random random = new Random(); for (int i = 0; i {return (o2-o1);}); System.out.println("Reverse Sorting: " + intsList2); // Approach-3 Set sortedSet = new TreeSet(intsSet); System.out.println("Sorted Set: " + sortedSet); } } where is approach 1 and 2? direct copy to ide is showing lots of errors
- Ganeshraj
Great tutorial! But I have one point still not understand Set intsSet = new HashSet(); Random random = new Random(); for (int i = 0; i {return (o2-o1);}); System.out.println("Reverse Sorting: " + intsList2); // Approach-3 Set sortedSet = new TreeSet(intsSet); System.out.println("Sorted Set: " + sortedSet); I can’t see // Approach-1 and // Approach-2, can input data, can you help to provide it? Thanks you for you post, it help me a lot!
- Phuc Vuu
The mentioned link is not accessible. Request you to provide the updated link.
- vsb
Code provided for Sorting wont compile Java Set Sorting
- Rahul
It was nice tutorial. I have one doubt, how set does not allow adding duplicate elements. I want some good information on this. Is it possible for you to post some information with some example on this? Thanks Ajay Madhav
- Ajay madhav
Very Clean Explanation Sir .Thanks for sharing .i have been Following your blog since one year great Blog
- Bala