This example shows how to get first and last elements from the TreeSet in Java. This example also shows how to get the lowest (smallest) or highest (biggest) elements using the first, last, pollFirst, and pollLast methods.
How to get the first and last element from TreeSet in Java?
The TreeSet class in Java provides several methods to get these elements from the set object.
How to get the first element (lowest or smallest) from the TreeSet?
There are several ways using which we can get the element as given below.
1. Using the first method
The first
method of the TreeSet class returns the lowest or smallest element from the TreeSet object.
1 |
public E first() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.TreeSet; public class TreeSetGetFirstAndLastElements { public static void main(String[] args) { TreeSet<Integer> tSetEvenNumbers = new TreeSet<Integer>(); tSetEvenNumbers.add(24); tSetEvenNumbers.add(12); tSetEvenNumbers.add(32); tSetEvenNumbers.add(72); tSetEvenNumbers.add(14); /* * To get the first, lowest or smallest element * from the TreeSet use the first method */ System.out.println( tSetEvenNumbers.first() ); } } |
2. Using the Iterator
We can also get an iterator over the TrreeSet and get the first element as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
TreeSet<Integer> tSetEvenNumbers = new TreeSet<Integer>(); tSetEvenNumbers.add(24); tSetEvenNumbers.add(12); tSetEvenNumbers.add(32); tSetEvenNumbers.add(72); tSetEvenNumbers.add(14); //get the iterator Iterator<Integer> itr = tSetEvenNumbers.iterator(); if( itr.hasNext() ){ System.out.println( itr.next() ); } |
Output
1 |
12 |
3. Using the pollFirst method
The pollFirst
method removes and returns the smallest or lowest TreeSet element.
1 |
public E pollFirst() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
TreeSet<Integer> tSetEvenNumbers = new TreeSet<Integer>(); tSetEvenNumbers.add(24); tSetEvenNumbers.add(12); tSetEvenNumbers.add(32); tSetEvenNumbers.add(72); tSetEvenNumbers.add(14); /* * The pollFirst method removes and returns * the first element from the TreeSet */ System.out.println( tSetEvenNumbers.pollFirst() ); System.out.println("TreeSet contains: " + tSetEvenNumbers); |
Output
1 2 |
12 TreeSet contains: [14, 24, 32, 72] |
Important Note: The pollFirst
method removes the first element from the TreeSet.
4. Using Java 8
If you are using Java version 8 or later you can use the stream to get the element from the set as given below.
1 2 3 4 5 6 7 8 9 10 11 |
TreeSet<Integer> tSetEvenNumbers = new TreeSet<Integer>(); tSetEvenNumbers.add(24); tSetEvenNumbers.add(12); tSetEvenNumbers.add(32); tSetEvenNumbers.add(72); tSetEvenNumbers.add(14); if(!tSetEvenNumbers.isEmpty()){ System.out.println( tSetEvenNumbers.stream().findFirst().get() ); } |
Output
1 |
12 |
How to get the last element (highest or biggest) from the TreeSet?
We can use the below given methods to get the last (or biggest or highest) element from the TreeSet in Java.
1. Using the last method
The last
method of the TreeSet class returns the highest or biggest element from the set.
1 |
public E last() |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
TreeSet<Integer> tSetEvenNumbers = new TreeSet<Integer>(); tSetEvenNumbers.add(24); tSetEvenNumbers.add(12); tSetEvenNumbers.add(32); tSetEvenNumbers.add(72); tSetEvenNumbers.add(14); /* * Use the last method to get the highest * or biggest element from the TreeSet */ System.out.println( tSetEvenNumbers.last() ); |
Output
1 |
72 |
2. Using the iterator
We can also use an Iterator over TreeSet elements to get the highest element from the set.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
TreeSet<Integer> tSetEvenNumbers = new TreeSet<Integer>(); tSetEvenNumbers.add(24); tSetEvenNumbers.add(12); tSetEvenNumbers.add(32); tSetEvenNumbers.add(72); tSetEvenNumbers.add(14); if(!tSetEvenNumbers.isEmpty()){ Iterator<Integer> itr = tSetEvenNumbers.iterator(); Integer lastElement = null; while(itr.hasNext()){ lastElement = itr.next(); } System.out.println(lastElement); } |
Output
1 |
72 |
3. Using the pollLast method
The pollLast
method removes and returns the highest or biggest element of the set.
1 |
public E pollLast() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
TreeSet<Integer> tSetEvenNumbers = new TreeSet<Integer>(); tSetEvenNumbers.add(24); tSetEvenNumbers.add(12); tSetEvenNumbers.add(32); tSetEvenNumbers.add(72); tSetEvenNumbers.add(14); /* * The pollLast method removes and returns the * last element from the TreeSet */ System.out.println( tSetEvenNumbers.pollLast() ); System.out.println("TreeSet contains: " + tSetEvenNumbers); |
Output
1 2 |
72 TreeSet contains: [12, 14, 24, 32] |
As we can see from the output, the pollLast
method removes and returns the element from the TreeSet.
This example is a part of the Java TreeSet Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 TreeSet