Get Sub Set from Java TreeSet example

Get Sub Set from Java TreeSet example


Posted in : Java Posted on : November 25, 2010 at 12:51 PM Comments : [ 0 ]

In this example, We will discuss about how to get the sub set from java TreeSet object.

Get Sub Set from Java TreeSet example

In this example, We will discuss about how to get the sub set from java TreeSet object. the sub set can be obtained by giving specific
range of values using subSet method of Java TreeSet class. To get the sub set we use SortedSet subSet(Object FromElement, Object toElement)  method. This method returns a view of the portion of this set whose elements are within the specified range.

The Example is as follows-

SubSetOfTreSetExample.java

package devmanuals.com;

import java.util.SortedSet;
import java.util.TreeSet;

public class SubSetOfTreSetExample {
	public static void main(String args[]) {

		TreeSet oTreeSet = new TreeSet();

		oTreeSet.add("C");
		oTreeSet.add("A");
		oTreeSet.add("B");
		oTreeSet.add("E");
		oTreeSet.add("F");
		oTreeSet.add("D");
		System.out.println("The elements are: " + oTreeSet);
		SortedSet Osort = oTreeSet.subSet("B", "F");
		System.out.println("The subset of TreeSet is: " + Osort);

	}
}

Output:-

The elements are: [A, B, C, D, E, F] 

The subset of TreeSet is: [B, C, D, E]

Download This Example

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics