欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Comparable 和 TreeSet

程序员文章站 2022-05-14 13:07:06
...
import java.util.Set;
import java.util.TreeSet;

/**
 * 测试TreeSet的使用(实际使用TreeMap实现的)
 * 熟悉Comparable接口
 * @author Administrator
 *
 */
public class TestTreeSet {
	public static void main(String[] args) {
		Set<Integer> set = new TreeSet<>(); 
		set.add(30);
		set.add(100);
		set.add(90);
		for(Integer m:set) {
			System.out.println(m);
		}
		
		Set<Employee> set2 = new TreeSet<>();
		set2.add(new Employee(100,"张三",50000));
		set2.add(new Employee(200,"李四",20000));
		set2.add(new Employee(300,"王五",80000));
		set2.add(new Employee(400,"赵六",20000));
		for(Employee m:set2) {
			System.out.println(m);
		}
	}
}

 

相关标签: TreeSet