Java工具类--Set集合(三)

Personnel类:
public class Personnel implements Comparable<Object> {
	private String name;
	private int age;
	public Personnel() {
	}
	public Personnel(String name,int age){
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public int getAge() {
		return age;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setAge(int age) {
		
		this.age = age;
	}

	@Override
	public String toString() {
		return this.getName()+":"+this.getAge();
	}

	//覆写treeSet种对象的比较方法
	public int compareTo(Object o) {
		Personnel p = (Personnel)o;
		int byName =  name.compareTo(p.getName());
		return byName==0?this.age-p.getAge():byName;
	}
	
}
Comparator接口,compare比较方法:
/**
 * TreeSet构造函数比较器
 */
public class ComparatorByAge implements Comparator<Object> {
	public int compare(Object o1, Object o2) {
		Personnel p1 = (Personnel) o1;
		Personnel p2 = (Personnel) o2;
		int byAge = p1.getAge()-p2.getAge();//按照用户的年龄比较
		return byAge == 0?p1.getName().compareTo(p2.getName()):byAge;
	}
}
TreeSet类(实例):
/**
 * Set集合和Collection 接口中方法是一样
 *   Set:
 *     |--HashSet 哈希表(实际上是一个 HashMap 实例)支持。它不保证 set 的迭代顺序;
 *        |--LinkedHashSet 可预知迭代顺序(添加的顺序)的 Set 接口的哈希表和链接列表实现
 *     			      特别是它不保证该顺序恒久不变。此类允许使用 null 元素。
 *     |--TreeSet 使用元素的自然顺序对元素进行排序,或者根据创建 set 时提供的 Comparator 进行排序,具体取决于使用的构造方法。
 *        注意:TreeSet排序是二叉树结构
 */
public class TreeSetDemo {
	public static void main(String[] args) {
		TreeSet<String> ts = new TreeSet<String>();
		TreeSet<Personnel> tp = new TreeSet<Personnel>();
		//TreeSet构造函数比较器优先级大于实体Comparable接口、compareTo()方法
		TreeSet<Personnel> tps = new TreeSet<Personnel>(new ComparatorByAge());

		//字符串直接调用了compareTo()比较
		fonctionts(ts);
		System.out.println();
		//实体种覆盖Comparable接口、compareTo()方法
		fonctiontp(tp);
		System.out.println();
		//通过构造函数来创建比较器
		fonctiontps(tps);
	}

	private static void fonctionts(TreeSet<String> ts) {
		//添加数据
		ts.add("bca");
		ts.add("nba");
		ts.add("af");
		ts.add("maykc");
		ts.add("c");
		ts.add("XXX");
		System.out.println("打印对象数据源:"+ts);
	}

	//由于Personnel对象没有可比较性,所以要复写compareTo()方法
	private static void fonctiontp(TreeSet<Personnel> tp) {
		//添加对象
		Personnel p = new Personnel("xiaofang",18);
		Personnel pr = new Personnel("zhangsan",10);
		tp.add(new Personnel("lisi",20));
		//添加两次
		tp.add(new Personnel("lisi",21));
		tp.add(new Personnel("wangwu",30));
		tp.add(p);
		tp.add(pr);

		System.out.println("打印对象数据源:"+tp);
	}

	//由于Personnel对象没有可比较性,通过TreeSet构造函数创建比较器
	private static void fonctiontps(TreeSet<Personnel> tp) {
		//添加对象
		Personnel p = new Personnel("xiaofang",18);
		Personnel pr = new Personnel("zhangsan",10);
		tp.add(new Personnel("lisi",20));
		//添加两次
		tp.add(new Personnel("lisi",21));
		tp.add(new Personnel("wangwu",30));
		tp.add(p);
		tp.add(pr);

		System.out.println("打印TreeSet构造函数、比较器对象数据源:"+tp);
	}
}
main函数运行结果:
打印对象数据源:[XXX, af, bca, c, maykc, nba]

打印对象数据源:[lisi:20, lisi:21, wangwu:30, xiaofang:18, zhangsan:10]

打印TreeSet构造函数、比较器对象数据源:[zhangsan:10, xiaofang:18, lisi:20, lisi:21, wangwu:30]


参考Api:

类 TreeSet<E>

java.lang.Object
  |--java.util.AbstractCollection<E>
      |--java.util.AbstractSet<E>
          |--java.util.TreeSet<E>
构造方法摘要
TreeSet() 
          构造一个新的空 set,该 set 根据其元素的自然顺序进行排序。
TreeSet(Collection<? extends E> c) 
          构造一个包含指定 collection 元素的新 TreeSet,它按照其元素的自然顺序进行排序。
TreeSet(Comparator<? super E> comparator) 
          构造一个新的空 TreeSet,它根据指定比较器进行排序。
TreeSet(SortedSet<E> s) 
          构造一个与指定有序 set 具有相同映射关系和相同排序的新 TreeSet。
方法摘要
boolean add(E e) 
          将指定的元素添加到此 set(如果该元素尚未存在于 set 中)。
boolean addAll(Collection<? extends E> c) 
          将指定 collection 中的所有元素添加到此 set 中。
 E ceiling(E e) 
          返回此 set 中大于等于给定元素的最小元素;如果不存在这样的元素,则返回 null
 void clear() 
          移除此 set 中的所有元素。
Object clone() 
          返回 TreeSet 实例的浅表副本。
Comparator<? super E> comparator() 
          返回对此 set 中的元素进行排序的比较器;如果此 set 使用其元素的自然顺序,则返回 null
boolean contains(Object o) 
          如果此 set 包含指定的元素,则返回 true
 Iterator<E> descendingIterator() 
          返回在此 set 元素上按降序进行迭代的迭代器。
 NavigableSet<E> descendingSet() 
          返回此 set 中所包含元素的逆序视图。
 E first() 
          返回此 set 中当前第一个(最低)元素。
 E floor(E e) 
          返回此 set 中小于等于给定元素的最大元素;如果不存在这样的元素,则返回 null
 SortedSet<E> headSet(E toElement) 
          返回此 set 的部分视图,其元素严格小于 toElement
 NavigableSet<E> headSet(E toElement, boolean inclusive) 
          返回此 set 的部分视图,其元素小于(或等于,如果 inclusive 为 true)toElement
 E higher(E e) 
          返回此 set 中严格大于给定元素的最小元素;如果不存在这样的元素,则返回 null
boolean isEmpty() 
          如果此 set 不包含任何元素,则返回 true
 Iterator<E> iterator() 
          返回在此 set 中的元素上按升序进行迭代的迭代器。
 E last() 
          返回此 set 中当前最后一个(最高)元素。
 E lower(E e) 
          返回此 set 中严格小于给定元素的最大元素;如果不存在这样的元素,则返回 null
 E pollFirst() 
          获取并移除第一个(最低)元素;如果此 set 为空,则返回 null
 E pollLast() 
          获取并移除最后一个(最高)元素;如果此 set 为空,则返回 null
boolean remove(Object o) 
          将指定的元素从 set 中移除(如果该元素存在于此 set 中)。
int size() 
          返回 set 中的元素数(set 的容量)。
NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) 
          返回此 set 的部分视图,其元素范围从 fromElement 到 toElement
SortedSet<E> subSet(E fromElement, E toElement) 
          返回此 set 的部分视图,其元素从 fromElement(包括)到 toElement(不包括)。
 SortedSet<E> tailSet(E fromElement) 
          返回此 set 的部分视图,其元素大于等于 fromElement
 NavigableSet<E> tailSet(E fromElement, boolean inclusive) 
          返回此 set 的部分视图,其元素大于(或等于,如果 inclusive 为 true)fromElement

java.util 
     |--接口 Comparator<T>

方法摘要
int compare(T o1, T o2) 
          比较用来排序的两个参数。
boolean equals(Object obj) 
          指示某个其他对象是否“等于”此 Comparator。


猜你喜欢

转载自blog.csdn.net/u013251413/article/details/80689028