treeset last method java code example

Example 1: TreeSet last() method in java

import java.util.TreeSet;
public class TreeSetLastMethodExample
{
   public static void main(String[] args)
   {
      TreeSet<Integer> ts = new TreeSet<Integer>();
      ts.add(58);
      ts.add(42);
      ts.add(29);
      ts.add(10);
      // print last highest element
      System.out.println("Last highest element is: " + ts.last());
   }
}

Example 2: TreeSet last() method in java

TreeSet last() method when elements are String types.
import java.util.TreeSet;
public class TreeSetLastMethodExample
{
   public static void main(String[] args)
   {
      TreeSet<String> ts = new TreeSet<String>();
      ts.add("grapes");
      ts.add("banana");
      ts.add("watermelon");
      ts.add("apple");
      // print last highest element
      System.out.println("Last highest element is: " + ts.last());
   }
}

Tags:

Java Example