Open In App

TreeSet last() Method in Java

Improve
Improve
Like Article
Like
Save
Share
Report

The Java.util.TreeSet.last() method is used to return the last of the element of a TreeSet. The last element here is being referred to the highest of the elements in the set. If the elements are of integer types, then the largest integer is returned. If the elements are of the string types, then the elements are checked in the alphabetical order and the string starting with the last alphabets in the order of dictionary is returned irrespective of length.

Syntax:

Tree_Set.last()

Parameters: The method does not take any parameters.

Return Value: The method returns the highest member of the set. If the elements are of string type then they are checked in alphabetical order irrespective of length and if the elements are of integer types then the largest integer is returned. The string of alphabet types is given higher priority.

Below are the programs illustrating the use of Java.util.TreeSet.last() method:
Program 1: When the elements are of integer types:




// Java code to illustrate last()
import java.util.*;
import java.util.TreeSet;
  
public class TreeSetDemo {
    public static void main(String args[])
    {
        // Creating an empty TreeSet
        TreeSet<Integer> tree = new TreeSet<Integer>();
  
        // Use add() method to add elements into the Set
        tree.add(14);
        tree.add(8);
        tree.add(200);
        tree.add(48);
        tree.add(7);
        tree.add(124);
  
        // Displaying the TreeSet
        System.out.println("TreeSet: " + tree);
  
        // Displaying the highest element of the set
        System.out.println("The last element is: " + tree.last());
    }
}


Output:

TreeSet: [7, 8, 14, 48, 124, 200]
The last element is: 200

Program 2: When the elements are of string types:




// Java code to illustrate last()
import java.util.*;
import java.util.TreeSet;
  
public class TreeSetDemo {
    public static void main(String args[])
    {
        // Creating an empty TreeSet
        TreeSet<String> tree = new TreeSet<String>();
  
        // Use add() method to add elements into the Set
        tree.add("Welcome");
        tree.add("To");
        tree.add("Geeks");
        tree.add("Ab");
        tree.add("TreeSet");
        tree.add("B");
  
        // Displaying the TreeSet
        System.out.println("TreeSet: " + tree);
  
        // Displaying the highest element of the set
        System.out.println("The last element is: " + tree.last());
    }
}


Output:

TreeSet: [Ab, B, Geeks, To, TreeSet, Welcome]
The last element is: Welcome

Program 3: When the elements are of string types but with integer values. Here we see that the alphabets appearing to the last in the dictionary are given higher priority:




// Java code to illustrate last()
import java.util.*;
import java.util.TreeSet;
  
public class TreeSetDemo {
    public static void main(String args[])
    {
        // Creating an empty TreeSet
        TreeSet<String> tree = new TreeSet<String>();
  
        // Use add() method to add elements into the Set
        tree.add("Welcome");
        tree.add("To");
        tree.add("Geeks");
        tree.add("45");
        tree.add("90000000");
        tree.add("Z");
  
        // Displaying the TreeSet
        System.out.println("TreeSet: " + tree);
  
        // Displaying the highest element of the set
        System.out.println("The last element is: " + tree.last());
    }
}


Output:

TreeSet: [45, 90000000, Geeks, To, Welcome, Z]
The last element is: Z


Last Updated : 26 Nov, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads