标签:set private dem result hashcode rri pareto generate div
1.初步完成排序
重写equlas方法(hashCode()哈希值性能) toString hashset
1 public class Person { 2 3 //定义属性 4 private String name; 5 private int age; 6 7 8 9 @Override 10 public int hashCode() { 11 final int prime = 31; 12 int result = 1; 13 result = prime * result + age; 14 result = prime * result + ((name == null) ? 0 : name.hashCode()); 15 return result; 16 } 17 18 @Override 19 public boolean equals(Object obj) { 20 if (this == obj) 21 return true; 22 if (obj == null) 23 return false; 24 if (getClass() != obj.getClass()) 25 return false; 26 Person other = (Person) obj; 27 if (age != other.age) 28 return false; 29 if (name == null) { 30 if (other.name != null) 31 return false; 32 } else if (!name.equals(other.name)) 33 return false; 34 return true; 35 } 36 37 public String getName() { 38 return name; 39 } 40 41 public void setName(String name) { 42 this.name = name; 43 } 44 45 public int getAge() { 46 return age; 47 } 48 49 public void setAge(int age) { 50 this.age = age; 51 } 52 53 public Person() {} 54 55 public Person(String name,int age) { 56 super(); 57 this.name=name; 58 this.age=age; 59 60 } 61 62 @Override 63 public String toString() { 64 return name+".."+age; 65 } 66 67 }
import java.util.HashSet; public class PersonTest { /** * * @author Vivtol */ public static void main(String[] args) { // TODO Auto-generated method stub HashSet<Person> setperson = new HashSet<Person>(); setperson.add(new Person("a",11)); setperson.add(new Person("c",44)); setperson.add(new Person("b",22)); setperson.add(new Person("d",66)); setperson.add(new Person("b",22)); System.out.println(setperson); } }
输出结果:[d..66, a..11, c..44, b..22] 并没有达到需求
2.完善代码
Comparable<T>接口 重写comparaTo方法
package com.itvitol_01; public class Person implements Comparable<Person>{ //定义属性 private String name; private int age; @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Person() {} public Person(String name,int age) { super(); this.name=name; this.age=age; } @Override public String toString() { return name+".."+age; } @Override public int compareTo(Person p) { if(this.age>p.age) { return 1; }else if(this.age<p.age) { return -1; }else { return this.name.compareTo(p.name); } } }
package com.itvitol_01; import java.util.HashSet; import java.util.Set; import java.util.TreeSet; public class PersonTest { /** * * @author Vivtol */ public static void main(String[] args) { // TODO Auto-generated method stub // HashSet<Person> setperson = new HashSet<Person>(); Set<Person> setperson = new TreeSet<Person>(); setperson.add(new Person("a",11)); setperson.add(new Person("c",44)); setperson.add(new Person("b",22)); setperson.add(new Person("d",66)); setperson.add(new Person("b",22)); System.out.println(setperson); } }
输出结果:[a..11, b..22, c..44, d..66] 希望大家能完善一下我的代码哟!
标签:set private dem result hashcode rri pareto generate div
原文地址:https://www.cnblogs.com/987m/p/11228941.html