爪哇中Comparator和Comparable之间的区别
可比接口:
要对其对象进行排序的类必须实现此接口。在此,我们必须实现compareTo(Object)方法。
例如:
1 2 3 4 5 6 7 |
上市 类 国家 实施 可比{ @覆写 上市 整型 相比于(国家 国家) { 返回 (这个.国家Id < 国家.国家Id ) ? -1: (这个.国家Id > 国家.国家Id ) ? 1:0 ; }} |
如果任何类实现可比的接口,则可以使用Collection.sort()或Arrays.sort()自动对该对象的集合进行排序。对象将基于该类中的compareTo方法进行排序。
在Java中实现Comparable的对象可以用作TreeMap之类的SortedMap或TreeSet之类的SortedSet中的键,而无需实现任何其他接口。
比较器界面:
要排序对象的类不需要实现此接口。某些第三类可以实现此接口进行排序。例如,CountrySortByIdComparator类可以实现Comparator接口以按ID对国家对象的集合进行排序。例如:
1 2 3 4 5 6 7 8 9 10 11 |
上市 类 国家SortByIdComparator 实施 比较器{ @覆写 上市 整型 相比(国家 国家1, 国家 国家2) { 返回 (国家1.得到CountryId() < 国家2.得到CountryId() ) ? -1: (国家1.得到CountryId() > 国家2.得到CountryId() ) ? 1:0 ; } } |
Using 比较器 整型erface,we can write different 分类ing based on different attributes of objects to be 分类ed.You can use anonymous comparator to 相比 at particular line of 码. 例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
国家 印度国家=新 国家(1, “印度”); 国家 俄罗斯国家=新 国家(4, “俄国”); 国家 英国国家=新 国家(3, “英国”); 国家 德国国家=新 国家(2, “德国”); 清单 listOfCountries = 新 数组列表(); listOfCountries.加(印度国家); listOfCountries.加(俄罗斯国家); listOfCountries.加(英国国家); listOfCountries.加(德国国家); //按国家/地区名称排序 馆藏.分类(listOfCountries,新 比较器() { @覆写 上市 整型 相比(国家 o1, 国家 o2) { 返回 o1.得到CountryName().相比于(o2.得到CountryName()); } }); |
比较器vs可比
参数
|
可比
|
比较器
|
---|---|---|
排序逻辑
|
排序逻辑必须在要对其对象进行排序的同一类中。因此,这称为对象的自然排序
|
排序逻辑在单独的类中。因此,我们可以根据要排序的对象的不同属性编写不同的排序。例如。使用ID,名称等进行排序
|
实作
|
要对其对象进行排序的类必须实现此接口。例如,国家/地区类需要实现与按ID收集国家/地区对象相当的类 |
要排序对象的类不需要实现此接口。其他一些类也可以实现此接口。例如-CountrySortByIdComparator类可以实现Comparator接口以按ID对国家/地区对象的集合进行排序
|
整型 相比于(Object o1)
此方法将该对象与o1对象进行比较并返回整数,其值具有以下含义 1.正数–该对象大于o1 2.零–此对象等于o1 3.负数–该对象小于o1 |
整型 相比(对象o1,对象o2)
此方法比较o1和o2对象。并返回一个整数,其值具有以下含义。 1.正数– o1大于o2 2.零– o1等于o2 3.负数– o1小于o1 |
|
调用方式
|
馆藏.sort(列表)
在这里,对象将根据CompareTo方法进行排序 |
馆藏.sort(列表,比较器)
在这里,对象将根据Comparator中的Compare方法进行排序 |
包
|
爪哇.lang.Comparable
|
爪哇.util.Comparator
|
爪哇 码:
对于可比:
我们将创建具有属性ID和名称的country类,该类将实现Comparable接口并实现CompareTo方法以按ID对国家对象的集合进行排序。
1. 国家.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
包 组织.Arpit.爪哇postsforlearning; //如果this.cuntryId<country.countryId:然后compare方法将返回-1 //如果this.countryId>country.countryId:然后compare方法将返回1 //如果this.countryId == 国家.countryId:则compare方法将返回0 上市 类 国家 实施 可比{ 整型 国家Id; 串 国家的名字; 上市 国家(整型 国家Id, 串 国家的名字) { 超(); 这个.国家Id = 国家Id; 这个.国家的名字 = 国家的名字; } @覆写 上市 整型 相比于(国家 国家) { 返回 (这个.国家Id < 国家.国家Id ) ? -1: (这个.国家Id > 国家.国家Id ) ? 1:0 ; } 上市 整型 得到CountryId() { 返回 国家Id; } 上市 虚空 setCountryId(整型 国家Id) { 这个.国家Id = 国家Id; } 上市 串 得到CountryName() { 返回 国家的名字; } 上市 虚空 setCountryName(串 国家的名字) { 这个.国家的名字 = 国家的名字; } } |
2.ComparableMain.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
包 组织.Arpit.爪哇postsforlearning; 进口 爪哇.实用程序.数组列表; 进口 爪哇.实用程序.馆藏; 进口 爪哇.实用程序.清单; 上市 类 可比主要 { / ** * @作者Arpit Mandliya */ 上市 静态的 虚空 主要(串[] args) { 国家 印度国家=新 国家(1, “印度”); 国家 俄罗斯国家=新 国家(4, “俄国”); 国家 英国国家=新 国家(3, “英国”); 国家 德国国家=新 国家(2, “德国”); 清单 listOfCountries = 新 数组列表(); listOfCountries.加(印度国家); listOfCountries.加(俄罗斯国家); listOfCountries.加(英国国家); listOfCountries.加(德国国家); 系统.出.打印(“在排序之前 : "); 对于 (整型 i = 0; i < listOfCountries.尺寸(); i++) { 国家 国家=(国家) listOfCountries.得到(i); 系统.出.打印(“国家/地区ID:”+国家.得到CountryId()+“ || ”+“国家的名字: ”+国家.得到CountryName()); } 馆藏.分类(listOfCountries); 系统.出.打印(“排序后 : "); 对于 (整型 i = 0; i < listOfCountries.尺寸(); i++) { 国家 国家=(国家) listOfCountries.得到(i); 系统.出.打印(“国家/地区ID:”+国家.得到CountryId()+“ || ”+“国家的名字: ”+国家.得到CountryName()); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
之前 分类 : 国家 ID: 1|| 国家 名称: 印度 国家 ID: 4|| 国家 名称: 俄国 国家 ID: 3|| 国家 名称: 英国 国家 ID: 2|| 国家 名称: 德国 后 分类 : 国家 ID: 1|| 国家 名称: 印度 国家 ID: 2|| 国家 名称: 德国 国家 ID: 3|| 国家 名称: 英国 国家 ID: 4|| 国家 名称: 俄国 |
对于比较器:
我们将创建具有属性ID和名称的country类,并创建另一个类CountrySortByIdComparator,该类将实现Comparator接口并实现compare方法以按ID对国家对象的集合进行排序,并且还将看到如何使用匿名比较器。
1.国家/地区
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
包 组织.Arpit.爪哇postsforlearning; 上市 类 国家{ 整型 国家Id; 串 国家的名字; 上市 国家(整型 国家Id, 串 国家的名字) { 超(); 这个.国家Id = 国家Id; 这个.国家的名字 = 国家的名字; } 上市 整型 得到CountryId() { 返回 国家Id; } 上市 虚空 setCountryId(整型 国家Id) { 这个.国家Id = 国家Id; } 上市 串 得到CountryName() { 返回 国家的名字; } 上市 虚空 setCountryName(串 国家的名字) { 这个.国家的名字 = 国家的名字; } } |
2.CountrySortbyIdComparator.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
包 组织.Arpit.爪哇postsforlearning; 进口 爪哇.实用程序.比较器; //如果country1.getCountryId()<country2.getCountryId():then 相比 method will 返回 -1 //如果country1.getCountryId()>country2.getCountryId():然后compare方法将返回1 //如果country1.getCountryId()==country2.getCountryId():then 相比 method will 返回 0 上市 类 国家SortByIdComparator 实施 比较器{ @覆写 上市 整型 相比(国家 国家1, 国家 国家2) { 返回 (国家1.得到CountryId() < 国家2.得到CountryId() ) ? -1: (国家1.得到CountryId() > 国家2.得到CountryId() ) ? 1:0 ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
包 组织.Arpit.爪哇postsforlearning; 进口 爪哇.实用程序.数组列表; 进口 爪哇.实用程序.馆藏; 进口 爪哇.实用程序.比较器; 进口 爪哇.实用程序.清单; 上市 类 比较器主要 { / ** * @作者Arpit Mandliya */ 上市 静态的 虚空 主要(串[] args) { 国家 印度国家=新 国家(1, “印度”); 国家 俄罗斯国家=新 国家(4, “俄国”); 国家 英国国家=新 国家(3, “英国”); 国家 德国国家=新 国家(2, “德国”); 清单 listOfCountries = 新 数组列表(); listOfCountries.加(印度国家); listOfCountries.加(俄罗斯国家); listOfCountries.加(英国国家); listOfCountries.加(德国国家); 系统.出.打印(“在按ID排序之前:”); 对于 (整型 i = 0; i < listOfCountries.尺寸(); i++) { 国家 国家=(国家) listOfCountries.得到(i); 系统.出.打印(“国家/地区ID:”+国家.得到CountryId()+“ || ”+“国家的名字: ”+国家.得到CountryName()); } 馆藏.分类(listOfCountries,新 国家SortByIdComparator()); 系统.出.打印(“按ID排序后:”); 对于 (整型 i = 0; i < listOfCountries.尺寸(); i++) { 国家 国家=(国家) listOfCountries.得到(i); 系统.出.打印(“国家/地区ID:”+国家.得到CountryId()+“ || ”+“国家的名字: ”+国家.得到CountryName()); } //按国家/地区名称排序 馆藏.分类(listOfCountries,新 比较器() { @覆写 上市 整型 相比(国家 o1, 国家 o2) { 返回 o1.得到CountryName().相比于(o2.得到CountryName()); } }); 系统.出.打印(“按名称排序后:”); 对于 (整型 i = 0; i < listOfCountries.尺寸(); i++) { 国家 国家=(国家) listOfCountries.得到(i); 系统.出.打印(“国家/地区ID:”+国家.得到CountryId()+“ || ”+“国家的名字: ”+国家.得到CountryName()); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
之前 分类 通过 ID : 国家 ID: 1|| 国家 名称: 印度 国家 ID: 4|| 国家 名称: 俄国 国家 ID: 3|| 国家 名称: 英国 国家 ID: 2|| 国家 名称: 德国 后 分类 通过 ID: 国家 ID: 1|| 国家 名称: 印度 国家 ID: 2|| 国家 名称: 德国 国家 ID: 3|| 国家 名称: 英国 国家 ID: 4|| 国家 名称: 俄国 后 分类 通过 名称: 国家 ID: 2|| 国家 名称: 英国 国家 ID: 4|| 国家 名称: 德国 国家 ID: 1|| 国家 名称: 印度 国家 ID: 3|| 国家 名称: 俄国 |
请通过 爪哇面试的前50个核心问题 了解更多面试问题。