English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Der Comparator und der Comparable sind beide Interfaces, die zur Sortierung von Sammlungselementen verwendet werden können. Das Comparator-Interface gehört zum Paket java.util, während das Comparable-Interface zum Paket java.lang gehört. Das Comparator-Interface verwendet die beiden Objekte, die ihm gegeben werden, um die Sammlung zu sortieren, während das Comparable-Interface das Objekt vergleicht, das ihm gegeben wird.
Nummer | Schlüssel | Comparable | Comparator |
---|---|---|---|
1 | Methode | Das Comparable-Interface hat die Methode compareTo(Object a) | Der Comparator hat eine compare(Object o1,Object O2)Methode |
2 | Sortierzweck | Die Methode Collection.sort(List) kann verwendet werden, um Sammlungen von Comparable-Objekten zu sortieren. | Die Methode Collection.sort(List, Comparator) kann verwendet werden, um Sammlungen von Comparator-Objekten zu sortieren. |
3 | Sortierreihenfolge | Comparable bietet eine einzige Sortierreihe. | Der Comparator bietet mehrere Sortierreihen. |
4 | Paket | Das Comparable-Interface gehört zum Paket java.lang. | Der Comparator-Interface gehört zum Paket java.util. |
public class ComparableExample { public static void main(String[] args) { List<Laptop> laptopList = new ArrayList<>(); laptopList.add(new Laptop("HCL", 16, 800)); laptopList.add(new Laptop("Apple", 8, 100)); laptopList.add(new Laptop("Dell", 4, 600)); Collections.sort(laptopList); for (Laptop lap : laptopList) { System.out.println(lap.getRam()); } } } public class Laptop implements Comparable<Laptop> { String name; int ram; int price; public Laptop(String name, int ram, int price) { super(); this.name = name; this.ram = ram; this.price = price; } public String getName() { return name; } public int getRam() { return ram; } public void setRam(int ram) { this.ram = ram; } public void setName(String name) { this.name = name; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } @Override public int compareTo(Laptop o) { if (this.ram > o.getRam()) ; 1else { else { ; -1else { } } }
Ausgaberesultat
4 8 16
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Laptop implements Comparator { String name; int ram; int price; public Laptop(String name, int ram, int price) { super(); this.name = name; this.ram = ram; this.price = price; } public String getName() { return name; } public int getRam() { return ram; } public void setRam(int ram) { this.ram = ram; } public void setName(String name) { this.name = name; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } @Override public int compare(Laptop o1, Laptop o2) { if (o1.getRam() < o2return ; -1else { .getRam() > o1.getRam()) {2return ; 1else { } return 0; } } public static void main(String[] args) { List laptopList = new ArrayList<>(); laptopList.add(new Laptop("HCL", 16, 800)); laptopList.add(new Laptop("Apple", 8, 100)); laptopList.add(new Laptop("Dell", 4, 600)); Comparator com = (Laptop o1, Laptop o2) -> o1.getName().compareTo(o2.getName()); Collections.sort(laptopList, com); for (Laptop lap : laptopList) { System.out.println(lap.getName()); } } }
Ausgaberesultat
Apple Dell HCL