标签:style java color 使用 strong os
Set:无序,不可以重复元素。在集合初始化时,就有了比较方式。
示例:需求:
往TreeSet集合中存储自定义对象学生,按照学生的年龄进行排序。
package tan; import java.util.Iterator; import java.util.TreeSet; public class TreeSetDemo { public static void main(String[] args) { TreeSet ts=new TreeSet(); ts.add(new Student("tan1", 21)); ts.add(new Student("tan3", 20)); ts.add(new Student("tan2", 23)); ts.add(new Student("tan5", 21)); Iterator it=ts.iterator(); while(it.hasNext()){ System.out.println(it.next()); } } } class Student implements Comparable{ private String name; private Integer age; public Student(String name, int age) { this.name = name; this.age = age; } 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; } @Override public String toString() { return "name:"+this.name+" "+"age:"+this.age; } @Override public int compareTo(Object obj) { if(!(obj instanceof Student))throw new RuntimeException("非学生对象"); Student s=(Student)obj; if(this.age>s.age) return 1; //排序时,当主要条件相同时,一定判断一下次要条件。 if(this.age==s.age) { return this.name.compareTo(s.name); } return -1; } }
package tan; import java.util.Comparator; import java.util.Iterator; import java.util.TreeSet; public class TreeSetDemo { public static void main(String[] args) { //在这里面可以传入自定义比较器 TreeSet ts=new TreeSet(new StudentAgeComparator()); ts.add(new Student("tan01", 21)); ts.add(new Student("tan03", 20)); ts.add(new Student("tan03", 22)); ts.add(new Student("tan0012", 23)); ts.add(new Student("tan007", 21)); Iterator it=ts.iterator(); while(it.hasNext()){ System.out.println(it.next()); } } } class Student implements Comparable{ private String name; private Integer age; public Student(String name, int age) { this.name = name; this.age = age; } 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; } @Override public String toString() { return "name:"+this.name+" "+"age:"+this.age; } @Override public int compareTo(Object obj) { if(!(obj instanceof Student))throw new RuntimeException("非学生对象"); Student s=(Student)obj; if(this.age>s.age) return 1; //排序时,当主要条件相同时,一定判断一下次要条件。 if(this.age==s.age) { return this.name.compareTo(s.name); } return -1; } } //自定义姓名比较器 class StudentNameComparator implements Comparator{ @Override public int compare(Object o1, Object o2) { Student s1=(Student)o1; Student s2=(Student)o2; int num=s1.getName().compareTo(s2.getName()); if(num==0){ //因为Ingteger已经实现了comparable接口 return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge())); //也可以这样写 /* if(s1.getAge()>s2.getAge()) return 1; if(s1.getAge()==s2.getAge()) return 0; return -1; */ } return num; } } //自定义年龄比较器 class StudentAgeComparator implements Comparator<Student>{ @Override public int compare(Student o1, Student o2) { int i=o1.getAge()-o2.getAge(); return i; } }
package tan; import java.util.*; public class TreeSetTest { public static void main(String[] args) { TreeSet ts = new TreeSet(new StrLengthComparator()); ts.add("abcd"); ts.add("cc"); ts.add("cba"); ts.add("aaa"); ts.add("z"); ts.add("hahaha"); Iterator it = ts.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } } class StrLengthComparator implements Comparator { @Override public int compare(Object o1, Object o2) { String s1 = (String) o1; String s2 = (String) o2; int num = new Integer(s1.length()).compareTo(new Integer(s2.length())); //当第一条件满足时,判断第二个条件按照自然顺序排序 if(num==0){ return s1.compareTo(s2); } return num; } }
TreeSet排序,存储自定义对象,自定义比较器示例,布布扣,bubuko.com
标签:style java color 使用 strong os
原文地址:http://blog.csdn.net/u010834071/article/details/37990491