Collections类的常用方法以及compareto方法

获取List集合中最大值最小值:

public static void main(String[] args) {
     
		List<String> list = new ArrayList<String>();
		list.add("zoo");
		list.add("anlmal");
		list.add("fruit");
		list.add("banana");
		list.add("teacher");
		list.add("love");
		
		//打印集合中最大最小值
		System.out.println("最大值:"+Collections.max(list));
		System.out.println("最大值:"+Collections.min(list));
		
	
	}

在这里插入图片描述
对List进行升序排序:

public static void main(String[] args) {
     
		List<String> list = new ArrayList<String>();
		list.add("zoo");
		list.add("anlmal");
		list.add("fruit");
		list.add("banana");
		list.add("teacher");
		list.add("love");
		
		//打印集合中最大最小值
		System.out.println("最大值:"+Collections.max(list));
		System.out.println("最大值:"+Collections.min(list));
		
		//升序排序
		Collections.sort(list);
		Iterator<String> it = list.iterator();
		while(it.hasNext()) {
     
			System.out.println(it.next());
		}
		
	}

Collections类的常用方法以及compareto方法_第1张图片
反转集合元素:

public static void main(String[] args) {
     
		List<String> list = new ArrayList<String>();
		list.add("zoo");
		list.add("anlmal");
		list.add("fruit");
		list.add("banana");
		list.add("teacher");
		list.add("love");
		
		//打印集合中最大最小值
		System.out.println("最大值:"+Collections.max(list));
		System.out.println("最大值:"+Collections.min(list));
		
		//升序排序
		Collections.sort(list);
		Iterator<String> it = list.iterator();
		while(it.hasNext()) {
     
			System.out.println(it.next());
		}
		//反转集合元素
		Collections.reverse(list);
		for (String s : list) {
     
			System.out.println(s);
		}
	
	}

Collections类的常用方法以及compareto方法_第2张图片
查找:(注:不能在反转后查找,不然找不到)

public static void main(String[] args) {
     
		List<String> list = new ArrayList<String>();
		list.add("zoo");
		list.add("anlmal");
		list.add("fruit");
		list.add("banana");
		list.add("teacher");
		list.add("love");
		
		//打印集合中最大最小值
		System.out.println("最大值:"+Collections.max(list));
		System.out.println("最大值:"+Collections.min(list));
		
		//升序排序
		Collections.sort(list);
		Iterator<String> it = list.iterator();
		while(it.hasNext()) {
     
			System.out.println(it.next());
		}
	
		//查找
		System.out.println(Collections.binarySearch(list,"zoo")); //返回下标值,找不到则返回负数
		
		
		
	}

在这里插入图片描述

若List中放入的是对象,则用sort进行排序之前需要重写compareTo方法,否则sort方法报错,因为对象有多个属性,而sort方法并不知道用哪个属性来排序,且该对象必须实现Comparable接口

/*封装类*/
public class Student implements Comparable<Student> {
     
	//学号
	private int number;
	//姓名
	private String name;
	//性别
	private String gender;
	public int getNumber() {
     
		return number;
	}
	public String getName() {
     
		return name;
	}
	public void setName(String name) {
     
		this.name = name;
	}
	public String getGender() {
     
		return gender;
	}
	public void setGender(String gender) {
     
		this.gender = gender;
	}
	public void setNumber(int number) {
     
		this.number = number;
	}
	public Student() {
     
	}
	public Student(int number) {
     
		super();
		this.number = number;
	}
	public Student(int number, String name, String gender) {
     
		super();
		this.number = number;
		this.name = name;
		this.gender = gender;
	}
	@Override
	public int compareTo(Student o) {
     
		if(this.number>o.getNumber()) return 1;
		if(this.number<o.getNumber()) return -1;
		
		return 0;
	}
}
/*测试类*/
public static void main(String[] args) {
     
		List<Student> list2 = new ArrayList<Student>();
		Student s1 = new Student(2, "小明", "男");
		Student s2 = new Student(1, "小红", "女");
		Student s3 = new Student(4, "小紫", "男");
		Student s4 = new Student(3, "小绿", "女");
		list2.add(s1);
		list2.add(s2);
		list2.add(s3);
		list2.add(s4);
		
		//升序排序
		Collections.sort(list2);
		for (Student s : list2) {
     
			System.out.println("学号:"+s.getNumber()+"\t姓名:"+s.getName()+"\t性别:"+s.getGender());
		}
	}

Collections类的常用方法以及compareto方法_第3张图片

你可能感兴趣的:(javac)