The Java.util.TreeSet.first() method is used to return the first of the element of a TreeSet. The first element here is being referred to the lowest of the elements in the set. If the elements are of integer types then the smallest 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 initial alphabets in the order of dictionary is returned irrespective of length.
Syntax:
Tree_Set.first()
Parameters: The method does not take any parameters.
Return Value: The method returns the lowest 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 smallest integer is returned. Also, the numbers of string type hold higher priority.
Below are the programs illustrating the use of Java.util.TreeSet.first() method:
Program 1: When the elements are of integer types.
// Java code to illustrate first() 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 lowest element of the set System.out.println("The first element is: " + tree.first()); } } |
TreeSet: [7, 8, 14, 48, 124, 200] The first element is: 7
Program 2: When the elements are of string types.
// Java code to illustrate first() 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 lowest element of the set System.out.println("The first element is: " + tree.first()); } } |
TreeSet: [Ab, B, Geeks, To, TreeSet, Welcome] The first element is: Ab
Program 3: When the elements are of string types but with integer values. Here we see that the digit appearing first is considered as first element irrespective of length:
// Java code to illustrate first() 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("100"); tree.add("5"); // Displaying the TreeSet System.out.println("TreeSet: " + tree); // Displaying the lowest element of the set System.out.println("The first element is: " + tree.first()); } } |
TreeSet: [100, 45, 5, Geeks, To, Welcome] The first element is: 100
Recommended Posts:
- TreeSet add() Method in Java
- TreeSet contains() Method in Java
- TreeSet last() Method in Java
- TreeSet pollLast() method in Java with Example
- TreeSet subSet() Method in Java
- TreeSet tailSet() Method in Java
- TreeSet headSet() Method in Java
- TreeSet isEmpty() Method in Java
- TreeSet clone() Method in Java
- TreeSet clear() Method in Java
- TreeSet addAll() Method in Java
- TreeSet lower() method in Java
- TreeSet comparator() Method in Java
- TreeSet remove() Method in Java
- TreeSet iterator() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



