comparator

Java PriorityQueue Comparator - How/When do you sort?

泪湿孤枕 提交于 2019-12-07 09:18:30
问题 I'm initialising a Priority Queue like: strategy = new FuelPriority(); incoming = new PriorityQueue<Vehicle>(1, strategy); The code for my Comparator class is: public class FuelPriority implements Comparator<Object> { public int compare(Object o1, Object o2) { Vehicle a1 = (Vehicle) o1; Vehicle a2 = (Vheicle) o2; return Integer.compare(a1.getFuelLevel(), a2.getFuelLevel()); } } After running a simulation, the elements aren't ordered at all - they are random; I set a breakpoint in the compare

Attempting to use Comparator to sort by name, ignore case, as well as nulls first

不想你离开。 提交于 2019-12-07 08:35:24
问题 I am having issues using the Java 8 Comparator class to sort a list of items. My current working comparator is below: comparator = Comparator.comparing(Person::getName, Comparator.nullsFirst(Comparator.naturalOrder())); This works: it orders the list by name with the null values first. However, I am now attempting to ignore case of the names. I know that I can write a new getter that returns the name all lowercase, but I do not want to go with this approach as I have to do this for multiple

Make TreeMap Comparator tolerate null

瘦欲@ 提交于 2019-12-07 07:29:07
问题 This customized Valuecomparator sorts the TreeMap by it's value. But it doesn't tolerate nullpointexception when searching whether the TreeMap has a certain key. How do i modify the comparator to handle the nullpoint? import java.io.IOException; import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; public class TestTreeMap { public static class ValueComparator<T> implements Comparator<Object> { Map<T, Double> base; public ValueComparator(Map<T,

How do I initialize a std::set comparator?

…衆ロ難τιáo~ 提交于 2019-12-07 05:36:26
I need to initialize some comparator of the new data type TType based on std::set with some object o of another class Object: typedef std::set <unsigned int, sortSet(o)> TType This declaration is otside the class (in header file). At the time of the declaration this object does not have to exist, it will be created later. class sortSet { private: Object o; public: sortSet(Object &oo): o(oo) {} bool operator() ( const unsigned int &i1, const unsigned int &i2 ) const { //Some code... } }; If the declaration was inside some method (when object has already been created), situation would be quite

Unable to replicate : “Comparison method violates its general contract!”

北城以北 提交于 2019-12-07 02:41:10
问题 I receive the following error: "Comparison method violates its general contract!" when using the following comparator, however I am unable to replicate the exception using jUnit. I'd like to know what caused this issue and how to replicate it. There are examples of others having the same problem but not how to replicate it. public class DtoComparator implements Comparator<Dto> { @Override public int compare(Dto r1, Dto r2) { int value = 0; value = r1.getOrder() - r2.getOrder(); if (value == 0

Comparator<String> must override super class method

不打扰是莪最后的温柔 提交于 2019-12-06 21:16:19
问题 I'm making a TreeMap<String, String> and want to order it in a descending fashion. I created the following comparator: Comparator<String> descender = new Comparator<String>() { @Override public int compare(String o1, String o2) { return o2.compareTo(o1); } }; I construct the TreeMap like so: myMap = new TreeMap<String, String>(descender); However, I'm getting the following error: The method compare(String, String) of type new Comparator<String>(){} must override a superclass method I've never

in java, use String::length for Comparator.comparing()

落爺英雄遲暮 提交于 2019-12-06 10:41:28
I have a code piece like this: String[] yetToSortedArray = {"Abc","hello world","nihao?","chilemaNin"}; Arrays.parallelSort(yetToSortedArray, Comparator.comparing(String::length)); for(String str : yetToSortedArray){ System.out.println(str + ", "); } My question is here: "String::length" What am I really passing in to Comparator.comparing()? Why there is no parenthesis for String::length()? I think I am using this : https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#comparing-java.util.function.Function-java.util.Comparator- And it says "Accepts a function that extracts a

Unable to add a logic to sort with dates in the existing comparator in Java/android

余生颓废 提交于 2019-12-06 05:35:32
I want to add a condition that also sorts according to the DATE (desc order) of the item.startReading. item.startReading value has the date value in the string format 2018-10-20T12:05:41 . But I have no clue how to add this logic to the current code. Before the user clicks the magazine or the book item, the value of item.startReading is null . . When the user clicks the either one of the items, I want the newest item.startReading to go to the top like the following. Sorry for my explanation, but I really need to know how to do since I started learning java/android. Some examples or tips would

Sorting Map using Comparator

北城余情 提交于 2019-12-06 05:01:09
I'm trying Comparator to implement a sort in TreeMap according to a sequence. final String sequence="People,Object,Environment,Message,Service"; Comparator<String> comparator = new Comparator<String>() { @Override public int compare(String key1, String key2) { int returned = sequence.indexOf(key1) - sequence.indexOf(key2); if (returned == 0 && !key1.contains(key2)) returned = -1; return returned; } }; List<String> list=new ArrayList<String>(); Map<String,String> lhm = new TreeMap<String,String>(comparator); // Put elements to the map lhm.put("Object", "biu"); lhm.put("Message", "nuios"); lhm

Angular 2: what differences between comparison operators == and === in ngIf directive

末鹿安然 提交于 2019-12-06 02:39:08
问题 I don't understand why these two operators exist. In case of boolean comparison both == and === seem to work, but in case of enum comparison only '==' works: <div class="interventionGroup"> <div class="interventionGroupHeader transition_1s" (click)="onClickHeader()"> {{GroupName}} <div *ngIf="expanded == true" class="expand-icon"><i class="material-icons">expand_less</i></div> <!-- WORKS --> <div *ngIf="expanded === false" class="expand-icon"><i class="material-icons expand-icon">expand_more<