comparator

Why does Collections.sort call Comparator twice with the same arguments?

你离开我真会死。 提交于 2020-05-11 09:07:49
问题 I'm running an example to understand the behavior of Comparator in Java. import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; class HDTV { private int size; private String brand; public HDTV(int size, String brand) { this.size = size; this.brand = brand; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } } class

Why does Collections.sort call Comparator twice with the same arguments?

余生颓废 提交于 2020-05-11 09:07:36
问题 I'm running an example to understand the behavior of Comparator in Java. import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; class HDTV { private int size; private String brand; public HDTV(int size, String brand) { this.size = size; this.brand = brand; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } } class

Access private variables outside of a class in Java using comparator [duplicate]

谁说我不能喝 提交于 2020-05-09 17:38:07
问题 This question already has answers here : How to sort a list by a private field? (10 answers) Closed 18 days ago . Struggling to figure out how to access private attributes outside of a class in Java, using comparator. I'm using some code online as reference. If you change the private to public it works, but I need to know how to make it work with the variables set as private. // Java program to demonstrate working of Comparator // interface import java.util.*; import java.lang.*; import java

Access private variables outside of a class in Java using comparator [duplicate]

亡梦爱人 提交于 2020-05-09 17:37:05
问题 This question already has answers here : How to sort a list by a private field? (10 answers) Closed 18 days ago . Struggling to figure out how to access private attributes outside of a class in Java, using comparator. I'm using some code online as reference. If you change the private to public it works, but I need to know how to make it work with the variables set as private. // Java program to demonstrate working of Comparator // interface import java.util.*; import java.lang.*; import java

stable_sort on a vector of pairs by first element in pair in increasing order without comparator function in C++

假如想象 提交于 2020-05-09 05:57:46
问题 #include <bits/stdc++.h> using namespace std; int main() { vector<pair<int,int>>v; v.push_back(make_pair(1,3)); v.push_back(make_pair(1,1)); v.push_back(make_pair(2,19)); v.push_back(make_pair(2,4)); int n = 4; stable_sort(v.begin(),v.end()); for (int i = 0; i < n; i++) cout << "[" << v[i].first << ", " << v[i].second << "] "; return 0; } Output : [1, 1] [1, 3] [2, 4] [2, 19] Expected Output : [1, 3] [1, 1] [2, 19] [2, 4] Why does the vector of pairs not maintain the relative ordering even

TreeSet Comparator failed to remove duplicates in some cases?

百般思念 提交于 2020-04-11 04:45:18
问题 I have the following comparator for my TreeSet: public class Obj { public int id; public String value; public Obj(int id, String value) { this.id = id; this.value = value; } public String toString() { return "(" + id + value + ")"; } } Obj obja = new Obj(1, "a"); Obj objb = new Obj(1, "b"); Obj objc = new Obj(2, "c"); Obj objd = new Obj(2, "a"); Set<Obj> set = new TreeSet<>((a, b) -> { System.out.println("Comparing " + a + " and " + b); int result = a.value.compareTo(b.value); if (a.id == b

TreeSet Comparator failed to remove duplicates in some cases?

余生长醉 提交于 2020-04-11 04:44:47
问题 I have the following comparator for my TreeSet: public class Obj { public int id; public String value; public Obj(int id, String value) { this.id = id; this.value = value; } public String toString() { return "(" + id + value + ")"; } } Obj obja = new Obj(1, "a"); Obj objb = new Obj(1, "b"); Obj objc = new Obj(2, "c"); Obj objd = new Obj(2, "a"); Set<Obj> set = new TreeSet<>((a, b) -> { System.out.println("Comparing " + a + " and " + b); int result = a.value.compareTo(b.value); if (a.id == b

equals() vs compareTo() in Comparator/able (Theoretical)

我的未来我决定 提交于 2020-02-28 07:35:06
问题 I don,t understand Javadoc: The natural ordering for a class C is said to be consistent with equals if and only if (e1.compareTo((Object)e2) == 0) has the same boolean value as e1.equals((Object)e2) for every e1 and e2 of class C. Why should be that way? I understand that e1.equals(e2)=true should always imply e1.compareTo(e2)==0, but I cannot understand why the opposite should be true. Compareness is not equalness! 2 equal objects should be compared to zero, but 2 differents ones should be

sort HashMap in reverse? [duplicate]

左心房为你撑大大i 提交于 2020-02-24 12:09:07
问题 This question already has answers here : How to use a Java8 lambda to sort a stream in reverse order? (7 answers) Comparator.reversed() does not compile using lambda (2 answers) Closed 2 years ago . So I came across this method which is able to sort HashMaps by value. public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) { return map.entrySet() .stream() .sorted(Map.Entry.comparingByValue()) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue,

Collections.sort() Comparison method violates its general contract in Java [duplicate]

Deadly 提交于 2020-01-30 13:17:10
问题 This question already has answers here : “Comparison method violates its general contract!” (11 answers) Closed 2 years ago . I know that this kind of question has been asked millions of times if not billions, however I couldn't find my answer yet :) This compare() method doesn't have Long , Double , Float , ..., it only has Date , boolean , and Null checker, however it shows me that contract violation error , can any one help plz? Collections.sort(users, new Comparator<MiniUser>() {