HasSet让元素有按照hash排序 LinkSet让元素的顺序按照输入顺序排列 Treeset让元素可以排序可以实现Comparator接口实现对象的对比或者自己在类里面实现comparable接口的方法 package test; import java.util.*; import javax.imageio.event.IIOReadWarningListener; import javax.security.auth.kerberos.KerberosKey; import privateclass.*; import java.io.*; import java.math.BigInteger; class lop { public void te() { System.out.println("yey"); } } class Animal extends lop { int num = 4; public void eat() { } } class Dog extends Animal { int num = 5; } interface kk { public void wo(); } interface dd { public void wo(); } class poue implements kk,dd{ @Override public void wo() { // TODO Auto-generated method stub } } public class Main { private static final String space_operator = " "; public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(new BufferedInputStream(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); TreeSet has = new TreeSet(new ComparaorName()); has.add(new Person("zhangsan1",21)); has.add(new Person("zhangsan2",31)); has.add(new Person("zhangsan3",41)); has.add(new Person("lhangsan4",51)); has.add(new Person("zhangsan1",11)); for(Iterator it = has.iterator(); it.hasNext();) { Object object = it.next(); System.out.println(object.toString()); } } } package privateclass; public class Person implements Comparable{ private String naem; private int age; public String getNaem() { return naem; } public void setNaem(String naem) { this.naem = naem; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Person(String naem, int age) { super(); this.naem = naem; this.age = age; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((naem == null) ? 0 : naem.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 (naem == null) { if (other.naem != null) return false; } else if (!naem.equals(other.naem)) return false; return true; } public Person() { super(); } @Override public String toString() { return "Person [naem=" + naem + ", age=" + age + "]"; } @Override public int compareTo(Object o) { Person p = (Person) o; int temp = this.age - p.age; return temp == 0 ? this.naem.compareTo(p.naem) : temp; } } package privateclass; import java.util.Comparator; public class ComparaorName implements Comparator { @Override public int compare(Object o1, Object o2) { Person p1 = (Person) o1; Person p2 = (Person) o2; int temp = p1.getNaem().compareTo(p2.getNaem()); return temp == 0 ? p1.getAge() - p2.getAge() : temp; } public static void main(String[] args) { // TODO Auto-generated method stub } }