Set集合之TreeSet

TreeSet:可以对Set集合中的元素进行排序

排序是按照ascii来排序的。

import java.util.Iterator;import java.util.TreeSet;public class TreeSetDemo {public static void main(String[] args) {// TODO Auto-generated method stubmethod1();}public static void method1() {TreeSet ts = new TreeSet();ts.add("cba");ts.add("aaa");ts.add("bca");ts.add("abcd");Iterator it = ts.iterator();while (it.hasNext()) {System.out.println(it.next());}/** * 打印结果是: aaa abcd bca cba treeset中元素是有顺序的 是按照ascii码来排序的 */}}

自定义类Student,中有属性name和age,按照age从小到大排序。

import java.util.Iterator;import java.util.TreeSet;class Student {private String name;private int age;public Student(String name, int age) {super();this.name = name;this.age = age;}// set and get methodspublic 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 class TreeSetDemo {public static void main(String[] args) {// TODO Auto-generated method stubTreeSet ts = new TreeSet();ts.add(new Student("01", 10));ts.add(new Student("02", 15));ts.add(new Student("03", 9));ts.add(new Student("04", 18));Iterator it = ts.iterator();while (it.hasNext()){Student student = (Student) it.next();System.out.println(student.getName() +"::::"+student.getAge());}}}

运行发现出现错误:

Exception in thread "main" java.lang.ClassCastException: Student cannot be cast to java.lang.Comparableat java.util.TreeMap.compare(TreeMap.java:1290)at java.util.TreeMap.put(TreeMap.java:538)at java.util.TreeSet.add(TreeSet.java:255)at TreeSetDemo.main(TreeSetDemo.java:39)

因为TreeSet是一个有序的集合,它要知道它里边的元素是如何进行排列的,所以需要Student类实现

Comparable接口强行实现它的每个类的对象进行整体排序,这种排序被称为自然排序。此接口中只有一个方法compareToxuyao复写。

import java.util.Iterator;import java.util.TreeSet;class Student implements Comparable{private String name;private int age;public Student(String name, int age) {super();this.name = name;this.age = age;}// set and get methodspublic 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 int compareTo(Object o) {if (!(o instanceof Student))throw new RuntimeException("不是学生对象");Student student = (Student) o;if (this.age > student.age)return 1;if (this.age == student.age)return 0;return -1;}}public class TreeSetDemo {public static void main(String[] args) {// TODO Auto-generated method stubTreeSet ts = new TreeSet();ts.add(new Student("01", 10));ts.add(new Student("02", 15));ts.add(new Student("03", 9));ts.add(new Student("04", 18));Iterator it = ts.iterator();while (it.hasNext()){Student student = (Student) it.next();System.out.println(student.getName() +"::::"+student.getAge());}}}

通过复写这个方法,可以打印出结果:

03::::901::::1002::::1504::::18

但是当添加一个与已有元素年龄相同但是姓名不同的元素却添加不上。。

我们加上这么一个元素

ts.add(new Student("05", 18));

但是显示结果却没有它。因为在这个compareTo方法中认为年龄相同的就是同一个元素,,为此,当年龄相同时我们还要再做一次判断,判断其姓名是否也相同。

我们将compareTo方法修改如下:

public int compareTo(Object o) {if (!(o instanceof Student))throw new RuntimeException("不是学生对象");Student student = (Student) o;if (this.age > student.age)return 1;if (this.age == student.age) {return this.name.compareTo(student.name);}return -1;}

再次运行就会发现,05元素存入到集合中了。那么TreeSet是如何进行比较的呢?

我们在compareTo方法中添加这么一句:

System.out.println(this.name + "—–compaerTo—–" + student.name);

用来显示其运行过程。

public int compareTo(Object o) {if (!(o instanceof Student))throw new RuntimeException("不是学生对象");Student student = (Student) o;System.out.println(this.name + "—–compaerTo—–" + student.name);if (this.age > student.age)return 1;if (this.age == student.age) {return this.name.compareTo(student.name);}return -1;}

运行结果:

02—–compaerTo—–0103—–compaerTo—–0104—–compaerTo—–0104—–compaerTo—–0205—–compaerTo—–0105—–compaerTo—–0205—–compaerTo—–0403::::901::::1002::::1504::::1805::::18

通过这个结果,我们可了解到此程序的运行过程:

首先,01先存入

02与01进行比较,存入

03与01比较,存入

04与0102比较存入

05和010204比较存入

风不懂云的漂泊,天不懂雨的落魄,眼不懂泪的懦弱,

Set集合之TreeSet

相关文章:

你感兴趣的文章:

标签云: