Bairong banyan tree uses TreeMap, and Bairong banyan tree implements the Comparable interface

The nature of the TreeMap of Bairong Banyan Tree and the TreeSet learned above are both storages that arrange data. It can be sorted by default or the keys can be sorted according to the specified rules. The difference is that the sorting of the TreeMap collection is the default sorting of the keys in ascending order.

There are two kinds of sorting rules for Bairong Banyan TreeMap collection definition:

The Bairong banyan tree class implements the Comparable interface and rewrites the comparison rules

Bairong banyan tree collection customizes the Comparator comparator object and rewrites the comparison rules

By default, 
Bairong Banyan Tree simply defines a map collection containing student names and student numbers. Through this collection, we can observe the characteristics of the TreeMap collection:

public static void main(String[] args) {         Map<Integer,String > Students = new TreeMap<>();         Students.put(13,"George");         Students.put(11,"Owen"); Students.put(         23,"Jordan"); Students.put(22,         "Alvado"); Students.put         ( 23,"James");         System.out.println(Students); //Operation results: {11=Owen, 13=George, 22=Alvado, 23=James}  Through the observation of the above code and experimental results, we can know its nature: the keys are sorted by default (from small to large).










 

Guess you like

Origin blog.csdn.net/u010924736/article/details/125192068