zoukankan      html  css  js  c++  java
  • JavaSE-TreeSet练习

    package com.btp.t4;
    
    import java.util.Comparator;
    import java.util.TreeSet;
    
    import org.junit.Test;
    
    public class TestEmployee {
    	//自然排序:使Employee实现comparable接口,并按name排序
        @Test
        public void test1(){
        	Employee e1=new Employee("Kobe",37,new MyDate(4,12,1983));
        	Employee e2=new Employee("Lebulang",30,new MyDate(8,2,19782));
        	Employee e3=new Employee("Wade",17,new MyDate(3,1,1979));
        	Employee e4=new Employee("James",27,new MyDate(2,1,1975));
        	Employee e5=new Employee("Joradan",77,new MyDate(7,7,1933));
        	Employee e6=new Employee("Joradan",76,new MyDate(6,7,1933));
        	
        	TreeSet set=new TreeSet();
        	set.add(e1);//不实现cpmparable接口一个也不进去
        	set.add(e2);
        	set.add(e3);
        	set.add(e4);
        	set.add(e5);
        	set.add(e6);//进不去
        	
        	for(Object o:set){
        		System.out.println(o);
        	}
        }
        
        //定制排序:创建TreeSet时传入Comparator对象,按生日的先后排序
        @Test
        public void test2(){
        	Comparator com=new Comparator(){
    
    			@Override
    			public int compare(Object o1, Object o2) {
    				if(o1 instanceof Employee && o2 instanceof Employee){
    					Employee e1=(Employee)o1;
    					Employee e2=(Employee)o2;
    					MyDate birth1=e1.getBirthday();
    					MyDate birth2=e2.getBirthday();
    					if(birth1.getYear()!=birth2.getYear()){
    						return birth1.getYear()-birth2.getYear();
    					}else{
    						if(birth1.getMonth()!=birth2.getMonth()){
    							return birth1.getMonth()-birth2.getMonth();
    						}else{
    							return birth1.getDay()-birth2.getDay();
    						}
    					}
    				}
    				return 0;
    			}
        	};
        	
        	TreeSet set=new TreeSet(com);
        	Employee e1=new Employee("Kobe",37,new MyDate(4,12,1983));
        	Employee e2=new Employee("Lebulang",30,new MyDate(8,2,1983));
        	Employee e3=new Employee("Wade",17,new MyDate(3,2,1983));
        	Employee e4=new Employee("James",27,new MyDate(2,1,1975));
        	Employee e5=new Employee("Joradan",77,new MyDate(7,7,1933));
        	Employee e6=new Employee("Joradan",76,new MyDate(7,7,1933));//生日相同进不去
        	set.add(e1);
        	set.add(e2);
        	set.add(e3);
        	set.add(e4);
        	set.add(e5);
        	set.add(e6);//进不去
        	for(Object o:set){
        		System.out.println(o);
        	}
        }
        
    }
    
    
    
    
    
    
    
    
    
    
    class Employee implements Comparable{
    	private String name;
    	private Integer age;
    	private MyDate birthday;
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public Integer getAge() {
    		return age;
    	}
    	public void setAge(Integer age) {
    		this.age = age;
    	}
    	public MyDate getBirthday() {
    		return birthday;
    	}
    	public void setBirthday(MyDate birthday) {
    		this.birthday = birthday;
    	}
    	public Employee(String name, Integer age, MyDate birthday) {
    		super();
    		this.name = name;
    		this.age = age;
    		this.birthday = birthday;
    	}
    	public Employee() {
    		super();
    		// TODO 自动生成的构造函数存根
    	}
    	@Override
    	public String toString() {
    		return "Employee [name=" + name + ", age=" + age + ", birthday=" + birthday + "]";
    	}
    	//重写的comparaTo方法
    	@Override
    	public int compareTo(Object o) {
    		if(o instanceof Employee){
    			Employee p=(Employee)o;
    			return this.name.compareTo(p.name);
    		}
    		return 0;
    	}
    	@Override
    	public int hashCode() {
    		final int prime = 31;
    		int result = 1;
    		result = prime * result + ((age == null) ? 0 : age.hashCode());
    		result = prime * result + ((birthday == null) ? 0 : birthday.hashCode());
    		result = prime * result + ((name == null) ? 0 : name.hashCode());
    		return result;
    	}
    	@Override
    	public boolean equals(Object obj) {
    		if (this == obj)
    			return true;
    		if (obj == null)
    			return false;
    		if (getClass() != obj.getClass())
    			return false;
    		Employee other = (Employee) obj;
    		if (age == null) {
    			if (other.age != null)
    				return false;
    		} else if (!age.equals(other.age))
    			return false;
    		if (birthday == null) {
    			if (other.birthday != null)
    				return false;
    		} else if (!birthday.equals(other.birthday))
    			return false;
    		if (name == null) {
    			if (other.name != null)
    				return false;
    		} else if (!name.equals(other.name))
    			return false;
    		return true;
    	}
    	
    }
    
    
    
    
    
    class MyDate{
    	private int year;
    	private int month;
    	private int day;
    	public int getYear() {
    		return year;
    	}
    	public void setYear(int year) {
    		this.year = year;
    	}
    	public int getMonth() {
    		return month;
    	}
    	public void setMonth(int month) {
    		this.month = month;
    	}
    	public int getDay() {
    		return day;
    	}
    	public void setDay(int day) {
    		this.day = day;
    	}
    	public MyDate(int day,int month,int year) {
    		super();
    		this.year = year;
    		this.month = month;
    		this.day = day;
    	}
    	public MyDate() {
    		super();
    		// TODO 自动生成的构造函数存根
    	}
    	@Override
    	public String toString() {
    		return "MyDate [year=" + year + ", month=" + month + ", day=" + day + "]";
    	}
    	@Override
    	public int hashCode() {
    		final int prime = 31;
    		int result = 1;
    		result = prime * result + day;
    		result = prime * result + month;
    		result = prime * result + year;
    		return result;
    	}
    	@Override
    	public boolean equals(Object obj) {
    		if (this == obj)
    			return true;
    		if (obj == null)
    			return false;
    		if (getClass() != obj.getClass())
    			return false;
    		MyDate other = (MyDate) obj;
    		if (day != other.day)
    			return false;
    		if (month != other.month)
    			return false;
    		if (year != other.year)
    			return false;
    		return true;
    	}
    	
    	
    }
    

      

  • 相关阅读:
    平时用到的Linux命令
    angular.element方法汇总
    CSS display:inline-block
    CSS:position:fixed使用(转)
    常见浏览器兼容性问题与解决方案【转】
    部分浏览器兼容问题
    php对应js math.random
    php stdclass转数组
    写在2016年末
    VirtualBox中Ubuntu 14.04屏幕分辨率不能设置的问题
  • 原文地址:https://www.cnblogs.com/a842297171/p/5162846.html
Copyright © 2011-2022 走看看