comparator

Java 8 Comparator nullsFirst naturalOrder confused

落花浮王杯 提交于 2019-11-27 04:27:21
问题 this may be a simple question but I would like to understand it clearly... I have a code like this: public final class Persona { private final int id; private final String name public Persona(final int id,final String name) { this.id = id; this.name = name; } public int getId(){return id;} public String getName(){return name;} @Override public String toString(){return "Persona{" + "id=" + id + ", name=" + name+'}';} } And I am testing this code: import static java.util.Comparator.*; private

What does comparison being consistent with equals mean ? What can possibly happen if my class doesn't follow this principle?

核能气质少年 提交于 2019-11-27 04:15:47
From the JavaDoc of TreeMap : Note that the ordering maintained by a sorted map (whether or not an explicit comparator is provided) must be consistent with equals if this sorted map is to correctly implement the Map interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Map interface is defined in terms of the equals operation, but a map performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the sorted map, equal. The behavior of a sorted map

Implementing Java Comparator

无人久伴 提交于 2019-11-27 02:45:08
问题 I am trying to write an algorithm which utilizes a min-priority queue, so I looked around on google and found the PriorityQueue. It seems that in order to use it, though, I am going to need to tell it how I want it to prioritize, and that the way to do this is with a comparator (I want to compare specific data fields of my "Node1" objects). More googling presented the idea of creating a new comparator which implements Comparator but overrides the compare method. What I am trying is this (and

Comparable and Comparator contract with regards to null

こ雲淡風輕ζ 提交于 2019-11-27 01:57:04
Comparable contract specifies that e.compareTo(null) must throw NullPointerException . From the API : Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false . On the other hand, Comparator API mentions nothing about what needs to happen when comparing null . Consider the following attempt of a generic method that takes a Comparable , and return a Comparator for it that puts null as the minimum element. static <T extends Comparable<? super T>> Comparator<T> nullComparableComparator() { return new

What's the difference between “LIKE” and “=” in SQL?

岁酱吖の 提交于 2019-11-27 01:44:50
问题 Is there any difference between: SELECT * FROM users WHERE username="davyjones" and SELECT * FROM users WHERE username LIKE "davyjones" (I think I've bungled up the syntax... pardon me for that, I'm mostly a desktop app development guy) 回答1: As per SQL standard, the difference is treatment of trailing whitespace in CHAR columns. Example: create table t1 ( c10 char(10) ); insert into t1 values ('davyjones'); select * from t1 where c10 = 'davyjones'; -- yields 1 row select * from t1 where c10

STL Priority Queue on custom class

让人想犯罪 __ 提交于 2019-11-27 00:47:22
问题 I'm having a lot of trouble getting my priority queue to recognize which parameter it should sort by. I've overloaded the less than operator in my custom class but it doesn't seem to use it. Here's the relevant code: Node.h class Node { public: Node(...); ~Node(); bool operator<(Node &aNode); ... } Node.cpp #include "Node.h" bool Node::operator<(Node &aNode) { return (this->getTotalCost() < aNode.getTotalCost()); } getTotalCost() returns an int main.cpp priority_queue<Node*, vector<Node*>

how to sort an ArrayList in ascending order using Collections and Comparator

不羁的心 提交于 2019-11-26 23:18:33
问题 How to sort an ArrayList in ascending order using Comparator ? I know how to sort it in descending order using: Comparator mycomparator = Collections.reverseOrder(); then Collections.sort(myarrayList,mycomparator); just want to know how to sort it in ascending order using Collections and comparator? Thanks! 回答1: Just throwing this out there...Can't you just do: Collections.sort(myarrayList); It's been awhile though... 回答2: Use the default version: Collections.sort(myarrayList); Of course this

Create a SortedMap in Java with a custom Comparator

瘦欲@ 提交于 2019-11-26 21:28:39
问题 I want to create a TreeMap in Java with a custom sort order. The sorted keys which are string need to be sorted according to the second character. The values are also string. Sample map: Za,FOO Ab,Bar 回答1: You can use a custom comparator like this: Comparator<String> secondCharComparator = new Comparator<String>() { @Override public int compare(String s1, String s2) { return s1.substring(1, 2).compareTo(s2.substring(1, 2)); } }; Sample: SortedMap<String,String> map = new TreeMap<String,String

PriorityQueue.toString wrong element order

倖福魔咒の 提交于 2019-11-26 21:02:59
I am trying to make a priority queue in java with the nodes with the lowest frequency in priority. However, my comparator is not working and the output is very weird. I believe I need to change my comparator but I am not sure how to change it. Here is my code: public class HuffmanComparator implements Comparator<TreeNodeHuffman> { public int compare(TreeNodeHuffman p1, TreeNodeHuffman p2) { if (p1.frequency < p2.frequency) return -1; if (p1.frequency > p2.frequency) return 1; return 0; } } public class TreeNodeHuffman { public static void main(String[] args) { HuffmanComparator compare = new

compare object properties and show diff in PHP

馋奶兔 提交于 2019-11-26 20:38:31
问题 I'm searching for a way to show me the different properties/values from given objects... $obj1 = new StdClass; $obj1->prop = 1; $obj2 = new StdClass; $obj2->prop = 2; var_dump(array_diff((array)$obj1, (array)$obj2)); //output array(1) { ["prop"]=> int(1) } This works very well as long the property is not a object or array. $obj1 = new StdClass; $obj1->prop = array(1,2); $obj2 = new StdClass; $obj2->prop = array(1,3); var_dump(array_diff((array)$obj1, (array)$obj2)) // Output array(0) { } //