About defining the interface Comparable (comparator) to compare the sizes of two objects

1. Topic: Comparing the sizes of objects of different reference types: Requirement description: Although the student category and the news category are completely different categories, they both have the ability to compare. For example, you can compare the size of two students, but you need to specify whether the comparison is based on student number, […]

Comparatorcomparable

The use of natural sorting Comparable Case requirements Store student objects and traverse, create a TreeSet collection and use the no-parameter construction method Requirements: Sort by age from youngest to oldest. If the ages are the same, sort by name in alphabetical order. Implementation steps Create a TreeSet collection using empty parameter construction Use the […]

The use and difference between Comparable interface and Comparator interface

One: Custom sorting of collections: TreeSet: The underlying data structure is a red-black tree with customizable sorting rules Construction method: public TreeSet(); public TreeSet(Comparator c); The sorting methods using TreeSet collection are: Method 1: When calling the parameterless constructor 1. Let the generic Stu class of Treeset implement the interface Comparable, And the generic type […]

Comparator and Comparable comparison

Comparable is a sorting interface: If a class implements the Comparable interface, it means “this class supports sorting”. Comparator is a comparator: If we need to control the order of a certain class, we can create a “comparator of this class” for sorting. Comparable is equivalent to “internal comparator”, and Comparator is equivalent to “external […]

Partition and full sorting (WritableComparable)

1. Concept 1. Partition: Hadoop’s default partition is obtained modulo the number of ReduceTasks based on the hashCode of the key. Users cannot control which key is stored in which partition. If you want to control which key is stored in which partition, you need to customize the class to inherit Partitioner<KEY, VALUE>.<br>Generic KEY and […]

JavaSE—Comparable interface and Comparator

[JavaSE]—Comparable interface and comparator comparator 1. Comparable interface 1. Definition of Comparable Interface It can be seen that this interface is defined through generics, and its function is to specify the sorting rules of a certain object. Return value: 1: means greater than -1: indicates less than 0: Indicates equal 2. Actual Case Reference to […]

Comparable and Comparator

<strong>To implement comparison of custom classes in java, the following two interfaces are provided:<br></strong> Comparable (internal sorting) int compareTo(Object obj); the return value is int, the default is ascending order Comparator (external sorting) int compare(Object ob1,Object obj2); the return value is int, Flexible sorting <br><br> <strong>java.lang.Comparable interface (internal comparator)java.lang</strong><br><strong>If a class implements the Comparable (sorting […]

Use Arrays.sort, Comparable, Comparator to implement object array sorting

Object array sorting 1. How to output the contents of an object array? 2. How to sort objects in an array 1. How to output the contents of the object array? You need to override the toString method in the object’s class //Rewrite method @Override public String toString() {<!– –> ret![Please add image description](https://img-blog.csdnimg.cn/503f7f999cd845a7bae729fff4ba1699.png) rn […]

Comparable interface and Cloneable interface

Article directory 1. Comparable interface 2. Clonable interface and deep copy 1, Comparable interface The following is a program that implements sorting an array of objects: class Student {<!– –> private String name; private int score; public Student(String name, int score) {<!– –> this.name = name; this.score = score; } @Override public String toString() {<!– […]