Collections tool class: A tool class about collections in Java, which contains various static polymorphic methods related to collection operations, and cannot be instantiated (privatize the constructor).
The difference from Collection: Collection is an interface, which provides general interface methods for basic operations on collection objects, and various specific implementation classes such as List and Set. Collections is a tool class that specializes in manipulating the elements in the Collection interface implementation class.
Commonly used methods: sort sort (List list) is visible
in ascending order of natural sorting , and the output is in ascending alphabetical order after sorting by default.
package Demo_1;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SortTest {
public static void main(String[] args) {
sortTest();
}
public static void sortTest() {
List<String> list = new ArrayList<>();
list.add("aaa");
list.add("yyy");
list.add("mmm");
list.add("ddd");
System.out.println("排序前:" + " " + list);
Collections.sort(list);
System.out.println("排序后:" + " " + list);
When the array contains uppercase and lowercase characters, the output result of sorting is uppercase first, then lowercase.
package Demo_1;
public class SortTest {
public static void main(String[] args) {
sortTest();
}
public static void sortTest() {
List<String> list = new ArrayList<>();
list.add("AAA");
list.add("yyy");
list.add("MMM");
list.add("ddd");
System.out.println("排序前:" + " " + list);
Collections.sort(list);
System.out.println("排序后:" + " " + list);
The default is ascending, equal to Collections.sort(list), Collections.sort(list, Comparator.naturalOrder())
descending: Collections.sort(list, Comparator.reverseOrder());
//默认升序
Collections.sort(list, Comparator.naturalOrder());
System.out.println("默认升序:" + " " + list);
//降序
Collections.sort(list, Comparator.reverseOrder());
System.out.println("降序:" + " " + list);
Random sort shuffle(List list): It can be seen that the output sort order is random.
//随机排序
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
list.add("5");
list.add("6");
list.add("7");
list.add("8");
list.add("9");
list.add("10");
list.add("M");
list.add("P");
list.add("A");
System.out.println(list);
Collections.shuffle(list);
System.out.println(list);
Get the maximum element max(Collection coll, Comparator comparator) where Comparator is an object passed in. The following uses an example of a daily order to explain its usage.
Create a Customer class and create its get/set methods
public class CollectionsTest {
public static void main(String [] args){
}
}
class Customer{
private String name;
private int order;
public Customer(String name,int order){
this.name = name;
this.order = order;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
}
Create a List object
public static void main(String [] args){
List<Customer> list = new ArrayList<>();
list.add(new Customer("陈小姐",25));
list.add(new Customer("王先生",30));
list.add(new Customer("黄小姐",33));
list.add(new Customer("伍小姐",18));
list.add(new Customer("李先生",42));
System.out.println(list);
}
Print this list, the result is just a bunch of memory addresses.
We can obtain detailed order information by adding a debugging method.
@Override
public String toString() {
return "Customer{" +
"name='" + name + '\'' +
", order=" + order +
'}';
}
Each customer's order information can be generated by calling the toString() method.
Get the Max value: Create new Comparator(). Comparator is actually an interface. According to the underlying code, o1 - o2 is in ascending order. When o1 - o2 > 0, it proves that o1 > o2. , so the result is 42 of the maximum
On the contrary, to obtain the minimum value, o2 - o1 is in descending order. When o2 - o1 < 0, it is proved that o2 < o1, and it will be ranked all the way down. Whoever is thinner will be ranked the bottom. The minimum value is 18
Another custom writing method, such as creating another CustomerComparator class to implement the Comparator interface.
package Demo_2;
import java.util.Comparator;
public class CustomerComparator implements Comparator<CollectionsTest.Customer> {
@Override
public int compare(CollectionsTest.Customer o1, CollectionsTest.Customer o2) {
return o1.getOrder()-o2.getOrder();
}
}
Also call Collections.max, passing in the newly created StudentComparator object.
Create an immutable collection unmodifiablleXXX() , where XXX is a collection of objects.
When adding a new product, an error will appear.
Similarly, you can also create Set, Map
After adding content, there is also an error.
Finally, introduce Comparable, which is an interface for customizing sorting rules. Sort the objects of each class that implements it as a whole. The compareTo method is a specific method to achieve sorting, such as TreeSet, SortedSet, and Collections.sort() method calls for sorting. Classes such as String and Integer implement this interface by default, so they can be sorted.
public interface Comparable<T> {
public int compareTo(T o);
}
The compareTo method is used to compare the order of the secondary object and the specified object, o is the object to be compared, and returns an int type.
If it is greater than 0, it means that this is greater than the object o passed in, and then it goes to the back row, that is, the ascending order
is equal to 0, which means that this is equal to the object passed in. o
is less than 0, which means this is smaller than the object o passed in
Create a TreeSet container object and add the same customer order information as above.
Similarly, create a Customer class and implement the Comparable interface
Override the compareTo() method, force the Customer, and return this.order - customer.order, which is expressed as ascending order, where this is the current element, for example, if it is the first one in the set, then this is the first one, And so on.
@Override
public int compareTo(Object o) {
Customer customer = (Customer)o;
return this.order - customer.order;
}
Another relatively standard overwriting method is judged by the instanceof condition. The function is to judge whether a class implements a certain interface, or to judge whether an instance object belongs to a class. But an error like "Missing return statement" appears.
The compareTo() method actually returns 0 by default, which means that when the returned number is 0, the two objects compared are the same.
@Override
public int compareTo(Object o) {
// Customer customer = (Customer) o;
// return this.order - customer.order;
if (o instanceof Customer) {
Customer customer = (Customer) o;
return this.order - customer.order;
}
return 0;
}
Similarly, if you want to achieve descending order, you only need to change it to customer.order - this.order.
@Override
public int compareTo(Object o) {
// Customer customer = (Customer) o;
// return this.order - customer.order;
if (o instanceof Customer) {
Customer customer = (Customer) o;
// return this.order - customer.order;
return customer.order - this.order;
}
return 0;
}
Combining the sorting of Python and Javascript introduced in this issue and the previous issue, you can find that although the writing method of Java seems to be relatively complicated, in fact, some of the principles are basically the same. Each has its own application advantages in different scenarios. The next issue will continue to explain the application of Collections in detail through another daily project.