El método comparador() de java.util.concurrent.ConcurrentSkipListSet es una función integrada en Java que devuelve el comparador utilizado para ordenar los elementos en el conjunto dado. Si se utiliza el ordenamiento natural, se devuelve nulo.
Sintaxis:
public Comparator<E> comparator()
Aquí E es el parámetro de tipo, es decir, el tipo de elementos que mantiene el conjunto.
Valor devuelto: la función devuelve el comparador que se usa para ordenar los elementos en el conjunto dado, devuelve nulo si se usa el ordenamiento natural.
El programa que se muestra a continuación ilustra el método ConcurrentSkipListSet.comparator():
Programa 1:
// Java program to demonstrate comparator() // method of ConcurrentSkipListSet import java.util.concurrent.*; class ConcurrentSkipListSetcomparatorExample1 { public static void main(String[] args) { // Creating first set object ConcurrentSkipListSet<Integer> set1 = new ConcurrentSkipListSet<Integer>(); // Creating second set object ConcurrentSkipListSet<Integer> set2 = new ConcurrentSkipListSet<Integer>(); // Adding elements to first set set1.add(30); set1.add(5); set1.add(50); set1.add(20); // Ordering the elements in descending order set2 = (ConcurrentSkipListSet<Integer>) set1.descendingSet(); // Displaying the contents of the set1 System.out.println("Contents of the set1: " + set1); // Displaying the contents of the set2 System.out.println("Contents of the set2: " + set2); // Retrieving the comparator // used for ordering of elements System.out.println("The comparator" + " used in the set:\n" + set2.comparator()); } }
Contents of the set1: [5, 20, 30, 50] Contents of the set2: [50, 30, 20, 5] The comparator used in the set: java.util.Collections$ReverseComparator@74a14482
Programa 2:
// Java program to demonstrate comparator() // method of ConcurrentSkipListSet import java.util.concurrent.*; class ConcurrentSkipListSetcomparatorExample2 { public static void main(String[] args) { // Creating first set object ConcurrentSkipListSet<Integer> set1 = new ConcurrentSkipListSet<Integer>(); // Adding elements to first set set1.add(30); set1.add(5); set1.add(50); set1.add(20); // Displaying the contents of the set1 System.out.println("Contents of the set1: " + set1); // Retrieving the comparator // used for ordering of elements System.out.println("The comparator used in the set: " + set1.comparator()); } }
Contents of the set1: [5, 20, 30, 50] The comparator used in the set: null
Referencia: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#comparator–
Publicación traducida automáticamente
Artículo escrito por NamanSingh34 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA