TreeSet自然排序
阿新 • • 發佈:2019-02-17
package 容器類; import java.util.*; class A3 implements Comparable{ private int a; public A3(int a){ this.a = a; } public int compareTo(Object o) { A3 a3 = (A3)o; int result = a3.a>a ? 1:-1; return result; } public int getA() { return a; } } public class set3 { public static void main(String[] args) { TreeSet ts = new TreeSet(); ts.add(new A3(5)); ts.add(new A3(4)); ts.add(new A3(4)); System.out.println(ts.size()); Iterator<Object> it = ts.iterator(); System.out.println(ts.size()); while(it.hasNext()){ A3 a2 = (A3)it.next(); System.out.println(a2.getA()); } } }