Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Brief Insight into Java’s Set Interface and Java SortedSet Interface

In this article you will see more things about subtypes from Java Collections.

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.

  • Set Implementations
  • Elements Addition and Accessing
  • Elements Removal
  • Generic Sets

Set Implementations

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.

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

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();

Elements Addition and Accessing

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;
}

Elements Removal

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.

Generic Sets

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...
}
Java SortedSet Interface

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);

Example of Set Interface

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){}
  }
}

Output of the Program:

Output of the Program


I am a software developer from India with hands on experience on java, html for over 5 years.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

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

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login