comparator and comparable in set sorting

On the sorting of sets in set framework

When it comes to the sorting of sets, it is necessary to use The sort() method has two interfaces for sorting Collections:

  • 1,java.lang.interface Comparable
  • 2,java.util.Interface Comparator
    1. Use of comparable
    For example, we define a User class: User
package test002;

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

/**
 * User class
 * @author Huang Xiaobao
 *
 */
public class User implements Comparable<User>{
	//User age
	private int age;
	//User name
	private String name;
	
	public User() {
		// TODO Auto-generated constructor stub
	}

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



	public int getAge() {
		return age;
	}

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

	public String getName() {
		return name;
	}

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

	//Override equals method
	@Override
	public String toString() {
		return "User [age=" + age + ", name=" + name + "]";
	}


	//Rewrite the methods in the interface to sort by age
	@Override
	public int compareTo(User u) {
		return this.age-u.getAge();
	}

	
	public static void main(String[] args) {
		List<User> list = new ArrayList<>();
		list.add(new User(25, "wolf"));
		list.add(new User(18, "Lisi"));
		list.add(new User(24, "Wang Wu"));
		list.add(new User(32, "Guan Yu"));
		list.add(new User(47, "Zhang Fei"));
		list.add(new User(27, "Liu Bei"));
		
		Collections.sort(list);
		for(User u:list) {
			System.out.println(u);
		}
	}
	
}


Output results:
User [age=18, name=Lisi]
User [age=24, name=Wang Wu]
User [age=25, name=wolf]
User [age=27, name=Liu Bei]
User [age=32, name=Guan Yu]
User [age=47, name=Zhang Fei]

In a collection, if the element types in the collection do not implement the comparable interface, you can customize the sorting rules in this way (only in this class), and then use: collections Sort (list). If the class of the elements in the list has implemented the interface by default, it will sort in the default way.

  • 2. Use of comparator
    The same problem, a User class: User
package test002;

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

/**
 * User class
 * @author Huang Xiaobao
 *
 */
public class User implements Comparable<User>{
	//User age
	private int age;
	//User name
	private String name;
	
	public User() {
		// TODO Auto-generated constructor stub
	}

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



	public int getAge() {
		return age;
	}

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

	public String getName() {
		return name;
	}

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

	//Override equals method
	@Override
	public String toString() {
		return "User [age=" + age + ", name=" + name + "]";
	}	
}

For a User type collection

List<User> list = new ArrayList<>();
		list.add(new User(25, "wolf"));
		list.add(new User(18, "Lisi"));
		list.add(new User(24, "Wang Wu"));
		list.add(new User(32, "Guan Yu"));
		list.add(new User(47, "Zhang Fei"));
		list.add(new User(27, "Liu Bei"));
		

We sort them. Because this User class does not implement the sorting interface, we can define a class to implement the comparator interface. The code is as follows:

package test002;

import java.util.Comparator;

public class MyComparator implements Comparator<User>{

	@Override
	public int compare(User u1, User u2) {
		
		return u1.getAge()-u2.getAge();
	}

}

Then we can sort the set list:

package test002;

import java.util.ArrayList;
import java.util.List;

public class Test {
	public static void main(String[] args) {
		List<User> list = new ArrayList<>();
		list.add(new User(25, "wolf"));
		list.add(new User(18, "Lisi"));
		list.add(new User(24, "Wang Wu"));
		list.add(new User(32, "Guan Yu"));
		list.add(new User(47, "Zhang Fei"));
		list.add(new User(27, "Liu Bei"));
		
		list.sort(new MyComparator());//Note: anonymous inner classes and ramda expressions can also be used here
		for(User u:list) {
			System.out.println(u);
		}
	}
}

Output results:
User [age=18, name=Lisi]
User [age=24, name=Wang Wu]
User [age=25, name=wolf]
User [age=27, name=Liu Bei]
User [age=32, name=Guan Yu]
User [age=47, name=Zhang Fei]

  • **Collections Sort (list); When the elements in the list do not implement the comparable interface, the class of the elements in the list collection implements the comparable interface to sort. (only limited to sorting in this class). If the elements in the list collection have implemented interfaces, they will be sorted in the default way. For example, an alphabetic string will be sorted in the order of letters in the dictionary.
  • **Collections Sort (list, comparator C); The elements in the list do not implement the comparator interface. Instead, implement the comparator interface by anonymous internal classes or ramda expressions or by creating external classes, and then sort the collection. (not limited by class)
  • **List Sort (comparator C); Directly create a sorting standard for the elements in the list, regardless of whether the class of the elements in the list collection implements the sorting interface. If it is implemented, we will define a new standard. If it is not implemented, we will create a standard. Sorting can be realized by anonymous internal classes or ramda expressions or by creating external classes.

Well, the above is purely a personal point of view, if there are errors, please point out!!!

Tags: Java Lambda

Posted by Mgccl on Mon, 30 May 2022 21:29:26 +0530