Introduction to two collection sorting methods in Java

  • 2020-04-01 03:30:13
  • OfStack

Direct code:


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

/**
 * 
 * <p>
 * ClassName CollectionsSort
 * </p>
 * <p>
 * Description  This paper mainly introduces two kinds of sorting algorithms of sets <br/>
 *  The first: java.util.Collections.sort(java.util.List) , requires that the ordered elements be implemented java.lang.Comparable interface  <br/>
 *  The second: java.util.Collections.sort(java.util.List, java.util.Comparator) , this method requires implementation java.util.Comparator interface  <br/>
 *  Third: the following example is used correctly int Type attribute sort, right String Attribute sorting can be done using the following methods <br/>
 * public int compareTo(Cat o){return this.getName().compareTo(o.getName(0);}<br/>
 *  Fourth: compareTo() Description of function  <br/>
 *  if   The results of ;<br/>
 * <0 a<b ;<br/>=
 * ==0 a==b;<br/>
 * >=0 a>b;
 * </p>
 * 
 * @author wangxu wangx89@126.com
 *     <p>
 *     Date 2014-9-16  In the afternoon 04:52:57
 *     </p>
 * @version V1.0
 * 
 */

public class CollectionsSort {
	public static void main(String[] args) {
		//Method1 (); Test the first method
		method2();//Test the second method
	}

	public static void method1() {
		List<Cat> list = new ArrayList<Cat>();
		Cat c = new Cat("a", 10);
		list.add(c);
		c = new Cat("b", 20);
		list.add(c);
		c = new Cat("c", 3);
		list.add(c);
		//Sort the output in ascending order
		Collections.sort(list);
		System.out.println(list);
		//Sort the output in descending order
		Collections.sort(list, Collections.reverseOrder());
		System.out.println(list);
	}

	public static void method2() {
		List<Cat> list = new ArrayList<Cat>();
		Cat c = new Cat("a", 10);
		list.add(c);
		c = new Cat("b", 20);
		list.add(c);
		c = new Cat("c", 3);
		list.add(c);
		Comparator<Cat> catComparator = new Cat();
		//Sort the output in ascending order
		Collections.sort(list, catComparator);
		System.out.println(list);
		//Sort the output in descending order
		catComparator = Collections.reverseOrder(catComparator);
		Collections.sort(list, catComparator);
		System.out.println(list);
	}
}

class Cat implements Comparable<Cat>, Comparator<Cat> {
	private int age;
	private String name;

	public Cat() {
	}

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

	public int getAge() {
		return this.age;
	}

	public String getName() {
		return this.name;
	}

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

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

	//If the Comparable interface is implemented, do not override the method
	@Override
	public int compareTo(Cat o) {
		// TODO Auto-generated method stub
		return this.age - o.age;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return " Name: " + getName() + ", Age: " + getAge();
	}

	//Implement the Comparator interface and override the method
	@Override
	public int compare(Cat o1, Cat o2) {
		// TODO Auto-generated method stub
		return o1.getAge() - o2.getAge();
	}

}


Related articles: