Tech and Media Labs
This site uses cookies to improve the user experience.

Java Collections - Set

Jakob Jenkov
Last update: 2014-06-23

The java.util.Set interface is a subtype of the java.util.Collection interface. It represents set of objects, meaning each element can only exists once in a Set.

Java Set Example

Here is first a simple Java Set example to give you a feel for how sets work:

Set setA = new HashSet();

String element = "element 1";


System.out.println( setA.contains(element) );

This example creates a HashSet which is one of the classes in the Java APIs that implement the Set interface. Then it adds a string object to the set, and finally it checks if the set contains the element just added.

Set Implementations

Being a Collection subtype all methods in the Collection interface are also available in the Set interface.

Since Set is an interface you need to instantiate a concrete implementation of the interface in order to use it. You can choose between the following Set implementations in the Java Collections API:

  • java.util.EnumSet
  • java.util.HashSet
  • java.util.LinkedHashSet
  • java.util.TreeSet

Each of these Set implementations behaves a little differently with respect to the order of the elements when iterating the Set, and the time (big O notation) it takes to insert and access elements in the sets.

HashSet is backed by a HashMap. It makes no guarantees about the sequence of the elements when you iterate them.

LinkedHashSet differs from HashSet by guaranteeing that the order of the elements during iteration is the same as the order they were inserted into the LinkedHashSet. Reinserting an element that is already in the LinkedHashSet does not change this order.

TreeSet also guarantees the order of the elements when iterated, but the order is the sorting order of the elements. In other words, the order in which the elements whould be sorted if you used a Collections.sort() on a List or array containing these elements. This order is determined either by their natural order (if they implement Comparable), or by a specific Comparator implementation.

There are also Set implementations in the java.util.concurrent package, but I will leave the concurrency utilities out of this tutorial.

Here are a few examples of how to create a Set instance:

Set setA = new EnumSet();
Set setB = new HashSet();
Set setC = new LinkedHashSet();
Set setD = new TreeSet();

Adding and Accessing Elements

To add elements to a Set you call its add() method. This method is inherited from the Collection interface. Here are a few examples:

Set setA = new HashSet();

setA.add("element 1");
setA.add("element 2");
setA.add("element 3");

The three add() calls add a String instance to the set.

When iterating the elements in the Set the order of the elements depends on what Set implementation you use, as mentioned earlier. Here is an iteration example:

Set setA = new HashSet();

setA.add("element 0");
setA.add("element 1");
setA.add("element 2");

//access via Iterator
Iterator iterator = setA.iterator();
  String element = (String);

//access via new for-loop
for(Object object : setA) {
    String element = (String) object;

Removing Elements

You remove elements by calling the remove(Object o) method. There is no way to remove an object based on index in a Set, since the order of the elements depends on the Set implementation.

Generic Sets

By default you can put any Object into a Set, but from Java 5, Java Generics makes it possible to limit the types of object you can insert into a Set. Here is an example:

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

This Set can now only have MyObject instances inserted into it. You can then access and iterate its elements without casting them. Here is how it looks:

for(MyObject anObject : set){
   //do someting to anObject...

For more information about Java Generics, see the Java Generics Tutorial.

More Details in the JavaDoc

There is a lot more you can do with a Set, but you will have to check out the JavaDoc for more details. This text focused on the two most common operations: Adding / removing elements, and iterating the elements.

Jakob Jenkov

Copyright  Jenkov Aps
Close TOC