java引用数据类型的排序总结(内置类,自定义类,容器类)
2015-07-05 21:57
531 查看
内置类
整数、小数 Integer Float Double 直接比较基本数据类型的大小字符 :比较的unicode码之差
字符串:
1)、如果其中一个是领外一个起始开始的子串,返回长度之差
2)、否则返回第一个不相等的unicode码之差
java.util.Date:根据日期的长整形数比较
package com.bjsxt.sort.innerType; /** * 内置引用数据类型(常用)的比较 * @author Administrator * */ public class Demo01 { /** * @param args */ public static void main(String[] args) { Integer a ; //根据基本数据类型大小 Character ch; //根据Unicode编码顺序 String str="abc"; //如果其中一个是例外一个起始开始的子串,返回长度之差 String str2 ="abcd123"; //否则返回第一个不相等的unicode码之差 System.out.println(str.compareTo(str2));//-4 str ="abc"; str2 ="aad"; System.out.println(str.compareTo(str2));//1 java.util.Date d ; //根据日期的长整形数比较 } }
自定义类
实体类 实现java.lang.Comparable接口 重写compareTo方法业务排序类 单独写一个java.util.Comparator类重写compare方法
1)解耦:与实体类分类
2)方便:应对多变的排序规则
示例代码:
新闻信息:时间排序 、点击量 、标题 (实现java.lang.Comparable接口 重写compareTo方法)
新闻条目实体类
import java.text.SimpleDateFormat; import java.util.Date; /** * 新闻条目实体类 * @author Administrator * */ public class NewsItem implements java.lang.Comparable<NewsItem>{ //标题 private String title; //点击量 private int hits; //时间 private Date pubTime; public NewsItem() { } public NewsItem(String title, int hits, Date pubTime) { super(); this.title = title; this.hits = hits; this.pubTime = pubTime; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getHits() { return hits; } public void setHits(int hits) { this.hits = hits; } public Date getPubTime() { return pubTime; } public void setPubTime(Date pubTime) { this.pubTime = pubTime; } //时间降序 +点击量升序+标题降序 @Override public int compareTo(NewsItem o) { int result =0; //比较 时间 result =-this.pubTime.compareTo(o.pubTime); //降序 if(0==result){ //时间相同 //点击量 result =this.hits-o.hits; //升序 if(0==result){ //点击量相同 //标题 result=-this.title.compareTo(o.title);//降序 } } return result; } @Override public String toString() { StringBuilder sb =new StringBuilder(); sb.append("标题:").append(this.title); sb.append(",时间:").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(this.pubTime)); sb.append(",点击量:").append(this.hits).append("\n"); return sb.toString(); } }
商品: 价格 、收藏量 (单独写一个java.util.Comparator类重写compare方法)
示例代码:
商品实体类
package com.bjsxt.sort.refType; /** * 实体类 * @author Administrator * */ public class Goods { //商品名称 private String name; //价格 private double price; //收藏量 private int fav; public Goods() { // TODO Auto-generated constructor stub } public Goods(String name, double price, int fav) { super(); this.name = name; this.price = price; this.fav = fav; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getFav() { return fav; } public void setFav(int fav) { this.fav = fav; } @Override public String toString() { return "商品名:"+name+",收藏量"+this.fav+",价格:"+this.price+"\n"; } }
按收藏量的排序器
package com.bjsxt.sort.refType; /** * 按收藏量排序的业务类 (升序) * @author Administrator * */ public class GoodsFavComp implements java.util.Comparator<Goods> { @Override public int compare(Goods o1, Goods o2) { return o1.getFav()-o2.getFav(); } }
按价格的排序器
package com.bjsxt.sort.refType; /** * 按价格排序的业务类 (降序) * @author Administrator * */ public class GoodsPriceComp implements java.util.Comparator<Goods> { @Override public int compare(Goods o1, Goods o2) { return -(o1.getPrice()-o2.getPrice()>0?1:(o1.getPrice()==o2.getPrice()?0:-1)); } }
容器类
TreeSet:数据元素可以排序且不可重复对比:Set接口:HashSet,元素必须重写 hashcode和equals方法。
去重:比较等于0即重复
1)、元素可以排序 java.lang.Comparable +compareTo new TreeSet()
2)、排序业务类 java.util.Comparator +compare new TreeSet(Comparator
package com.bjsxt.sort.col; public class Person { private final String name;//名称 private final int handsome;//颜值 public Person() { name =null; handsome =0; } public Person(String name, int handsome) { super(); this.name = name; this.handsome = handsome; } public String getName() { return name; } public int getHandsome() { return handsome; } @Override public String toString() { return "姓名:"+this.name+",颜值:"+this.handsome+"\n"; } }
package com.bjsxt.sort.col; import java.util.TreeSet; /** * 提供了 解耦的方式:业务排序类 * @author Administrator * */ public class TreeSetDemo { /** * @param args */ public static void main(String[] args) { Person p1 =new Person("tom",100); Person p2 =new Person("jerry",1000); Person p3 =new Person("jack",1200); Person p4 =new Person("honey",50); //依次存放到TreeSet容器中,使用排序的业务类(匿名内部类) TreeSet<Person> persons =new TreeSet<Person>( new java.util.Comparator<Person>(){ @Override public int compare(Person o1, Person o2) { return -(o1.getHandsome()-o2.getHandsome()); } } ); persons.add(p1); //TreeSet 在添加数据时排序 persons.add(p2); persons.add(p3); persons.add(p4); System.out.println(persons); /* //改变数据 p4.setHandsome(100); p4.setName("tom"); */ //p4 与p1 内容重复 System.out.println(persons); } }
package com.bjsxt.sort.col; import java.util.TreeSet; /** * 提供了 解耦的方式:业务排序类 * @author Administrator * */ public class TreeSetDemo { /** * @param args */ public static void main(String[] args) { Person p1 =new Person("tom",100); Person p2 =new Person("jerry",1000); Person p3 =new Person("jack",1200); Person p4 =new Person("honey",50); //依次存放到TreeSet容器中,使用排序的业务类(匿名内部类) TreeSet<Person> persons =new TreeSet<Person>( new java.util.Comparator<Person>(){ @Override public int compare(Person o1, Person o2) { return -(o1.getHandsome()-o2.getHandsome()); } } ); persons.add(p1); //TreeSet 在添加数据时排序 persons.add(p2); persons.add(p3); persons.add(p4); System.out.println(persons); /* //改变数据 p4.setHandsome(100); p4.setName("tom"); */ //p4 与p1 内容重复 System.out.println(persons); } }
相关文章推荐
- [LeetCode][Java] Longest Substring Without Repeating Characters
- JAVA面向对象第二天
- Java核心技术I——基础点拾掇(第六章)
- Java - Thinking in Java 第2章 一切都是对象
- scala eclipse plugin 插件安装
- 你应该更新的 Java 知识之常用程序库【转载】
- JAVA面向对象第一天
- Spring Mvc+ajax发送请求参数接收问题
- JAVA-反射-getGenericSuperclass()介绍
- Java程序设计日志一
- MyBatis查询传一个参数时报错:There is no getter for property named 'sleevetype' in 'class java.lang.Integer
- Eclipse主题更换方法
- java 线程基础知识
- [LeetCode][Java] Add Two Numbers
- 关于mac版eclipse的配置问题
- javaSE学习笔记之反射
- Eclipse关闭检查
- java 错误:由于文件名不对所造成
- eclipse 远程调试mapreduce
- [LeetCode][Java] Two Sum