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
Java-collection framework Collections. sort (), Comparable interface, and Comparator interface, javasortcomparator
Collentions tool class -- java. util. Collections
Collentions is a tool class used to operate collection objects in the Java Collection framework. It is also
In Java, when sorting data, comparable and comparator are not needed to be met. Ordinary string, integer and other types have implemented the comparable interface, and sometimes we need to sort some other classes that do not have an intrinsic sort relationship, then we need to implement the above two interfaces, but it
Java comparator has two types: comparable interface and comparator interface.
When sorting an array of objects, the comparator plays an obvious role. First, we will explain the comparable interface.
Implement the
1. How do objects compare to sort?The basic types of Java, such as int, float, and double can all be sorted directly using Java.util.Arrays.sort (object[]) , how is the object compared to the sort? The classes we define are all methods that inherit the equals of the object class, and the default is to compare two objects for equality (hashcode the same)public boolean equals (Object obj) { return (this = = obj); } and our reality requirements ar
Java uses the Comparator and Comparable interfaces to sort objects.
In programming, you may often sort an object array. Objects often have multiple attributes. Generally, objects are sorted by a specific attribute value, there are many ways to sort by one of the attributes. The most direct one is to directly use an attribute value in the object for comparison whe
Java Comparator has two types: Comparable interface and Comparator interface.When sorting an array of objects, the comparator plays an obvious role. First, we will explain the Comparable interface.
Implement the
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
When you want to sort a collection or array that is not a simple numeric type, you can usually use comparator or comparable to implement an object sort or custom sort in a simple way.
A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort or arrays.sort) to allow precise control over the Sor T order. Comparat
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
Comparable is the sort of method implementation defined within the set;Comparator is the sort implemented outside the collectionComparator is located under Package Java.util , while comparable is under package Java.langComparable is an interface that the object itself has already supported for self-comparison, such as String, Integer itself can do the comparison
(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 learning notes]
1 public class Person implements Comparable {2 private String name; 3 private int age; 4 5 public Person () {6 super (); 7} 8 public Person (String name, int age) 9 {10 super (); 11 this. name = name; 12 this. age = age; 13} 14 15 16 @ Override17 public int hashCode () {18 19 return name. hashCode () + age * 27; 20} 21 @ Override22 public boolean equals (Object obj) {23 if (this = obj
1. Sorting by comparablePackage Comparable;import Java.util.arrays;public class Comparableuser implements comparable2. Sorting by implementing comparatorPackage Comparable;import Java.util.arrays;import java.util.comparator;class User {private String ID; private int age; Public User (String ID, int age) {this.id = ID; This.age = age; } public int Getage () {return age; public
TreeMap, Comparable, and Comparator in JavaWe know that the storage location of HashMap is stored according to the hashCode of the key object, while that of TreeMap is stored according to the hashCode, it is stored according to the compareTo method of the implemented Comparable interface. As long as the returned result of compareTo is 0, the two objects are equal
After using collections to collect objects, if the relative objects are sorted, we can use the sort () method in Java.util.Collections, because an index is required to sort, so the collections sort () method is subject to the list action object. Use the sort () method to sort objects only according to the sort method it provides, and if you want to define collations yourself, you can manipulate the CompareTo () method in Java.util.Comparable. or the Compara () method in the 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.