自定义实体判断相等和比较大小
通过实体部分属性判断相等或比较大小
1. 判断自定义的实体的实例相等
a) 重写 hashCode() 和 equals() 两个方法
b) 具体举例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Stu{ 2 private String name = null; 3 private int age = 0; 4 5 public Stu(String name, int age){ 6 this.name = name; 7 this.age = age; 8 } 9 //覆写Object中的equals方法 10 public boolean equals(Object obj){ 11 if(this == obj){ 12 return true; 13 } 14 if(!(obj instanceof Stu)){ 15 return false; 16 } 17 Stu stu = (Stu)obj; 18 if((stu.age == this.age) && this.name.equals(stu.name)){ 19 return true; 20 } 21 return false; 22 } 23 //覆写Object中的hashCode方法 24 public int hashCode(){ 25 return this.name.hashCode() * this.age; 26 } 27 }
2. 通过部分属性(一个或多个字段)比较实体实例的大小
1) 让自定义类实现Comparable接口
实现Comparable接口并重写compareTo()方法, 在compareTo()方法中指明如何排序。
i. 实现Comparable接口,具体举例A:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Goods implements Comparable { 2 private String name; 3 private double price; 4 5 public Goods() { 6 } 7 8 public Goods(String name, double price) { 9 this.name = name; 10 this.price = price; 11 } 12 13 public String getName() { 14 return name; 15 } 16 17 public void setName(String name) { 18 this.name = name; 19 } 20 21 public double getPrice() { 22 return price; 23 } 24 25 public void setPrice(double price) { 26 this.price = price; 27 } 28 29 @Override 30 public String toString() { 31 return "Goods{" + 32 "name='" + name + '\'' + 33 ", price=" + price + 34 '}'; 35 } 36 37 //指明商品比较大小的方式,按照价格从低到高排序,如果出现价格相同的,再按照产品名称从低到高排序 38 @Override 39 public int compareTo(Object o) { 40 if(o instanceof Goods ){ 41 Goods goods =(Goods)o; 42 if (this.price > goods.price) { 43 return 1; 44 }else if(this.price < goods.price){ 45 return -1; 46 }else 47 //return 0; 48 return this.name.compareTo(goods.name); 49 } 50 throw new RuntimeException("传入的数据类型不一致"); 51 } 52 }
2) 指定Comparator方式排序
传入Comparator并在实例对象中重写compare(Object o1,Object o2)方法,比较o1和o2的大小,如果方法返回正整数,则表示o1大于o2,如果返回0,表示二者相等,如果返回负整数,表示o1小于o2.
ii. 传入Comparator接口,具体举例B
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 // 运用集合工具类 Collections.sort() 2 @Test 3 public void test1(){ 4 Map<Integer,String> map = new TreeMap <>(); 5 map.put(5, "a"); 6 map.put(3, "c"); 7 map.put(4, "b"); 8 map.put(2, "d"); 9 map.put(1, "e"); 10 List<Entry<Integer,String>> list = new ArrayList<>(map.entrySet()); 11 Collections.sort(list, new Comparator<Entry<Integer,String>>() { 12 @Override 13 public int compare(Entry<Integer,String> o1, Entry<Integer,String> o2) { 14 // TODO Auto-generated method stub 15 return o1.getValue().compareTo(o2.getValue()); 16 } 17 }); 18 for(Entry<Integer,String> aEntry : list) { 19 System.out.println(aEntry.getKey()+":"+aEntry.getValue()); 20 } 21 22 /* 23 运行结果: 24 5:a 25 4:b 26 3:c 27 2:d 28 1:e 29 */ 30 } 31 32 // 运用数组工具类 Arrays.sort() 33 @Test 34 public void test2(){ 35 Goods[] arr=new Goods[5]; 36 arr[0] = new Goods("lenovoMouse",34); 37 arr[1] = new Goods("dellMouse",66); 38 arr[2] = new Goods("xiaomiMouse",50); 39 arr[3] = new Goods("hahaMouse",66); 40 arr[4] = new Goods("hahaMouse",166); 41 42 Arrays.sort(arr, new Comparator() { 43 //指明商品比较大小的方式,按照产品名称从低到高排序,再按照价格从高到低排序 44 @Override 45 public int compare(Object o1, Object o2) { 46 if(o1 instanceof Goods && o2 instanceof Goods){ 47 48 Goods g1=(Goods)o1; 49 Goods g2=(Goods)o2; 50 if(g1.getName().equals(g2.getName())){ 51 return -Double.compare(g1.getPrice(),g2.getPrice()); 52 }else { 53 return g1.getName().compareTo(g2.getName()); 54 } 55 } 56 throw new RuntimeException("输入的数据类型不一致"); 57 } 58 }); 59 60 System.out.println(Arrays.toString(arr)); 61 62 /* 63 运行结果: 64 [Goods{name='dellMouse', price=66.0}, Goods{name='hahaMouse', price=166.0}, 65 Goods{name='hahaMouse', price=66.0}, Goods{name='lenovoMouse', price=34.0}, 66 Goods{name='xiaomiMouse', price=50.0}] 67 */ 68 }
3) Comparable接口与Comparator接口的使用的对比
Comparable接口的方式一旦指定,保证Comparable接口实现类的对象在任何位置都可以比较大小。
Comparator接口属于临时性的比较。
引用资料: