Collections工具类练习--sort,max,reverse,reversrOrder,binarySearch,

#日常练习

对Student类进行排序,取最大元素,翻转,二分查找,Student类将存储在ArrayList中

package Collections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;

//对Student类进行排序,Student类将存储在ArrayList中
public class CollectionsDemo {
	public static void printCollection(ArrayList al) {
		for (Iterator it = al.iterator(); it.hasNext();) {
			Student s = it.next();
			System.out.println(s.getId() + "\\" + s.getName() + "\\" + s.getAddress());
		}
	}
	class nameComparator implements Comparator {
		@Override
		public int compare(Student s1, Student s2) {
			int num = s1.getName().compareTo(s2.getName());
			if (num == 0)
				return new Integer(s1.getId()).compareTo(new Integer(s2.getId()));
			return num;

		}
	}


	public static void main(String[] args) {
		ArrayList studentList = new ArrayList();
		studentList.add(new Student(16114372, "sutaotao", "anhui"));
		studentList.add(new Student(16114385, "liuxiang", "nanjing"));
		studentList.add(new Student(16114334, "shuhe", "shanghai"));
		studentList.add(new Student(15114362, "xielixiao", "anhui"));
		
		//使用默认规则排序
		Collections.sort(studentList);
		printCollection(studentList);
		
		/*
		//按照指定比较器比较排序
		Collections.sort(studentList,new nameComparator());
		printCollection(studentList);
		*/
		
		/*
		// 调用max方法返回的是最大元素,此处返回的是Student对象(引用)
		//传入集合就算是无序的,只要集合内元素有默认比较方法就会返回比较结果,但推荐排序后在调用max
		// 底层调用的应该是调用了Sort方法,
		// max还有 一个重载方法,可以传入一个比较器,实现特定规则排序后的最大元素
		System.out.println("max:" + Collections.max(studentList).getId());
		*/
		
		/*
		//翻转
		Collections.reverse(studentList);
		printCollection(studentList);
		 */
		
		/*
		//强行逆转集合排序
		//还有一重载方法Collections.reverseOrder(List,Comparator comp),
		//逆转指定比较器的比较结果
		Collections.sort(studentList,Collections.reverseOrder());
		printCollection(studentList);
		*/
		/*
		// 使用二分法查找指定元素,返回角标,如果集合内有多个目标,只会返回查找到的第一个
		int index = Collections.binarySearch(studentList, new Student(16114372, "sutaotao", "anhui"));
		System.out.println("二分法查找结果:"+index);
		*/
	}

}



Student类
package Collections;

class Student implements Comparable{
	private int id;
	private String name;
	private String address;
	Student(int id,String name,String address){
		this.id = id;
		this.name = name;
		this.address = address;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public int hahCode(Object obj) {
		return name.hashCode() + address.hashCode() + id;
	}
	public boolean equals(Object obj) {
		if(!(obj instanceof Student)) {
			throw new ClassCastException();
		}
		Student s = (Student)obj;
		if(this.id == s.id && this.name.equals(s.name) && this.address.equals(s.address)) {
			return true;
		}
		return false;
	}
	//按id排序
	public int compareTo(Student s) {
		int num =  new Integer(this.id).compareTo(new Integer(s.id));
		if(num == 0) {
			return this.name.compareTo(s.name);
		}
		return num;
	}

}



你可能感兴趣的:(练习)