comparator and comparable in java interview questions
comparator and comparable in java interview questions
Learn about comparator and comparable in java interview questions, we have the largest and most updated comparator and comparable in java interview questions information on alibabacloud.com
according to the age of the user. public class User {private string id;private int age;public user (String ID, int.) {this.id = Id;this.age = age;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public String GetId () {return ID;} public void SetId (String id) {this.id = id;}} importjava.util.arrays;import java.util.comparator;/*** @author pengcqu**/publicclass Usercomparatorimplementscomparator{publicintcompare (Objectarg0, OBJECTNBSP;ARG1) {return (user) arg0.
the order of a class, and the class itself does not support ordering (that is, it does not implement the comparable interface), then we can create a "comparer for this class" to sort by. This "comparator" only needs to implement the comparator interface.That is, we can create a new comparer by implementing the comparator
and assign a value to an object to write the collation for the custom class in the Compare method2. Passing a Comparator object as a parameter to a method of the Sort class3. Add the custom class used in the Compare method to the Sort classExamples of interface operations:Import Java.util.arraylist;import Java.util.collections;import Java.util.comparator;public class Car {public String name;public int price;public Car (String name, int price) {this.n
Comparison between Java Comparator and Comparable
1. How to sort objects by comparison?
Java basic types such as int, float, and double can be directly compared and sorted using java. util. Arrays. sort (Object []). How can we compare and sort objects? The classes we define
1. 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 person{String name, int age}.When we have such a personlist, it contains Person1, Person2, Persion3 ...., we use Collections.sort (personlist),is not getting the expected results. At this point so
"comparer for that class" to sort by.Comparable is equivalent to an "internal comparator", while comparator is equivalent to an "external comparator".Each of the two methods have advantages and disadvantages, with comparable simple, as long as the implementation of the comparable
Comparable introduction
Comparable is the sort interface.
If a class implements the comparable interface, it means "this class supports sorting." The class that implements the comparable interface supports sorting, assuming that there is now a list (or array) of objects for the class that implements the
The Java comparator has two classes, namely the comparable interface and the comparator interface .When sorting an array of objects, the comparator works very clearly, first of all, to explain the comparable interface.
Let the obj
interface is to define a custom class's comparison rules, because the CompareTo method has only one parameter and the other parameter is this, which is the class itself, so we can think of it as defining a comparison rule within the class. The equivalent of a Java developer writing a string class should allow it to implement the comparable interface. However, the Compare method has two parameters, which ca
In our usual use of collection, it is very often necessary to sort the elements inside. For this sort, there are usually two ways to implement this:1. Create a comparator class to implement the comparator interface, and then apply the sort method provided by the collection internally. For example, the edges in the graph are sorted by their weight size (the second method is also described in the following ex
());int v2 = Math.abs(((Integer) o2).intValue());return v1 > v2 ? 1 : (v1 == v2 ? 0 : -1);}}
Use AbsComparator to sort the set:
Import java. util. *; public class Test {public static void main (String [] args) {// generate an array of 20 Random integers (with positive and negative values) Random rnd = new Random (); integer [] integers = new Integer [20]; for (int I = 0; I
Use the Comparable interface t
Same
Both comparable and comparator are used to implement object comparisons, sorting
To compare and sort objects, you need to implement comparable or comparator interfaces
Comparable and comparator are all
Comparableand is the Comparator interfaces provided by Java Core API. From their names, we can tell they is used for comparing stuff in some. But what exactly was they and what is the difference between them? The following is examples for answering this question. The simple examples compare the size of the HDTV ' s. How to use comparable vs.
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 d
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
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.