Get first and last value In Java TreeSet Example

Get first and last value In Java TreeSet Example


Posted in : Java Posted on : November 24, 2010 at 5:26 PM Comments : [ 0 ]

This Java Example shows how to first and last values exists in TreeSet.

Get first and last value In Java TreeSet Example

This Java Example shows how to first and last values exists in TreeSet. The first value represent the lowest and last value represent to the highest value because TreeSet stores the elements in sorted order.We use first() and last() method of TreeSet class.  It returns first and last element stored in specified object of  the collection.

Example:- FirstAndLastValueTreeSet.java

package devmanuals.com;

import java.util.TreeSet;

public class FirstAndLastValueTreeSet {
	public static void main(String args[]) {
		// Create a tree set
		TreeSet oTreeSet = new TreeSet();
		// Add elements to the tree set
		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);
		System.out.println("The first element is: " + oTreeSet.first());
		System.out.println("The last element is: " + oTreeSet.last());
	}
}

Output:-

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

The first element is: A 

The last element is: F

Download The Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics