网站建设方案报价费用明细价格,免费开店的电商平台,如何制作自己的网址,wordpress 归档链接常见的面试问题之一是“比较器和可比较器之间有什么区别”。 或“您将如何通过其ID或名称对员工对象集合进行排序”。为此#xff0c;我们可以使用两个接口#xff0c;即Comparator和Comparable。在我们真正看到差异之前#xff0c;让我简要介绍一下两者。 可比接口#x… 常见的面试问题之一是“比较器和可比较器之间有什么区别”。 或“您将如何通过其ID或名称对员工对象集合进行排序”。为此我们可以使用两个接口即Comparator和Comparable。在我们真正看到差异之前让我简要介绍一下两者。 可比接口要对其进行排序的对象的类必须实现此接口。在此我们必须实现compareToObject方法。 例如 public class Country implements Comparable{Overridepublic int compareTo(Object arg0) {Country country(Country) arg0;return (this.countryId country.countryId ) ? -1: (this.countryId country.countryId ) ? 1:0 ;
}} 如果任何类实现可比较的接口则可以使用Collection.sort或Arrays.sort自动对该对象的集合进行排序。对象将基于该类中的compareTo方法进行排序。 在Java中实现Comparable的对象可以用作SortedMap如TreeMap或SortedSet如TreeSet中的键而无需实现任何其他接口。 Comparator接口需要对其进行排序的对象的类无需实现此接口。某些第三类可以实现此接口进行排序。egCountrySortByIdComparator类可以实现Comparator接口以按ID对国家对象的集合进行排序。 例如 public class CountrySortByIdComparator implements ComparatorCountry{Overridepublic int compare(Country country1, Country country2) {return (country1.getCountryId() country2.getCountryId() ) ? -1: (country1.getCountryId() country2.getCountryId() ) ? 1:0 ;}} 使用Comparator界面我们可以根据要排序的对象的不同属性编写不同的排序。您可以使用匿名比较器在特定代码行进行比较。 例如 Country indiaCountrynew Country(1, India);Country chinaCountrynew Country(4, China);Country nepalCountrynew Country(3, Nepal);Country bhutanCountrynew Country(2, Bhutan);ListCountry listOfCountries new ArrayListCountry();listOfCountries.add(indiaCountry);listOfCountries.add(chinaCountry);listOfCountries.add(nepalCountry);listOfCountries.add(bhutanCountry); //Sort by countryNameCollections.sort(listOfCountries,new ComparatorCountry() {Overridepublic int compare(Country o1, Country o2) {return o1.getCountryName().compareTo(o2.getCountryName());}}); 比较器vs可比 参数 可比 比较器 排序逻辑 排序逻辑必须在要对其对象进行排序的同一类中。 因此这称为对象的自然排序 排序逻辑在单独的类中。 因此我们可以根据要排序的对象的不同属性编写不同的排序。 例如使用ID名称等进行排序 实作 对其对象进行排序的类必须实现此接口。例如Country类需要实现与id对应的country对象的收集类似的实现 要排序对象的类不需要实现此接口。其他一些类也可以实现此接口。 Eg-CountrySortByIdComparator类可以实现Comparator接口以按ID对国家/地区对象的集合进行排序 排序方式 int compareToObject o1 该方法将该对象与o1对象进行比较并返回一个整数其值具有以下含义 1.正数–该对象大于o1 2.零–此对象等于o1 3.负数–该对象小于o1 int compare对象o1对象o2 此方法比较o1和o2对象。 并返回一个整数。其值具有以下含义。 1.正数– o1大于o2 2.零– o1等于o2 3.负数– o1小于o1 调用方式 Collections.sort列表 在这里对象将根据CompareTo方法进行排序 Collections.sort列表比较器 在这里对象将根据Comparator中的Compare方法进行排序 包 Java.lang.Comparable Java.util.Comparator Java代码 对于Comparable我们将创建具有属性ID和名称的country类该类将实现Comparable接口并实现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.countryIdcountry.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;}Overridepublic int compareTo(Object arg0) {Country country(Country) arg0;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.ComparatorMain.java package org.arpit.javapostsforlearning;import java.util.ArrayList;
import java.util.Collections;
import java.util.List;public class ComparatorMain {/*** author Arpit Mandliya*/public static void main(String[] args) {Country indiaCountrynew Country(1, India);Country chinaCountrynew Country(4, China);Country nepalCountrynew Country(3, Nepal);Country bhutanCountrynew Country(2, Bhutan);ListCountry listOfCountries new ArrayListCountry();listOfCountries.add(indiaCountry);listOfCountries.add(chinaCountry);listOfCountries.add(nepalCountry);listOfCountries.add(bhutanCountry);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: India
Country Id: 4||Country name: China
Country Id: 3||Country name: Nepal
Country Id: 2||Country name: Bhutan
After Sort :
Country Id: 1|| Country name: India
Country Id: 2|| Country name: Bhutan
Country Id: 3|| Country name: Nepal
Country Id: 4|| Country name: China 对于Comparator我们将创建具有属性ID和名称的country类并创建另一个CountryAortByIdComparator类该类将实现Comparator接口并实现compare方法以按ID对国家对象的集合进行排序并且还将看到如何使用匿名比较器。 1.国家/地区 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 0public class CountrySortByIdComparator implements ComparatorCountry{Overridepublic int compare(Country country1, Country country2) {return (country1.getCountryId() country2.getCountryId() ) ? -1: (country1.getCountryId() country2.getCountryId() ) ? 1:0 ;}} 3ComparatorMain.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 indiaCountrynew Country(1, India);Country chinaCountrynew Country(4, China);Country nepalCountrynew Country(3, Nepal);Country bhutanCountrynew Country(2, Bhutan);ListCountry listOfCountries new ArrayListCountry();listOfCountries.add(indiaCountry);listOfCountries.add(chinaCountry);listOfCountries.add(nepalCountry);listOfCountries.add(bhutanCountry);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 countryNameCollections.sort(listOfCountries,new ComparatorCountry() {Overridepublic 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: India
Country Id: 4||Country name: China
Country Id: 3||Country name: Nepal
Country Id: 2||Country name: Bhutan
After Sort by id:
Country Id: 1|| Country name: India
Country Id: 2|| Country name: Bhutan
Country Id: 3|| Country name: Nepal
Country Id: 4|| Country name: China
After Sort by name:
Country Id: 2|| Country name: Bhutan
Country Id: 4|| Country name: China
Country Id: 1|| Country name: India
Country Id: 3|| Country name: Nepal 参考 JCG合作伙伴 Arpit Mandliya在Java框架和面向初学者博客的设计模式中的 比较器与Java中的Comparable之间的区别 。 翻译自: https://www.javacodegeeks.com/2013/03/difference-between-comparator-and-comparable-in-java.html