자바 의 TreeSet 소개

3617 단어 자바TreeSet
import java.util.Comparator;
import java.util.TreeSet;

import org.junit.Test;

//     
public class TreeSetTest {
	/**
	 * TreeSet:    Set               。           :          0.        :   。
	 * 
	 *         :           。       Comparable  ,  compareTo    。
	 * 
	 *   ,            ,            ,      。
	 *   ,             ,            。          。       ?
	 * 
	 *         :        。              。
	 * 
	 *          Comparator  ,  compare  。
	 *   Comparator               TreeSet      。
	 */
	public static void main(String[] args) {
		TreeSet<Student> ts = new TreeSet<Student>();
		// Student     Comparable,add    ,Student cannot be cast to java.lang.Comparable
		//        ,TreeSet       ,      ;
		//      Err   ,TreeSet        compareTo(Object obj)              ——
		//          Comparable  ,    ClassCastException  
		// add               ,           ,      Comparable  
		ts.add(new Student("lisi0", 30));
		ts.add(new Student("lisixx", 29));
		ts.add(new Student("lisi9", 29));
		ts.add(new Student("lisi8", 38));
		//       ,     (           ,   ,       )
		ts.add(new Student("lisixx", 29));
		ts.add(new Student("lisixx", 28));
		ts.add(new Student("lisi4", 14));
		ts.add(new Student("lisi7", 27));
		System.out.println(ts);
	}
	
	//               
	@Test
	public void testComparator() {
		//        ,       
		TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {

			@Override
			public int compare(Student s1, Student s2) {
				int num = s1.getName().compareTo(s2.getName());
				return num;
			}
		});

		ts.add(new Student("lisi0", 30));
		ts.add(new Student("lisixx", 29));
		ts.add(new Student("lisi9", 29));
		ts.add(new Student("lisi8", 38));
		//          ,        ,         
		//    ,       
		ts.add(new Student("lisixx", 29));
		ts.add(new Student("lisixx", 28));
		ts.add(new Student("lisi4", 14));
		ts.add(new Student("lisi7", 27));

		System.out.println(ts);
	}
	//    ,    
	@Test
	public void mySort() {
		TreeSet set = new TreeSet(new Comparator() {
			int r = 0;
			@Override
			public int compare(Object a, Object b) {
				int n1 = Integer.parseInt(a.toString());
				int n2 = Integer.parseInt(b.toString());
				if (n1 % 2 != n2 % 2) {
					r = (n2 % 2 - n1 % 2);
				} else if (n1 % 2 == 1) {
					if (n1 > n2)
						r = 1;
					else if (n1 < n2)
						r = -1;
				} else if (n1 % 2 == 0) {
					if (n1 > n2)
						r = -1;
					else if (n1 < n2)
						r = 1;
				}
				return r;
			}
		});
		set.add("1");
		set.add("2");
		set.add("3");
		set.add("4");
		set.add("5");
		set.add("6");
		set.add("7");
		set.add("8");
		set.add("9");
		set.add("10");
		System.out.println(set);
	}

	// //                
	public static class Student implements Comparable<Student> {

		private int age;
		private String name;

		public Student(String name, int age) {
			this.age = age;
			this.name = name;
		}

		@Override
		public int compareTo(Student stu) {
			int num = new Integer(this.age).compareTo(new Integer(stu.age));
			return num == 0 ? this.name.compareTo(stu.name) : num;
		}

		public String toString() {
			return name + "::" + age;
		}

		public int getAge() {
			return age;
		}

		public void setAge(int age) {
			this.age = age;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}
	}
}

좋은 웹페이지 즐겨찾기