Java comparator and comparable when a collection or array that needs to be sorted is not a purely numeric type, you can typically use comparator or comparable to implement object sorting or custom sorting in a simple way.1.Comparable
The details and differences of Java comparable and Comparator
Java provides us with two comparison mechanisms: comparable and Comparator, what is the difference between them? Come to understand today.
Comparable natural sort
Comparable under the
The sort () method exists in the arrays class before, and this method can sort the object array directly.
1.Comparable Interface
You can sort the array directly using the Java.util.Arrays class, but the class that contains the object must implement
/*** Demonstrate the necessity of data type packaging and generic classes*/Package seek;
/*** Design a sorted object array class to implement the search algorithm:* This example demonstrates the use of the comparable interface and the classes that
Java Collection framework: Java Collection framework
Structure of the Collection framework:
I. Collection Structure Framework:
Ii. Set, List, Queue, and Map
Set: the derivative interface of collection. Repeated elements -- equal () are not
TreeSet supports two sorting methods: Natural sorting and custom sorting. TreeSet uses natural sorting by default.
1. Natural sorting
TreeSet calls the compareTo (Object obj) method of the Set element to compare the size relationship between
Comparable & Comparator are used to implement the comparison and sorting of elements in the collection, except that comparable is the sort of method implementation defined within the collection, Comparator is the sort implemented outside the
A class that implements the Camparable interface indicates that objects of this class can be compared to each other, and that a collection of such objects can be sorted directly using the Sort method.Comparator can be regarded as an algorithm
/*1. When adding elements to TreeSet, if the elements themselves have the nature of the order, they are sorted by the nature of the element's natural order.2. When adding elements to TreeSet, if the element itself does not have a natural order, then
1. Array declarationsint New int []{1,2,3}; int [] Score2 = {n/a}; int New int [3];2. CollectionFirst, a logical diagram.In Java, the collection container is divided into two categories: one is a single-valued collection, and the other is a map that
First, describeCustom classes are sorted in a certain way, such as when a person class is ordered from small to large according to age, such as a student class to be ranked from highest to lowest by grade.Here we take two ways, one is to use the
Comparable & Comparator are used to implement the comparison and sorting of elements in the collection, except that comparable is the sort of method implementation defined within the collection, Comparator is the sort implemented outside the
TreeSet supports two sorts of sorting methods: natural sorting and custom sorting. TreeSet default to natural sorting.
1, Natural sorting
TreeSet invokes the CompareTo (Object obj) method of the collection element to compare the size relationships
"Javase" Day05_list Collection _list Sort _ queue and stack1.List Collection1) ordered set, repeatable set. The list is characterized by the ability to manipulate elements as an array, depending on the subscript. So list offers some unique methods.2)
Comparable & Comparator are used to implement the comparison and ordering of elements in the collection, except that comparable is the sort of method implementation defined within the collection,Comparator is the sort implemented outside the
Comparable & Comparator are used to implement the comparison and sorting of elements in the collection, except that comparable is the sort of method implementation defined within the collection, Comparator is the sort implemented outside the
Comparable & Comparator are used to implement the comparison and sorting of elements in the collection, except that comparable is the sort of method implementation defined within the collection, Comparator is the sort implemented outside the
See the original: http://www.cnblogs.com/sunflower627/p/3158042.html1. Comparator and comparable the same placeThey are all Java interfaces, and are used to compare the size of a custom class,What is a custom class: such as public class
Array Introduction
Arrays and other containers have three advantages:
Efficiency, Type Recognition
And
Can hold primitives
.
Arrays are among the many methods in Java that can store and access the reference sequence at random, the most efficient
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.