java.util.TreeSet.last() Method


Description

The last() method is used to return the last (highest) element currently in this set.

Declaration

Following is the declaration for java.util.TreeSet.last() method.

public E last()

Parameters

NA

Return Value

The method call returns the last (highest) element currently in this set.

Exception

NoSuchElementException − This exception is thrown if this set is empty.

Example

The following example shows the usage of java.util.TreeSet.last() method.

package com.tutorialspoint;

import java.util.TreeSet;

public class TreeSetDemo {
   public static void main(String[] args) {

      // creating a TreeSet 
      TreeSet <Integer>treeadd = new TreeSet<Integer>();

      // adding in the tree set
      treeadd.add(1);
      treeadd.add(13);
      treeadd.add(17);
      treeadd.add(2);

      // displaying the last highest element
      System.out.println("Last highest element: "+treeadd.last());    
   }    
}

Let us compile and run the above program, this will produce the following result.

Last highest element: 17 
java_util_treeset.htm
Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements