comparable

Comparable VS <? extends Comparable>

北城以北 提交于 2021-02-13 12:15:07
问题 regarding the following code: public class Test <T extends Comparable>{ public static void main(String[] args){ List<String> lst = Array.asList("abc","def"); System.out.println(func(lst)); } public static boolean func(List<**here**> lst){ return lst.get(0).compareTo(lst.get(1)) == 0; } } why writing " ? extends Comparable" here would compile , and writing "Comparable" would not compile? thanks in advance. 回答1: This happens because generics are invariant . Even if String is a Comparable ,

Comparable VS <? extends Comparable>

梦想的初衷 提交于 2021-02-13 12:14:53
问题 regarding the following code: public class Test <T extends Comparable>{ public static void main(String[] args){ List<String> lst = Array.asList("abc","def"); System.out.println(func(lst)); } public static boolean func(List<**here**> lst){ return lst.get(0).compareTo(lst.get(1)) == 0; } } why writing " ? extends Comparable" here would compile , and writing "Comparable" would not compile? thanks in advance. 回答1: This happens because generics are invariant . Even if String is a Comparable ,

How is the rule for sorting a list in ascending order and descending order?

不羁的心 提交于 2021-02-10 05:33:08
问题 I have been trying to understand how sorting using Comparable works. I have class Activity implementing Comparable interface. I am trying to understand how activity.finish-this.finish; sorts in descending order and how this.finish-activity.finish sorts in ascending order. I am confused about what to use first for sorting in ascending and descending? class Activity implements Comparable { int start; int finish; public Activity(int start, int finish) { this.start = start; this.finish = finish;

Using Comparable to compare generic variables

懵懂的女人 提交于 2021-02-08 04:37:27
问题 For one of the Homeworks in my class, we have a collection of a class titled Pair and we need to sort it in ascending order based on the value of the key. I could apply this if the keys were strings or integers, but how do I write code that would compare my items when they're Generic as seen below? The professor in my class explained what to do for integers or strings but when my variables are generic I'm at a complete loss. Below are copies of the relevant parts of my code. import java.util.

Java generics class cast exception

好久不见. 提交于 2021-02-05 11:38:51
问题 I am trying to create a class that processes comparables. I boiled down to the simplest code that gives an error when I try to instantiate the class. I get a couple of compile warnings (unchecked cast) but when I run this program it throws a classcast exception. I did look at some of the other questions on this topic but didnt come across something useful. public class GD<Item extends Comparable<Item>> { private Item[] data; private final int MAX_SIZE = 200; public GD() { data = (Item[]) new

How can I retrieve all of the maximum values from a list?

℡╲_俬逩灬. 提交于 2021-02-05 06:19:54
问题 I have a class called Employee that implements the Comparable interface. Now I have 5 Employee objects in my list, each of which has its own salary property. I want to find all of the Employee objects that have the max salary. I can get a single object using Employee employee = Collections.max(employeeList); but that only returns a single Employee , while I am trying retrieve an array or list of all of the objects with the same max value. How can I do this? 回答1: To be efficient, you should

How can I retrieve all of the maximum values from a list?

痞子三分冷 提交于 2021-02-05 06:19:05
问题 I have a class called Employee that implements the Comparable interface. Now I have 5 Employee objects in my list, each of which has its own salary property. I want to find all of the Employee objects that have the max salary. I can get a single object using Employee employee = Collections.max(employeeList); but that only returns a single Employee , while I am trying retrieve an array or list of all of the objects with the same max value. How can I do this? 回答1: To be efficient, you should

Sort List<Map<String,Object>> based on value

蹲街弑〆低调 提交于 2021-01-19 09:03:43
问题 Basically I have a List<Map<String,Object>> , and I want to sort it by the values of certain key in the map. The problem is that I do not know the type... This map can contain Strings, Integers, Doubles, Floats etc.... I would like to sort it: So far List<Map<String,Object>> data = getResults(); Collections.sort(data, (o1, o2) -> (String.valueOf(o2.get("Field1"))) .compareTo((String.valueOf(o1.get("Field1"))))); It is not a great Idea since numbers are not properly sorted.... How can I handle

Sort List<Map<String,Object>> based on value

拟墨画扇 提交于 2021-01-19 09:01:26
问题 Basically I have a List<Map<String,Object>> , and I want to sort it by the values of certain key in the map. The problem is that I do not know the type... This map can contain Strings, Integers, Doubles, Floats etc.... I would like to sort it: So far List<Map<String,Object>> data = getResults(); Collections.sort(data, (o1, o2) -> (String.valueOf(o2.get("Field1"))) .compareTo((String.valueOf(o1.get("Field1"))))); It is not a great Idea since numbers are not properly sorted.... How can I handle

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