A lot of people just use the sort method without the comparator comparator in the collections to do some simple sorting of the dynamic array ArrayList that stores the shaping integer, including me before, just before the "Java" The collections class--java in Java, the data s
In Java, if you want to sort a collection object or array object, you need to implement the comparator interface to achieve the goal that we want.Next we simulate the sorting of the Date property in the collection objectFirst, the entity class stepPackage com.ljq.entity;/***/public class step{ /** Time * * Private String Accepttime = ""; /** Location * * Private String acceptaddress = ""; Pub
Java provides a comparable interface that contains only one CompareTo () method. This method can be used to sort two objects in a single order. Specifically, it returns a negative number, 0, a positive number to indicate that an existing object is less than, equal to, greater than the input object.Java provides a comparator interface that contains two methods of compare () and equals () .The Compare () meth
The Java comparator has two classes, namely the comparable interface and the comparator interface.
The comparer has a very large effect when sorting an array of objects
1, using the comparable interface, you need to rewrite the compareTo(T o) method, where the collation is defined, you can directly call Java.util.Arrays.sort () to sort the array of objects,
Examp
Java: // usage of Comparator and Comparable (output the key-value pairs of the map set in sequence as required ),
Import java. util .*;
Public class Person implements Comparable
{
Private String name;
Private int age;
Public Person (String name, int age) // initialize the constructor
{
This. name = name;
This. age = age;
}
Public void set (String name, int age)
In general, it is not a problem to write a simple sort program yourself, but can you guarantee the time complexity of the sequencing program you write? Can you guarantee the correctness of the program, robustness, and the clarity of the program structure, maintainability .... In summary, it is necessary to learn the sort interface to sort complex objects. Java has two to implement the sorting interface comparator
Reuse of java Comparator
Imagine A scenario where A has N fields and A comparison function dedicated to. Each comparison function is reused in N multiple business lines.
Then, the problem arises. One day A adds an in field, and the priority of this field in the original scenario is higher than that of all other fields. It has no effect in other scenarios.
How can this problem be solved?
1. rewrite all exi
Usage of comparator in JavaSpeaker: Wang Shaohua QQ Group No.: 483773664In Java, if you want to sort a collection object or array object, you need to implement the comparator interface to achieve the goal that we want.Next we simulate the sorting of the Date property in the collection objectFirst, the entity class
12345678910111213141516171819
In Java, if you want to sort a collection object or an array object, you need to implement the comparator interface to achieve the goal we want.
Next we simulate the sort of date attributes in the collection object
One, entity class step
Package com.ljq.entity;
/** *
@author Administrator */Public
class step{
/** processing time/
private String accepttime = "";
/** Express Location * *
, or is called the default order.
The second sorting method of TreeSet.When the element itself does not have the comparison or the comparison, it is not necessary.This requires the set to have a comparison. When the set is initialized, a comparison method is available.Define a class to implement the compareable interface and overwrite the compare method.When both methods exist, the comparator is used as the main component.
The Code is as fol
to modify the public class,2, when we need to meet different ways to sort, this method is not feasibleThe comparator interface is then provided in Java, and comparator uses the integer returned by its compare () method to compare 2 objects.This eliminates the need to modify public classesSystem.out.println ("---------------");listList.add (The New Employee ("AAA
Java comparators are useful to implement the compare () callback method of the comparator interface to make a collation, and then call Collections.sort (list, new Comparatortest ()), and you can sort the list. Very convenient
Pay attention to the order of return in the Compare () method in use, and prioritize the collation to be written in the front
Entity classes:
Copy Code code as foll
Author: gnuhpcSource: http://www.cnblogs.com/gnuhpc/
// Create a comparator that returns the outcome// Of a reverse string comparison.Class revstrcomp implements comparator // Implement the compare () method so that it// Reverses the order of the string comparison.Public int compare (string stra, string strb) {// compare strb to stra, rather than stra to strb.Return strb. compareto (stra ); }}
// Sort an ar
element itself does not have the comparison or the comparison, it is not necessary.This requires the set to have a comparison. When the set is initialized, a comparison method is available.Define a class to implement the compareable interface and overwrite the compare method.When both methods exist, the comparator is used as the main component. The code is as follows:Copy code Package com. day14.wd;Import jav
In the programmer's interview, everyone should have seen a variety of sorting methods:BubbleChooseInsertFastHeap SortWait a minute..These are simple data types, such as the numeric type int, when encountered in the actual situation of the complex sort, such as to a company's employees, according to the name, age, gender and other factors of the comprehensive sorting situation, with the above sorting method, will not be realized. But don't be afraid. There are big artifacts in ~
(i) List and array①list default Sort code:public static void Main (string[] args) { list②: Array:String[] names = {"A li", "Put B", "A Li", "D to", "Amount E", "D to", "a b li"};//should be based on the ASCII code so Arrays.sort (names); System.out.println (arrays.tostring (names)); Arrays.sort (Names,collator.getinstance (Locale.china));//According to the Chinese alphabet, first pinyin is compared with the first letter, then the second letter pinyin. System.out.println (arrays.tostring (name
Java Object instanceof keyword exercise: judge whether the set of the same person is sorted by age. If the same age is sorted by name in alphabetical order, Comparator,
Package com. swift; public class Same_Person_Test {public static void main (String [] args) {/** Object determines whether the Object is the same Person */Person per1 = new Person ("zhangsan", 30 ); person per2 = new Person ("lisi", 27); Per
Use of Comparator in Java to sort sets.
Objective: To sort the age of Person objects in ascending order
1. Sorting
1 package com. app; 2 3 import java. util. arrayList; 4 import java. util. collections; 5 import java. util. comparator
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.