comparator

How to write a custom Comparator for TreeMap in Java?

大城市里の小女人 提交于 2020-06-29 08:21:25
问题 I want to store key-value pairs in TreeMap and sort the entries based on the value of Key as per following logic: Sort by the length of the key. If the length of two keys is same then sort them alphabetically. Example, for the following key-value pairs. IBARAKI MitoCity TOCHIGI UtunomiyaCity GUNMA MaehashiCity SAITAMA SaitamaCity CHIBA ChibaCity TOKYO Sinjyuku KANAGAWA YokohamaCity The expected output is like this. CHIBA : ChibaCity GUNMA : MaehashiCity TOKYO : Sinjyuku IBARAKI : MitoCity

Why Double::compareTo can be used as an argument of Stream.max(Comparator<? super T> comparator)

◇◆丶佛笑我妖孽 提交于 2020-06-25 04:42:13
问题 The api for Stream.max requires an argument of type Comparator<? super T> , and for Comparator , the only abstract method is int compare(T o1, T o2) but Double::compareTo , the compareTo api is public int compareTo(Double anotherDouble) why just provide one argument, so why can Double::compareTo use as argument of Stream Optional<T> max(Comparator<? super T> comparator) 回答1: The below MyComparator implement a Comparator . It takes two arguments. It's the same as the lambda expression (d1,d2)

How does implements Comparator work in java?

隐身守侯 提交于 2020-05-14 01:29:08
问题 Java Comparator interface - Here is an example of how Comparator works. My question is : Where is Object type o1 and o2 coming when the method compare(Object o1,Object o2) ? I don't know see any Class invoke the compare() method. I only see Collections.sort(al,new NameComparator()); Please explain, thank you. 回答1: When you call Collections.sort on any collection and pass the comparator reference , The underlying sorting method makes a call to compare method to decide which one is greater

How does implements Comparator work in java?

北城余情 提交于 2020-05-14 01:29:07
问题 Java Comparator interface - Here is an example of how Comparator works. My question is : Where is Object type o1 and o2 coming when the method compare(Object o1,Object o2) ? I don't know see any Class invoke the compare() method. I only see Collections.sort(al,new NameComparator()); Please explain, thank you. 回答1: When you call Collections.sort on any collection and pass the comparator reference , The underlying sorting method makes a call to compare method to decide which one is greater

java comparator for arrays sort [duplicate]

时光怂恿深爱的人放手 提交于 2020-05-13 15:28:27
问题 This question already has answers here : Sort arrays of primitive types in descending order (21 answers) Closed 3 years ago . My code is shown below: public class Solution { public void nextPermutation(int[] nums) { int k = 0; for(int i = nums.length -1; i> 0 ;i--){ if(nums[i-1] < nums[i]){ k = i-1; break; } } if( k == 0) {Arrays.sort(nums); return;} int tmp = nums[k]; nums[k] = nums[nums.length - 1]; nums[nums.length-1] = tmp; Arrays.sort(nums,k+1,nums.length,new Comparator<Integer>(){

java comparator for arrays sort [duplicate]

人盡茶涼 提交于 2020-05-13 15:28:04
问题 This question already has answers here : Sort arrays of primitive types in descending order (21 answers) Closed 3 years ago . My code is shown below: public class Solution { public void nextPermutation(int[] nums) { int k = 0; for(int i = nums.length -1; i> 0 ;i--){ if(nums[i-1] < nums[i]){ k = i-1; break; } } if( k == 0) {Arrays.sort(nums); return;} int tmp = nums[k]; nums[k] = nums[nums.length - 1]; nums[nums.length-1] = tmp; Arrays.sort(nums,k+1,nums.length,new Comparator<Integer>(){

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

让人想犯罪 __ 提交于 2020-05-11 09:10:13
问题 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