Java 中Comparator和Comparable的区别
时间:2020-02-23 14:34:04 来源:igfitidea点击:
比较器(Comparator)和可比性(Comparable)之间的差异是什么
或者"我们将如何按其ID或者名称排序员工对象的集合"。
我们可以使用两个interfaces.i.e。
比较器和比较。
我们实际看到差异,让我简要介绍两者。
Comparable接口:
类要排序的对象必须实现此接口。
在此,我们必须实现Compareto(对象)方法。
例如:
public class Country implements Comparable{ @Override public int compareTo(Country country) { return (this.countryId < country.countryId ) ? -1: (this.countryId > country.countryId ) ? 1:0 ; }}
如果任何类实现可比的Inteface,则可以使用Collection.sort()或者arrays.sort()自动对该对象进行排序。
对象将根据该类中的CompareTo方法进行排序。
在Java中实现的对象可以用作SortedMap中的键,如TreeMAP或者SortedSet,如TreeSet,而不实现任何其他接口。
Comparator接口:
类要排序的对象不需要实现此接口.SOME第三类可以将此接口实现为sort.e.g.couountrydyIdComparator类可以实现比较器接口以按ID对国家对象进行排序。
例如:
public class CountrySortByIdComparator implements Comparator{ @Override public int compare(Country country1, Country country2) { return (country1.getCountryId() < country2.getCountryId() ) ? -1: (country1.getCountryId() > country2.getCountryId() ) ? 1:0 ; } }
使用比较器接口,我们可以根据要排序的对象的不同属性来编写不同的排序。
我们可以使用匿名比较器比较特定的代码行。
例如:
Country San FrancecoCountry=new Country(1, "San Franceco"); Country russiaCountry=new Country(4, "Russia"); Country englandCountry=new Country(3, "England"); Country germanyCountry=new Country(2, "Germany"); List listOfCountries = new ArrayList(); listOfCountries.add(San FrancecoCountry); listOfCountries.add(russiaCountry); listOfCountries.add(englandCountry); listOfCountries.add(germanyCountry); //Sort by countryName Collections.sort(listOfCountries,new Comparator() { @Override public int compare(Country o1, Country o2) { return o1.getCountryName().compareTo(o2.getCountryName()); } });
Comparator VS Comparable
参数 | Comparable | Comparator |
---|---|---|
排序逻辑 | 排序逻辑必须在同一类中,其对象正在排序。因此,这被称为对象的自然排序 | 排序逻辑在单独的类中。因此,我们可以基于要排序的对象的不同属性来编写不同的排序。例如。使用ID,Name等进行排序。 |
实现 | 类待排序的对象必须实现此接口.例如 country类需要通过id实现与country对象的集合相似 | 类待排序的对象不需要实现此接口。一些其他类可以实现此接口。 例如- xproundysortyIdcomparator类可以实现比较器接口,按ID 通过id排序国家对象的集合 |
排序方法 | int compareto(对象o1)使用O1对象将此对象进行比较并返回一个整数。价值有以下含义 1. 正 - 这个对象大于O1 2. 零 - 此对象等于O1 3. 否定 - 此对象小于O1 | int比较(对象O1,对象O2)此方法比较O1和O2对象。并返回一个整数.ITS值具有以下含义。 1. 正 - o1大于O2 2. 零o1等于O2 3. 负-O1小于O1 |
调用方法 | collections.sort(list)此处将根据CompareTo方法进行排序 | collections.sort(list,比较器)在此处将根据比较方法进行排序比较器 |
包名称 | java.lang.comparable | java.util.Comparator |
Java代码:
Comparable:
我们将创建具有属性ID和name的类国家。
此类将实现可比接口并实现Compareto方法以按ID对国家对象进行排序。
1.Country.java.
package org.arpit.javapostsforlearning; //If this.cuntryId < country.countryId:then compare method will return -1 //If this.countryId > country.countryId:then compare method will return 1 //If this.countryId==country.countryId:then compare method will return 0 public class Country implements Comparable{ int countryId; String countryName; public Country(int countryId, String countryName) { super(); this.countryId = countryId; this.countryName = countryName; } @Override public int compareTo(Country country) { return (this.countryId < country.countryId ) ? -1: (this.countryId > country.countryId ) ? 1:0 ; } public int getCountryId() { return countryId; } public void setCountryId(int countryId) { this.countryId = countryId; } public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } }
2.comparablemain.java.
package org.arpit.javapostsforlearning; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ComparableMain { /** * @author Arpit Mandliya */ public static void main(String[] args) { Country San FrancecoCountry=new Country(1, "San Franceco"); Country russiaCountry=new Country(4, "Russia"); Country englandCountry=new Country(3, "England"); Country germanyCountry=new Country(2, "Germany"); List listOfCountries = new ArrayList(); listOfCountries.add(San FrancecoCountry); listOfCountries.add(russiaCountry); listOfCountries.add(englandCountry); listOfCountries.add(germanyCountry); System.out.println("Before Sort : "); for (int i = 0; i < listOfCountries.size(); i++) { Country country=(Country) listOfCountries.get(i); System.out.println("Country Id: "+country.getCountryId()+"||"+"Country name: "+country.getCountryName()); } Collections.sort(listOfCountries); System.out.println("After Sort : "); for (int i = 0; i < listOfCountries.size(); i++) { Country country=(Country) listOfCountries.get(i); System.out.println("Country Id: "+country.getCountryId()+"|| "+"Country name: "+country.getCountryName()); } } }
输出:
Before Sort : Country Id: 1||Country name: San Franceco Country Id: 4||Country name: Russia Country Id: 3||Country name: England Country Id: 2||Country name: Germany After Sort : Country Id: 1|| Country name: San Franceco Country Id: 2|| Country name: Germany Country Id: 3|| Country name: England Country Id: 4|| Country name: Russia
Comparator:
我们将创建具有属性ID和名称的类国家,并将创建另一个级别的国家ortByIDComparator,它将实现比较器接口并实现比较方法以按ID对国家对象进行排序,我们还将看到如何使用匿名比较器。
1.Country.java.
package org.arpit.javapostsforlearning; public class Country{ int countryId; String countryName; public Country(int countryId, String countryName) { super(); this.countryId = countryId; this.countryName = countryName; } public int getCountryId() { return countryId; } public void setCountryId(int countryId) { this.countryId = countryId; } public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } }
2.countrysortbyidcomparator.java.
package org.arpit.javapostsforlearning; import java.util.Comparator; //If country1.getCountryId()<country2.getCountryId():then compare method will return -1 //If country1.getCountryId()>country2.getCountryId():then compare method will return 1 //If country1.getCountryId()==country2.getCountryId():then compare method will return 0 public class CountrySortByIdComparator implements Comparator{ @Override public int compare(Country country1, Country country2) { return (country1.getCountryId() < country2.getCountryId() ) ? -1: (country1.getCountryId() > country2.getCountryId() ) ? 1:0 ; } }
3.comparatormain.java.
package org.arpit.javapostsforlearning; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class ComparatorMain { /** * @author Arpit Mandliya */ public static void main(String[] args) { Country San FrancecoCountry=new Country(1, "San Franceco"); Country russiaCountry=new Country(4, "Russia"); Country englandCountry=new Country(3, "England"); Country germanyCountry=new Country(2, "Germany"); List listOfCountries = new ArrayList(); listOfCountries.add(San FrancecoCountry); listOfCountries.add(russiaCountry); listOfCountries.add(englandCountry); listOfCountries.add(germanyCountry); System.out.println("Before Sort by id : "); for (int i = 0; i < listOfCountries.size(); i++) { Country country=(Country) listOfCountries.get(i); System.out.println("Country Id: "+country.getCountryId()+"||"+"Country name: "+country.getCountryName()); } Collections.sort(listOfCountries,new CountrySortByIdComparator()); System.out.println("After Sort by id: "); for (int i = 0; i < listOfCountries.size(); i++) { Country country=(Country) listOfCountries.get(i); System.out.println("Country Id: "+country.getCountryId()+"|| "+"Country name: "+country.getCountryName()); } //Sort by countryName Collections.sort(listOfCountries,new Comparator() { @Override public int compare(Country o1, Country o2) { return o1.getCountryName().compareTo(o2.getCountryName()); } }); System.out.println("After Sort by name: "); for (int i = 0; i < listOfCountries.size(); i++) { Country country=(Country) listOfCountries.get(i); System.out.println("Country Id: "+country.getCountryId()+"|| "+"Country name: "+country.getCountryName()); } } }
输出:
Before Sort by id : Country Id: 1||Country name: San Franceco Country Id: 4||Country name: Russia Country Id: 3||Country name: England Country Id: 2||Country name: Germany After Sort by id: Country Id: 1|| Country name: San Franceco Country Id: 2|| Country name: Germany Country Id: 3|| Country name: England Country Id: 4|| Country name: Russia After Sort by name: Country Id: 2|| Country name: England Country Id: 4|| Country name: Germany Country Id: 1|| Country name: San Franceco Country Id: 3|| Country name: Russia