This tutorial will discuss on the Collections subtypes Java's Set Interface and Java SortedSet Interface. There are more subtypes for the Java Collection that will be discussed in the subsequent articles.
Java.util.Collection is the interface that has the subtype as java.util.set that represents set of objects. This means that the existence of element will not be more than one in a set.
This tutorial will cover the following topics as far as Java Set Interface is concerned.
All the methods that exist in the interface of the Collection are present in SetInterface as well. Now since when set being an interface, it would require to instantiate a functionality or interface implementation that is concrete in behaviour and ready to be used by everyone. One can pick from the subsequent set implementations present in the API of Java Collections.
When it comes to the behaviour of the above stated Set Implementations, they behave in altogether a different manner when talk about the sequence of the elements during iteration of a set. Also they differ in the time that is being taken to insert and retrieve or access the sets elements.
HashSet is backed by a HashMap and provides no assurity on the order of the elements during the process of iteration.
LinkedHashSet behaves in a bit different manner from HashSet. It guarantees on elements sequence when elements are iterated from the set that they will be in the same order as they were inserted into the LinkedHashSet. And when you go to reinsert any element that is already present in the LinkedHashSet, the order is not altered.
Talking about the TreeSet now, it is also capable to provide a guarantee of elements order during the process of iteration. However the order here will be the sorting order of the elements meaning that when you make use of Collections.sort() on a List or array that comprise of these elements would result in sorting of elements in the Set. The way through which the order is determined is defined by the natural order. The other way could be a specificComparator implementation.
The below illustrations displays the ways to create a set instance:
Set set1 = new EnumSet(); Set set2 = new HashSet(); Set set3 = new LinkedHashSet(); Set set4 = new TreeSet();
Add() method can be made use of too add the elements to a Set and collection interface is made use of to inherit this method. Few list the examples for the same.
Set set1 = new HashSet();
Set1.add("element 1");
Set1.add("element 2");
Set1.add("element 3");
The add() calls, three in number, are made use of to add a String instance to the set.
The order of elements when the elements in the set are iterated depends on which SetImplementation is made use of as discussed earlier. Let us see an example of this.
Set set1 = new HashSet();
Set1.add("element 0");
Set1.add("element 1");
Set1.add("element 2");
//access via Iterator
Iterator iterator = set1.iterator();
while(iterator.hasNext(){
String element = (String) iterator.next();
}
//access via new for-loop
for(Object object : set1) {
String element = (String) object;
}
Remove (Object o) method can be made use of to remove the elements from a set. There is no way via the help of which the object can be removed when it comes to removal of object that is dependent or based on index in a Set. The reason for the same is that the sequence of the elements in which they will be iterated depends on SetImplementation.
Any object that is required or need to be added before Java 5 can be added by default but from Java 5, the limit is been set to a particular number of objects that can be added to a Set. See the example below:
Set<MyObject> set = new HashSet<MyObject>();
Hence the above set will only have Myobject instances contained in it. Post this, the same can be retrieve or get access to so as to iterate the elements without the need of casting them. Below lists the example for the same:
for(MyObject anObject : set){
//do someting to anObject...
}The java.util.SortedSet is nothing but an interface that is a subtype of the java.util.Set. The behaviour of the same is similar to a normal set. The only exception is that hee the sorting of the elements is done internally. This means that when the elements of a SortedSet are iterated, elements are displayed or returned in the sorted order, and the order of the sorting will either be the natural process of sorting order when java.lang.comparable is implemented or the other could be to the determination via the help of a Comparator that one can provide to the SortedSet.
Ascending order is what is followed by default by the elements when they are iterated for the first time. It starts from the smallest one and move up to the largest element. But in case if one wants to order the elements in the descending order, the same can be done with help of the method namely TreeSet.descendingIterator().
There is only and only one implementation of SortedSet interface for Java Collections API, which is known by the name of - thejava.util.TreeSet class. Though the java.util.concurrent package as well has implementation for this particular type of interface, however we will discuss this in the upcoming tutorials.
The subsequent example lists how to create a SortedSet:
SortedSet set1 = new TreeSet(); Comparator comparator = new MyComparator(); SortedSet set2 = new TreeSet(comparator);
import java.util.*;
public class SetDemo {
public static void main(String args[]) {
int count[]={34, 22,10,60,30,22};
Set<Integer> set = new HashSet<Integer>();
try{
for(int i=0; i<5; i++){
set.add(count[i]);
}
System.out.println(set);
TreeSet sortedSet=new TreeSet<Integer>(set);
System.out.println("The sorted list is:");
System.out.println(sortedSet);
System.out.println("The First element of the set is: "+
(Integer)sortedSet.first());
System.out.println("The last element of the set is: "+
(Integer)sortedSet.last());
}
catch(Exception e){}
}
}

.jpg)







See the prices for this post in Mr.Bool Credits System below: