comparator

Why does Comparator declare equals?

心不动则不痛 提交于 2019-12-03 02:18:15
The Comparator interface has its own equals() method. Any class will get equals() by default through Object class. What is the need to have equals() method inside an interface? Comparator refines the contract of Object.equals : It has to satisfy the constraints set out by Object.equals and then some . Additionally, this method can return true only if the specified object is also a comparator and it imposes the same ordering as this comparator. Thus, comp1.equals(comp2) implies that sgn(comp1.compare(o1, o2))==sgn(comp2.compare(o1, o2)) for every object reference o1 and o2 . Declaring an equals

Sort String list in pre-defined custom ordering

吃可爱长大的小学妹 提交于 2019-12-02 23:26:30
问题 I have this project in which the program asks the user how many times it should run. During each run a list is created and elements are added. These elements in the array are sorted not alphabetically but sorted in the following order: q,w,e,r,t,y,u,i,o,p,l,k,j,h,g,f,d,s,a,z,x,c,v,b,n,m I am able to run my program up to the point where it accepts input, but I don't know what to do next... Sample input 2 3 what are you 2 sad set Sample Output what you are set sad Here is my code: import java

Why is equals not mandatory to implement in java.util.Comparator?

假装没事ソ 提交于 2019-12-02 20:47:20
Either in Javadoc as well as the code itself, Comparator interface defines: int compare(T o1, T o2); boolean equals(Object obj); But then this gives no probs compilating: Comparator a = new Comparator() { @Override public int compare(Object o1, Object o2) { //.. } }; But this does: Comparator a = new Comparator() { @Override public boolean equals(Object comparator) { //.. } }; How its done for the interface for allowing us not to override method? First of all JavaDocs explain clearly that you should implements this method: Additionally, this method can return true only if the specified object

Can I use a Comparator without implementing Comparable?

倖福魔咒の 提交于 2019-12-02 20:31:53
问题 Is it possible to use a Comparator without implementing the Comparable class? For instance, if I had the following: MyClass { Comparator comp; OrderedListInheritance(Comparator c) { this.comp = c; } } Could I then use comp to compare two objects? If so, how would I go about doing that? Thanks... 回答1: You don't use Comparable. You use Comparator. Comparable is an interface implemented by objects to specify their sort order with other objects of the same type. Comparator is a generic interface

Java 中 Comparable 和 Comparator 比较

泄露秘密 提交于 2019-12-02 20:26:47
本文,先介绍 Comparable 和 Comparator 两个接口,以及它们的差异;接着,通过示例,对它们的使用方法进行说明。 Comparable 简介 Comparable 是排序接口。 若一个类实现了Comparable接口,就意味着“ 该类支持排序 ”。 即然实现Comparable接口的类支持排序,假设现在存在“实现Comparable接口的类的对象的List列表(或数组)”,则该List列表(或数组)可以通过 Collections.sort(或 Arrays.sort)进行排序。 此外,“实现Comparable接口的类的对象”可以用作“有序映射(如TreeMap)”中的键或“有序集合(TreeSet)”中的元素,而不需要指定比较器。 Comparable 定义 Comparable 接口仅仅只包括一个函数,它的定义如下: package java.lang; import java.util.*; public interface Comparable<T> { public int compareTo(T o); } 说明: 假设我们通过 x.compareTo(y) 来“比较x和y的大小”。若返回“负数”,意味着“x比y小”;返回“零”,意味着“x等于y”;返回“正数”,意味着“x大于y”。 Comparator 简介 Comparator 是比较器接口。

I want to display the key value bean based on sorting of value in Struts 2

久未见 提交于 2019-12-02 20:21:15
问题 I want to display the key value bean based on sorting of value in Struts 2. <s:select list="preferredBranchList" listKey="displayKey" listValue="displayValue" name="preferredBranch" cssClass="selectpicker show-tick" required="true" /> its sorting the values based on listKey . please suggest me to sort the values based on listValue . 回答1: You can try and sort the values in Action class itself, so when select is rendered the list will appear sorted 回答2: The List is an ordered collection. If it

Why should a Comparator implement Serializable?

风格不统一 提交于 2019-12-02 20:04:54
New to Java. Learning it while working on an Android app. I am implementing a Comparator to sort a list of files and the android docs say that a Comparator should implement Serializable: It is recommended that a Comparator implements Serializable. This is the Serializable interface here . I just want to sort a list of files. Why should I implement this or what even is the reason why it should be for any Comparator? This is not just an Android thing, the Java SDK has the same recommendation : Note: It is generally a good idea for comparators to also implement java.io.Serializable, as they may

Java 8 Lambda: Comparator

十年热恋 提交于 2019-12-02 20:03:17
I want to sort a list with Lambda: List<Message> messagesByDeviceType = new ArrayList<Message>(); messagesByDeviceType.sort((Message o1, Message o2)->o1.getTime()-o2.getTime()); But I got this compilation error: Multiple markers at this line - Type mismatch: cannot convert from long to int - The method sort(Comparator<? super Message>) in the type List<Message> is not applicable for the arguments ((Message o1, Message o2) -> {}) Comparator#compareTo returns an int ; while getTime is obviously long . It would be nicer written like this: .sort(Comparator.comparingLong(Message::getTime)) The

What's the sort order of Java's Collections.sort(list, comparator)? small to big or big to small?

邮差的信 提交于 2019-12-02 19:56:33
Apparently, it's not documented or I missed it. Here 's the link to the documentation and below's the text as an image: EDIT (17/5): I think too many confused this question to be a comparator question. It is not. The comparator compares between 2 elements. According to that comparison, the list sorted. How? Ascending or Descending? I'll refine/simplify the question even further: If the comparator decides that element A is smaller than element B. In the sorted list , will element A be located at a lower index than element B? Andy Thomas The sort order is always ascending , where the Comparator

Sort String list in pre-defined custom ordering

巧了我就是萌 提交于 2019-12-02 13:23:35
I have this project in which the program asks the user how many times it should run. During each run a list is created and elements are added. These elements in the array are sorted not alphabetically but sorted in the following order: q,w,e,r,t,y,u,i,o,p,l,k,j,h,g,f,d,s,a,z,x,c,v,b,n,m I am able to run my program up to the point where it accepts input, but I don't know what to do next... Sample input 2 3 what are you 2 sad set Sample Output what you are set sad Here is my code: import java.util.*; public class WeirdWritings { public static void main(String[] args) { int data,word,ctr,i,ii;