java.util.TreeSet.first() Method


Advertisements


Description

The first() method is used to return the first (lowest) element currently in this set.

Declaration

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

public E first()

Parameters

NA

Return Value

The method call returns the first (lowest) element currently in this set.

Exception

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

Example

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

Live Demo
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(12);
      treeadd.add(11);
      treeadd.add(16);
      treeadd.add(15);

      // getting the first lowset element
      System.out.println("First lowest element: "+treeadd.first());   
   }    
}  

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

First lowest element: 11

java_util_treeset.htm

Advertisements
E-Books Store