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 |
[ 0 ] Comments