comparator

Wrong order in java.util.PriorityQueue and specific Comparator

时光毁灭记忆、已成空白 提交于 2019-12-02 12:57:01
i am very confused with this little example of java.util.PriorityQueue and my own Comparator: In this code i get a wrong order in the queue. The result is: 5,8,7 instead of 5,7,8 Is there anything wrong with my Comparator<Vertex> ? Thank you for your help. public class Test { public static void main(String[] args) { PriorityQueue<Vertex> priorityQueue = new PriorityQueue<Vertex>(new Comparator() { @Override public int compare(Object o1, Object o2) { Vertex u = (Vertex) o1; Vertex v = (Vertex) o2; return Integer.compare(new Integer(u.distance), new Integer(v.distance)); } }); Vertex vertex1 =

sort a List based on a parameter available in another class

我们两清 提交于 2019-12-02 09:03:13
问题 I have some design problems with Java Comparator Interface. I have a class which contains a Set of a simple custom data structure: class data { Long ID; int Priority; ... } ID s are unique, so it is possible to get the whole data using ID‍‍‍‍‍ . and the container class: class Container { Set<data> mySet = ...; List<Long> myList = ...; ... } for some inevitable reasons, I need to keep a sorted List of data IDs in parallel. I need the List to be sorted by Priority . Since, the Comparator should

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

邮差的信 提交于 2019-12-02 08:13:05
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 . You can try and sort the values in Action class itself, so when select is rendered the list will appear sorted Roman C The List is an ordered collection. If it contains elements sorted by the key property then other the value property remains unsorted unless it's

Very big JTable, RowFilter and extra load

假如想象 提交于 2019-12-02 07:07:35
I wanted to ask for a clarification about the use of RowFilter and its effects on performances. I implemented a filter through the method include(Entry) that for every row simply checks if its correspondent value in the model has a boolean flag set: if so, returns true, else false. Now, the JTable I have can be pretty big (1000000+ rows), and I wasn't sure if this simple filtering, applied to such a big input set, could be costly. How does the mapping between filtered rows and underlying data work exactly? I mean, does it store any extra data or it just draws the rows that match the filter "on

std::map, custom comparator's design constraints

做~自己de王妃 提交于 2019-12-02 06:38:55
问题 I have been trying defining a custom comparator for std::map container. The question is : can I relay on == and != operators or would that break strict weak order ? Am I forced to use operator < ? (One and Two classes do have operator!= and operator== defined correctly) typedef std::pair<One, Two> MapKey_t; class cmp { bool operator()(const MapKey_t& left, const MapKey_t& right) const { return left.first != right.first && right.first == right.second; } } typedef std::map<MapKey_t, Three*, cmp

Ordering with partial explicit and then another order?

我与影子孤独终老i 提交于 2019-12-02 06:09:25
问题 What I need is to order a list in a custom way, I'm looking into the correct way and found guava's Ordering api but the thing is that the list I'm ordering is not always going to be the same, and I just need 2 fields to be at the top of the list, for example I have this: List<AccountType> accountTypes = new ArrayList<>(); AccountType accountType = new AccountType(); accountType.type = "tfsa"; AccountType accountType2 = new AccountType(); accountType2.type = "rrsp"; AccountType accountType3 =

sort a List based on a parameter available in another class

我是研究僧i 提交于 2019-12-02 04:42:48
I have some design problems with Java Comparator Interface. I have a class which contains a Set of a simple custom data structure: class data { Long ID; int Priority; ... } ID s are unique, so it is possible to get the whole data using ID‍‍‍‍‍ . and the container class: class Container { Set<data> mySet = ...; List<Long> myList = ...; ... } for some inevitable reasons, I need to keep a sorted List of data IDs in parallel. I need the List to be sorted by Priority . Since, the Comparator should compare Priority s it should implements Comparator<int> . But the List only contains ID s and the

Comparing generic types using Comparable and Comparator

倖福魔咒の 提交于 2019-12-02 04:08:49
问题 I've run into a headache I'm having difficulty debugging. I am trying to compare two generic values so I can insertion sort them according to values into an array. This is my first time working with the Comparable and Comparator interfaces so any additional advice surrounding these issues would be great. This is how my class is set up: public class SVStore<K, V extends Comparable<V>> implements Pairs<K, V>, Iterable<K>, Comparable<V>, Comparator<V> { The put() method: @Override public V put(K

Java Long Compare and ValueOf method undefined

倖福魔咒の 提交于 2019-12-02 04:07:12
I am referencing my java version JDK 1.8 but I am still getting error. What is wrong with this referencing (writing Java after 6 years)? or any other simpler way to do this? I did some search and these functions are available in later java versions. Eclipse is Oxygen The method valueOf(Long) is undefined for the type Long The method compareTo() is undefined for the type Long import java.util.Comparator; import java.lang.Long; public class MyComparator<Long> implements Comparator<Long>{ @Override public int compare(Long long1, Long long2) { //Long.compareTo() return Long.valueOf(long1)

std::map, custom comparator's design constraints

对着背影说爱祢 提交于 2019-12-02 03:58:29
I have been trying defining a custom comparator for std::map container. The question is : can I relay on == and != operators or would that break strict weak order ? Am I forced to use operator < ? (One and Two classes do have operator!= and operator== defined correctly) typedef std::pair<One, Two> MapKey_t; class cmp { bool operator()(const MapKey_t& left, const MapKey_t& right) const { return left.first != right.first && right.first == right.second; } } typedef std::map<MapKey_t, Three*, cmp> MyMap_t; Since switching left with right would not change the comparator return value, will this be