Get first and last value In Java SortedSet Example

Get first and last value In Java SortedSet Example


Posted in : Java Posted on : December 6, 2010 at 6:15 PM Comments : [ 0 ]

This Java Example shows how to get the first and last values exists in Set.

Get first and last value In Java SortedSet Example

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

Example:- FirstAndLastValueSortedSet.java

package devmanuals.com;

import java.util.*;

public class FirstAndLastValueSortedSet {
	public static void main(String args[]) {
		SortedSet Sset = new TreeSet();
		Sset.add("Gyan");
		Sset.add("Ankit");
		Sset.add("Arun");
		Sset.add("Anand");
		Sset.add("Ram");
		System.out.println("The SortedSet entries is as follows : " + Sset);
		System.out.println("The first element of the set is : " + Sset.first());
		System.out.println("The last element of the set is : " + Sset.last());

	}
}

Output:-

The SortedSet entries is as follows : [Anand, Ankit, Arun, Gyan, Ram] 

The first element of the set is : Anand 

The last element of the set is : Ram

Download The Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics