comparable

MyClass cannot be cast to java.lang.Comparable: java.lang.ClassCastException

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 23:35:29
问题 I am doing a java project and I got this problem and don't know how to fix it. The classes in my project (simplified): public class Item { private String itemID; private Integer price; public Integer getPrice() { return this.price; } } public class Store { private String storeID; private String address; } public class Stock { private Item item; private Store store; private Integer itemCount; public Integer getInventoryValue() { return this.item.getPrice() * this.itemCount; } } Then I try to

Java Sorting: sort an array of objects by property, object not allowed to use Comparable

纵饮孤独 提交于 2019-11-26 20:43:04
问题 I have a class, Library, that contains an array of Book objects, and I need to sort the array based off the properties of Book, either Title or PageNumber. The problem is im not allowed to use the Comparable class with Book. How would you recommend I sort the array of Books in library? Write my own sort? Or is there an easier way? If you need snippets of code, just ask! 回答1: You can provide a Comparator for comparing any type you wish, Comparable or otherwise. For Arrays and Collections you

Comparing the values of two generic Numbers

爷,独闯天下 提交于 2019-11-26 18:57:15
I want to compare to variables, both of type T extends Number . Now I want to know which of the two variables is greater than the other or equal. Unfortunately I don't know the exact type yet, I only know that it will be a subtype of java.lang.Number . How can I do that? EDIT : I tried another workaround using TreeSet s, which actually worked with natural ordering (of course it works, all subclasses of Number implement Comparable except for AtomicInteger and AtomicLong). Thus I'll lose duplicate values. When using List s, Collection.sort() will not accept my list due to bound mismatchs. Very

What do < and > mean such as implements Comparable<BigInteger>?

﹥>﹥吖頭↗ 提交于 2019-11-26 17:18:26
问题 In Java 1.4.2, class java.math.BigInteger implements interfaces Comparable , Serializable . In Java 1.5.0, class java.math.BigInteger implements interfaces Serializable , Comparable<BigInteger> . This is just an example to help me ask about < and > . What I am really wondering about is the < and > stuff. My question is threefold: what does the <BigInteger> part of the implements statement mean? what is that syntax called? and what does it do? P.S.: It's really hard to google for < and > and

Why doesn't java.lang.Number implement Comparable? [duplicate]

对着背影说爱祢 提交于 2019-11-26 17:10:32
This question already has an answer here: Comparing the values of two generic Numbers 12 answers Does anyone know why java.lang.Number does not implement Comparable ? This means that you cannot sort Number s with Collections.sort which seems to me a little strange. Post discussion update: Thanks for all the helpful responses. I ended up doing some more research about this topic . The simplest explanation for why java.lang.Number does not implement Comparable is rooted in mutability concerns. For a bit of review, java.lang.Number is the abstract super-type of AtomicInteger , AtomicLong ,

What is the best way to get min and max value from a list of Comparables that main contain null values?

自闭症网瘾萝莉.ら 提交于 2019-11-26 17:05:54
问题 I am thinking about something like this: public static <T extends Comparable<T>> T minOf(T...ts){ SortedSet<T> set = new TreeSet<T>(Arrays.asList(ts)); return set.first(); } public static <T extends Comparable<T>> T maxOf(T...ts){ SortedSet<T> set = new TreeSet<T>(Arrays.asList(ts)); return set.last(); } But is not null safe, which is something I want too. Do you know a better way to solve this problem? EDIT: After the comments I have also tried min(): public static <T extends Comparable<T>>

How do I write a compareTo method which compares objects?

你。 提交于 2019-11-26 16:25:23
I am learning about arrays, and basically I have an array that collects a last name, first name, and score. I need to write a compareTo method that will compare the last name and then the first name so the list could be sorted alphabetically starting with the last names, and then if two people have the same last name then it will sort the first name. I'm confused, because all of the information in my book is comparing numbers, not objects and Strings. Here is what I have coded so far. I know it's wrong but it at least explains what I think I'm doing: public int compare(Object obj) // creating

What does comparison being consistent with equals mean ? What can possibly happen if my class doesn&#39;t follow this principle?

烂漫一生 提交于 2019-11-26 12:43:38
问题 From the JavaDoc of TreeMap : Note that the ordering maintained by a sorted map (whether or not an explicit comparator is provided) must be consistent with equals if this sorted map is to correctly implement the Map interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Map interface is defined in terms of the equals operation, but a map performs all key comparisons using its compareTo (or compare) method, so two keys that are

Sorting Java objects using multiple keys

馋奶兔 提交于 2019-11-26 10:25:53
I've got a collection of Duck objects and I'd like to sort them using multiple keys . class Duck { DuckAge age; //implements Comparable DuckWeight weight; //implements Comparable String name; } List<Duck> ducks = Pond.getDucks(); eg. I want to sort them primarily by their weights , and secondarily by their age . If two ducks have the exact same weight and the exact same age, then let's differentiate them using their names as a tertiary key . I might do something like this: Collections.sort(ducks, new Comparator<Duck>(){ @Override public int compare(Duck d1, Duck d2){ int weightCmp = d1.weight

How do I make 2 comparable methods in only one class?

百般思念 提交于 2019-11-26 09:25:52
问题 I\'ve got one class, that I sort it already by one attribute. Now I need to make another thing, that I need to create another way to sort my data. How can I make it, so I can choose between the two methods. The only command I know is Collections.sort that will pick up the method compareTo from the class I want to compare its data. Is it even possible? 回答1: What you need to do is implement a custom Comparator. And then use: Collections.sort(yourList, new CustomComparator<YourClass>());