El método pollFirst() de TreeSet en Java se usa para recuperar y eliminar el primer elemento (el más bajo), o devuelve un valor nulo si este TreeSet está vacío.
Sintaxis:
E pollFirst()
Donde, E es el tipo de elementos que mantiene este contenedor TreeSet.
Parámetros: Esta función no acepta ningún parámetro.
Tipo de devolución: Devuelve el primer elemento del Treeset si TreeSet no está vacío, de lo contrario devuelve nulo.
Los siguientes programas ilustran el método pollFirst() en Java:
Ejemplo 1
// A Java program to demonstrate // pollFirst() method of TreeSet import java.util.TreeSet; public class GFG { public static void main(String[] args) { // creating TreeSet TreeSet<Integer> ts = new TreeSet<Integer>(); // adding element to the TreeSet ts.add(0); ts.add(1); ts.add(2); ts.add(3); ts.add(4); ts.add(5); ts.add(6); // before removing element System.out.println("Before removing " + "element from TreeSet: " + ts); // first element is removed System.out.println("First lowest element " + "removed is : " + ts.pollFirst()); System.out.println("After removing element" + " from TreeSet: " + ts); } }
Producción:
Before removing element from TreeSet: [0, 1, 2, 3, 4, 5, 6] First lowest element removed is : 0 After removing element from TreeSet: [1, 2, 3, 4, 5, 6]
Ejemplo 2
// A Java program to demonstrate // pollFirst() method of TreeSet import java.util.TreeSet; public class GFG { public static void main(String[] args) { // creating TreeSet TreeSet<String> ts = new TreeSet<String>(); // removing element when TreeSet // is empty System.out.println("pollFirst() method when " + "TreeSet is empty : " + ts.pollFirst()); // adding element to the TreeSet ts.add("Geeks"); ts.add("for"); ts.add("Geek"); // before removing element System.out.println("Before removing element from" + " TreeSet: " + ts); // first element is removed System.out.println("First lowest element removed is : " + ts.pollFirst()); System.out.println("After removing element " + "from TreeSet: " + ts); } }
Producción:
pollFirst() method when TreeSet is empty : null Before removing element from TreeSet: [Geek, Geeks, for] First lowest element removed is : Geek After removing element from TreeSet: [Geeks, for]
Si TreeSet está vacío, este método devuelve nulo.
Ejemplo 3
// A Java program to demonstrate // pollFirst() method of TreeSet import java.util.TreeSet; public class GFG { public static void main(String[] args) { // creating TreeSet TreeSet<Integer> ts = new TreeSet<Integer>(); // before removing element System.out.println("Before removing " + "element from TreeSet: " + ts); // first element is removed System.out.println("First lowest element " + "removed is : " + ts.pollFirst()); System.out.println("After removing element" + " from TreeSet: " + ts); } }
Producción:
Before removing element from TreeSet: [] First lowest element removed is : null After removing element from TreeSet: []
Publicación traducida automáticamente
Artículo escrito por ankit15697 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA