comparator

How does C++ implicitly cast arguments to a comparator such as <?

≡放荡痞女 提交于 2019-12-08 18:43:59
问题 I had thought that this would be an easy question resolve via Google, but I can't seem to find a definitive (or even speculative) answer: When using a comparator statement, in which order does implicit casting occur? int i = -1; size_t t = 1; bool result = i < t; Is this equivalent to: bool result = i < int(t); // equals true or: bool result = size_t(i) < t; // equals false That is the easy part of the question - the second part is "what is the general rule", as it could be: The 'simpler'

Help comparing float member variables using Comparators

a 夏天 提交于 2019-12-08 16:39:07
问题 I am able to compare Strings fine, but would like to know how I can rank floating point numbers? getChange() returns a String. I want to be able to sort descending. How can I do this? UPDATE: package org.stocktwits.helper; import java.util.Comparator; import org.stocktwits.model.Quote; public class ChangeComparator implements Comparator<Quote> { public int compare(Quote o1, Quote o2) { float change1 = Float.valueOf(o1.getChange()); float change2 = Float.valueOf(o2.getChange()); if (change1 <

Inline comparator vs custom comparator in Java

微笑、不失礼 提交于 2019-12-08 15:53:54
问题 When sorting a list, is there any performance difference between using a java Comparator in-line (with an anonymous inner class) vs implementing a separate custom Comparator class? 1. public class SortByErrorComparator implements Comparator<WorkflowError> { public int compare(WorkflowError obj1, WorkflowError obj2) { return obj1.getErrorCode().compareTo(obj2.getErrorCode()); } } Collections.sort(list, new SortByErrorComparator()) ; 2. Collections.sort(list, new Comparator<WorkflowError>() {

Special order on String with the power of Java 8

梦想与她 提交于 2019-12-08 14:20:36
问题 I've got a finite list (or array) of strings. This list shall describe my new order by the following means: A string s1 is less than a string s2 iff one of the next three statements holds both are in the list an s1 has a lower index in the list than s2 both aren't in the list and s1 is less than s2 by the natural order on strings s1 is in the list and s2 is not I want to use the power of Java 8 to write a one line Comparator. What is the shortest such line? You can assume my list to be an

how to implement a comparator for StringBuffer class in Java for use in TreeSet?

为君一笑 提交于 2019-12-08 08:37:13
问题 I want to implement a Comparator for StringBuffer that compares the StringBuffer and adds to TreeSet accordingly. This is purely for learning purposes only. I know having a mutable object in a Hasable collection is a bad idea. but the purpose here is how to implement comparator for existing StringBuffer class of Java and use it to create a TreeSet. My current code is below. The code does not compile. kindly help me out here. Thanks public class ComparatorExample { public class

Hadoop MapReduce sort reduce output using the key

≡放荡痞女 提交于 2019-12-08 07:53:07
问题 down below there is a map-reduce program counting words of several text files. My aim is to have the result in a descending order regarding the amount of appearences. Unfortunately the program sorts the output lexicographically by the key. I want a natural order of the integer value. So I added a custom comparator with job.setSortComparatorClass(IntComparator.class) . But this doesn't work as expected. I'm getting the following exception: java.lang.Exception: java.nio.BufferUnderflowException

How do I initialize a std::set comparator?

£可爱£侵袭症+ 提交于 2019-12-08 04:50:30
问题 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

Why doesn't sorted(Comparator::reverseOrder) work?

故事扮演 提交于 2019-12-07 16:48:16
问题 The below Stream expression works perfectly fine: Stream<String> s = Stream.of("yellow","blue", "white"); s.sorted(Comparator.reverseOrder()) .forEach(System.out::print);` //yellowwhiteblue Why doesn't the equivalent one with method references compile? s.sorted(Comparator::reverseOrder).forEach(System.out::print); The type Comparator does not define reverseOrder(String, String) that is applicable here 回答1: A method reference is telling Java "treat this method as the implementation of a single

Comparator based on different nullable fields of an object

半腔热情 提交于 2019-12-07 16:25:44
问题 I have an Employee object which contains two fields name and jobTitle . For sorting the employee objects, first priority should be on jobTitle , if jobTitle is null then the sorting should be based on name. Below is the Employee object public class Employee { private String name; private String jobTitle; } I used chained Comparator with JobTitlecomparator and NameComparator to achieve this: public class EmployeeChainedComparator implements Comparator<Employee> { private List<Comparator

Using binarySearch with Comparator and regex

a 夏天 提交于 2019-12-07 09:38:31
问题 I am trying to write a quick search that searches a List<String> Instead of looping through the list and manually checking, I want to do this using binarySearch, but I am not sure how to do it. Old way: for(String s : list) { if(s.startsWith("contact.") return true; } Instead I would like something like this: Collections.sort(list); Collections.binarySearch(list, FindContactComparator()); Can someone help me write this Comparator? Is there any better way of doing this instead of using