当前位置: 首页 > news >正文

网站建设方案报价费用明细价格免费开店的电商平台

网站建设方案报价费用明细价格,免费开店的电商平台,如何制作自己的网址,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
http://www.pierceye.com/news/990798/

相关文章:

  • 济南网络建站模板用c 做的网站怎么打开
  • 网站建设培训课程好人一生平安网站哪个好
  • seo怎么做网站的tdk网站优化的核心不包括
  • 如何做一份网站的数据分析网站营销案例
  • 中小企业网站建设公司个人微信号做网站行吗
  • 网站无法连接服务器哪些国家网站无须备案
  • 重庆做网站设计培训机构排名全国十大教育机构排名
  • 做网站建设销售网络营销推广技巧
  • 南宁网站制作定制北京网站seo服务
  • 门户网站网页设计规范willin kan 让你的wordpress飞起来
  • 建设银行广州招聘网站wordpress dz
  • 如何介绍自己做的网站东莞回收网站设计
  • 北京驾校网站建设厦门网页设计培训班
  • 网络公司给我做网站我有没有源代码版权吗我怎么做个人网站
  • 免费建站网站一站式做网站需要懂那些软件
  • 做新网站怎样提交360寻找销售团队外包
  • 重庆市建设网站wordpress 新闻模版
  • 国内网站建设推荐手工做的网站
  • 深圳罗湖做网站的公司网站建设与管理案例教程第三版课后答案
  • 有关网站招标商务标书怎么做做终端客户网站
  • c 网站做微信收款功能青岛网站建设定制
  • 贵州安顺建设主管部门网站网站全程设计技术
  • 公司宣传网站建设企业网站建设与实现的论文
  • 连云港网站建设推广网站的推广优化
  • 手机商城网站制作公司网站版面设计方案
  • 网站开发制作熊掌号网站推广方法主要有什么
  • 怎么查看网站的建设时间提高企业网站的访问率
  • 宁德做网站的公司长沙网站建设 芙蓉区
  • 兴平市住房和城乡建设局门户网站会员管理网站建设
  • 做seo的网站是怎么样的上饶小程序开发公司