关于10K面试题的组合排序-Comparator

package com.kevin.demo;

import java.util.Comparator;
import java.util.TreeSet;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * @author  <a href="mailto:[email protected]">kevin.long</a>
 * @description
 */
public class TestUser {
	
	/**
	 * 班级
	 */
	private Integer classes; 
	
	/**
	 * 类型(teacher or student)
	 */
	private String type;
	
	/**
	 * 姓名
	 */
	private String name;
	
	public TestUser(Integer classes, String type, String name){
		this.classes = classes;
		this.type = type;
		this.name = name;
	}
	
	@Override
	public boolean equals(Object other) {
		if(!(other instanceof TestUser)){
			return false;
		}
		TestUser testUser = (TestUser)other;
		return new EqualsBuilder().append(classes, testUser.getClasses())
										.append(type, testUser.getType())
											.append(name, testUser.getName()).isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(classes)
										.append(type)
											.append(name).toHashCode();
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getName() {
		return name;
	}

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

	public Integer getClasses() {
		return classes;
	}

	public void setClasses(Integer classes) {
		this.classes = classes;
	}
	
	/**
	 * 现有List集合中存放有10W个无序的User(属性:classes 班级;type 身份【学生 or 老师】;name 姓名)对象。
	 * 要求:用JAVA实现将List集合中的User对象按照1-n班并且每个班的老师必须放在该班级学生的前面输出。
	 * (一个班只有一个老师,一个班存在多个老师,这两只情况可以分开用两个算法实现,也可以用一个算法实现,但要考虑性能)例如下面格式:
	 * 1班 老师 张三
	 * 1班 学生 李四
	 * 1班 学生 王五
	 * 2班 老师 张三2
	 * 2班 学生 李四2
	 * 2班 学生 王五2
	 */
	public static void main(String[] args) {
		//按班级排序,然后再按老师和学生循序排序
		TreeSet<TestUser> users = new TreeSet<TestUser>(new Comparator<Object>() {
			public int compare(Object arg0, Object arg1) {
				int calssKey1 = ((TestUser) arg0).getClasses();
				int calssKey2 = ((TestUser) arg1).getClasses();
                if(calssKey1 > calssKey2){ 
                	return 1;
                }else if(calssKey1 < calssKey2){
                	return -1;
                }else{ //在相等的情况下,按照老师排序
                	String typeKey1 = ((TestUser) arg0).getType();
                	String typeKey2 = ((TestUser) arg1).getType();
                	if(TestUserType.STUDENT.equals(typeKey1) && TestUserType.TEACHER.equals(typeKey2)){
                		return 1;
                	}else if(TestUserType.TEACHER.equals(typeKey1) && TestUserType.STUDENT.equals(typeKey2)){
                		return -1;
                	}else{ //在相等的情况下,按照名字排序
                		String nameKey1 = ((TestUser) arg0).getName();
                    	String nameKey2 = ((TestUser) arg1).getName();
                		if(nameKey1.hashCode() > nameKey2.hashCode()){
                			return 1;
                		}else if(nameKey1.hashCode() < nameKey2.hashCode()){
                			return -1;
                		}else{
                			if(arg0.hashCode() > arg1.hashCode()){
                    			return -1;
                    		}else if(arg0.hashCode() < arg1.hashCode()){
                    			return 1;
                    		}else{
                    			return 0;
                    		}
                		}
                	}
                }
			}
		});
		users.add(new TestUser(1, TestUserType.TEACHER, "张三"));
		users.add(new TestUser(2, TestUserType.TEACHER, "张三2"));
		users.add(new TestUser(1, TestUserType.STUDENT, "李四"));
		users.add(new TestUser(2, TestUserType.STUDENT, "李四2"));
		users.add(new TestUser(1, TestUserType.STUDENT, "王五"));
		users.add(new TestUser(2, TestUserType.STUDENT, "王五2"));
		for(TestUser user : users){
			System.out.println(user.getClasses() +"\t"+ user.getType() +"\t"+ user.getName());
		}
	}
}


关于10K面试题的组合排序-Comparator

你可能感兴趣的:(comparator)