/** * Created by PhpStorm. * User: Rahul * Date: 12/8/2016 * Time: 8:09 PM */ Set interface

Set Interface

Core Java

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. The Set interface does not add any methods to the Collection interface which is its parent. Any class implementing the Set interface is not permitted to have duplicate entries.
The formal definition of the Set interface requires that no two elements in the Set are equal [such that e1.equals(e2) is never true]. Since a Set can not have duplicates, the Set can contain no more than one null entry. A Set is not required to maintain the elements of the Set in the order they are added although the LinkedHashSet does maintain order.

Table of contents:

Java Set Example

Set Implementations

Adding and Accessing Elements

Removing Elements

Generic Sets

More Details in the JavaDoc

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( set.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:





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 would 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) iterator.next();

//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 set = new HashSet();

//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...

Click here to continue reading - Queue