comparator

Comparators in STL

て烟熏妆下的殇ゞ 提交于 2019-12-31 22:47:43
问题 I am using struct minHeap to generate a min heap using priority_queue .And function comp to print numbers in reverse order using sort function given in STL . Now my doubt is that I can not use struct minHeap in function sort and can not use function comp in priorityQueue . I feel that function of both struct minHeap and comp is similar. Please explain me when to use structs for comaprator and when to use normal functions to behave as comparators in STL ? #include<iostream> #include <queue>

Compare method throw exception: Comparison method violates its general contract [duplicate]

烂漫一生 提交于 2019-12-31 02:39:27
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: why does my compare method throw exception — Comparison method violates its general contract! I've this code: class TimeComparatorTipo0 implements Comparator { @Override public int compare(Object a, Object b) { String Time1 = ((DataImportedTipo0) a).ora; Long VolTot1 = Long.parseLong(((DataImportedTipo0) a).volume_totale); String Time2 = ((DataImportedTipo0) b).ora; Long VolTot2 = Long.parseLong((

How to get inverse of a comparator in java

老子叫甜甜 提交于 2019-12-30 17:31:29
问题 In a method I receive a generic object E extends Comparable<E> as an argument. Now i want to create two priority queues.One which uses the comparator used by E and other queue which uses the opposite of comparator used by E(i.e. if E uses '<' then second queue must use '>='). Please hep me how to create two such queues. queue2=new PriorityQueue<E>(0,Collections.reverseOrder(e)); I am getting the error that reverseOrder is not applicable. please help 回答1: Look at Collections.reverseOrder. 回答2:

Scala: Is there a way to use PriorityQueue like I would in Java?

跟風遠走 提交于 2019-12-30 09:49:27
问题 I have a class that I would like to use in a scala.collection.mutable.PriorityQueue, but I don't want to make it Ordered[A] just for this one purpose. I don't consider the ordering I want to use with respect to the PriorityQueue as the natural ordering of the class. class MyObject (sequence: Int, values: List[String]) ... So, in my PriorityQueue, I would like the values to be ordered by 'sequence'. However, just because two objects have the same sequence doesn't make them naturally equal

Java: PriorityQueue returning incorrect ordering from custom comparator? [duplicate]

偶尔善良 提交于 2019-12-29 07:49:08
问题 This question already has answers here : The built-in iterator for java's PriorityQueue does not traverse the data structure in any particular order. Why? (5 answers) Closed 3 years ago . I've written a custom comparator to compare my node classes, but the java priority queue is not returning my items in the correct order. Here is my comparator: public int compare(Node n1, Node n2){ if (n1.getF() > n2.getF()){ return +1; } else if (n1.getF() < n2.getF()){ return -1; } else { // equal return 0

Java, Why it is implied that objects are equal if compareTo() returns 0?

大城市里の小女人 提交于 2019-12-29 06:38:30
问题 Let's have a class Person . Person has a name and height. Equals and hashCode() takes into account only name. Person is comparable (or we implement comparator for it, does not matter which one). Persons are compared by height. It seems reasonable to expect a situation where two different persons can have same height, but eg. TreeSet behaves like comapareTo()==0 means equals, not merely same size. To avoid this, comparison can secondarily look at something else if size is the same, but then it

Java, Why it is implied that objects are equal if compareTo() returns 0?

女生的网名这么多〃 提交于 2019-12-29 06:38:08
问题 Let's have a class Person . Person has a name and height. Equals and hashCode() takes into account only name. Person is comparable (or we implement comparator for it, does not matter which one). Persons are compared by height. It seems reasonable to expect a situation where two different persons can have same height, but eg. TreeSet behaves like comapareTo()==0 means equals, not merely same size. To avoid this, comparison can secondarily look at something else if size is the same, but then it

Comparator and equals()

只愿长相守 提交于 2019-12-29 06:09:36
问题 Suppose I need TreeSet with elements sorted with some domain logic. By this logic it doesn't matter order of some elements that doesn't equal so compare method can return 0, but in this case I couldn't put them in TreeSet . So, question: what disadvantages I'll have from code like this: class Foo implements Comparable<Foo>{} new TreeSet<Foo>(new Comparator<Foo>(){ @Override public int compare(Foo o1, Foo o2) { int res = o1.compareTo(o2); if(res == 0 || !o1.equals(o2)){ return o1.hashCode() -

Comparison method violates its general contract Exception

半城伤御伤魂 提交于 2019-12-29 01:43:12
问题 Below is a block of code that results in exception as indicated, Code : Collections.sort( arrayList, new Comparator() { public int compare( Object o1, Object o2 ) { TypeAdapterSort tas1 = ( TypeAdapterSort ) o1; TypeAdapterSort tas2 = ( TypeAdapterSort ) o2; if ( tas1.order < tas2.order ) return -1; else return 1; } } ); Exception : java.lang.IllegalArgumentException: Comparison method violates its general contract! at java.util.TimSort.mergeLo(TimSort.java:747) at java.util.TimSort.mergeAt

java comparator, how to sort by integer?

落爺英雄遲暮 提交于 2019-12-28 11:42:53
问题 Im trying to learn comparator in java and I have found this great example online, my question is how would this code be changed so that the pet names are ordered by age and in descending order so that the oldest is first and youngest is last? class Dog implements Comparator<Dog>, Comparable<Dog>{ private String name; private int age; Dog(){ } Dog(String n, int a){ name = n; age = a; } public String getDogName(){ return name; } public int getDogAge(){ return age; } // Overriding the compareTo