Java比较器示例
在本教程中,我们将讨论java comparator及其示例。
什么是Java比较器?
Java Comparator是一个用于排列Java对象的接口。“召集人”java.util.comparator,Java Comparator分析“compare(Object 01,Object 02)”组中的两个Java抗议。
利用可配置的策略,javacomparator可以将对象与利润(基于正、相等或者负相关的数字)进行对比。因为它不局限于查看数字,所以可以设置Java比较器,以顺序或者数字顺序逐个请求记录。与java.io.Serializable可序列化,Java比较器同样可以用来有效地安排序列化信息结构。
Java Comparator类似于Comparable接口,但它被用于描述交换排序顺序,其中可比较的排序通过常规请求进行,例如,字典式排列。
语法
公共int比较(Object obj1,Object obj2):
如何使用Java比较器?
TreeSet和TreeMap都在已安排的请求中存储组件。尽管如此,比较器明确地描述了被安排的请求意味着什么。
Comparator接口具有两种技术:compare()和equals()。出现了compare()策略,它针对请求
比较法
int compare(对象obj1,Object obj2)
obj1和obj2被质疑被看。如果条款相等,则此策略返回零。如果obj1比obj2更值得注意,它会恢复积极的尊重。另一方面,消极的尊重也会回来。
通过取消compare(),我们可以修改请求抗议的方式。例如,要对请求进行向后排序,我们可以制作一个比较器来切换检查结果。
Equals方法
出现了equals()策略,测试一个文章是否与变戏法比较符
布尔等于(Object obj)
obj是为了公平而被审判的文章。如果obj和调用对象都是比较对象,并且使用类似的请求,则该策略返回true。否则,返回false。
废除equals()是多余的,大多数直接的比较器不会这样做。
Java比较器示例
import java.util.*; class Dog implements Comparator<Dog>, Comparable<Dog> { private String name; private int age; Dog() { } Dog(String n, int a) { name = n; age = a; } public String getDogName() { return name; } public int getDogAge() { return age; } //Overriding the compareTo method public int compareTo(Dog d) { return (this.name).compareTo(d.name); } //Overriding the compare method to sort the age public int compare(Dog d, Dog d1) { return d.age - d1.age; } } public class Example { public static void main(String args[]) { //Takes a list o Dog objects List<Dog> list = new ArrayList<Dog>(); list.add(new Dog("Shaggy", 3)); list.add(new Dog("Lacy", 2)); list.add(new Dog("Roger", 10)); list.add(new Dog("Tommy", 4)); list.add(new Dog("Tammy", 1)); Collections.sort(list); //Sorts the array list for(Dog a: list) //printing the sorted list of names System.out.print(a.getDogName() + ", "); //Sorts the array list using comparator Collections.sort(list, new Dog()); System.out.println(" "); for(Dog a: list) //printing the sorted list of ages System.out.print(a.getDogName() +" : "+ a.getDogAge() + ", "); } }
OUTPUT:
Lacy, Roger, Shaggy, Tammy, Tommy, Tammy : 1, Lacy : 2, Shaggy : 3, Tommy : 4, Roger : 10,
Java比较器的工作实例。
import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Comparator; class Student { //instance member variables String Name; int Age; //parameterized constructor public Student(String Name, Integer Age) { this.Name = Name; this.Age = Age; } public String getName() { return Name; } public void setName(String Name) { this.Name = Name; } public Integer getAge() { return Age; } public void setAge(Integer Age) { this.Age = Age; } //overriding toString() method @Override public String toString() { return "Customer{" + "Name=" + Name + ", Age=" + Age + '}'; } static class CustomerSortingComparator implements Comparator<Student> { @Override public int compare(Student customer1, Student customer2) { //for comparison int NameCompare = customer1.getName().compareTo(customer2.getName()); int AgeCompare = customer1.getAge().compareTo(customer2.getAge()); //2-level comparison using if-else block if (NameCompare == 0) { return ((AgeCompare == 0) ? NameCompare : AgeCompare); } else { return NameCompare; } } } public static void main(String[] args) { //create ArrayList to store Student List<Student> al = new ArrayList<>(); //create customer objects using constructor initialization Student obj1 = new Student("Ajay", 27); Student obj2 = new Student("Sneha", 23); Student obj3 = new Student("Simran", 37); Student obj4 = new Student("Ajay", 22); Student obj5 = new Student("Ajay", 29); Student obj6 = new Student("Sneha", 22); //add customer objects to ArrayList al.add(obj1); al.add(obj2); al.add(obj3); al.add(obj4); al.add(obj5); al.add(obj6); //before Sorting arraylist: iterate using Iterator Iterator<Student> custIterator = al.iterator(); System.out.println("Before Sorting:\n"); while (custIterator.hasNext()) { System.out.println(custIterator.next()); } //sorting using Collections.sort(al, comparator); Collections.sort(al, new CustomerSortingComparator()); //after Sorting arraylist: iterate using enhanced for-loop System.out.println("\n\nAfter Sorting:\n"); for (Student customer : al) { System.out.println(customer); } } }
输出
Unsorted 111 bbbb london 131 aaaa nyc 121 cccc jaipur Sorted by rollno 111 bbbb london 121 cccc jaipur 131 aaaa nyc Sorted by name 131 aaaa nyc 111 bbbb london 121 cccc jaipu