comparable

Sorting a list of objects based on different data members in java

一个人想着一个人 提交于 2020-01-24 09:32:26
问题 I have this class: public class Friend { private String name; private String location; private String temp; private String humidity; public String getTemp() { return temp; } public void setTemp(String temp) { this.temp = temp; } public String getHumidity() { return humidity; } public void setHumidity(String humidity) { this.humidity = humidity; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLocation() { return location; }

Sorting a list of objects based on different data members in java

微笑、不失礼 提交于 2020-01-24 09:32:26
问题 I have this class: public class Friend { private String name; private String location; private String temp; private String humidity; public String getTemp() { return temp; } public void setTemp(String temp) { this.temp = temp; } public String getHumidity() { return humidity; } public void setHumidity(String humidity) { this.humidity = humidity; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLocation() { return location; }

Implement comparable with 2 Enums

冷暖自知 提交于 2020-01-17 09:11:11
问题 I'm creating a Java program that simulates a game of Hearts. I have a Card class that I created for another card game simulation: public abstract class Card implements Comparable<Card>{ public enum Suits { SPADES, CLUBS, HEARTS, DIAMONDS } public enum Values { TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE, } private Suits suit; private Values value; public Card(final Values value, final Suits suit) { this.value = value; this.suit = suit; } public Values getValue

Java: Sort a Collection using a CollatorKey

风流意气都作罢 提交于 2020-01-12 07:48:09
问题 what I would like to achieve is to sort a colletion of objects by a string value. However in a locale dependant way using a collator. Due to performance reasons I do not want to use the Collator compare() method (as below in the code) rather the CollationKey class, as the java API states the using a CollationKey is much faster. But how do I implement the compareTo() method using the CollationKey? As far as I understood it, I have to completely write all the comparison Methods on my own if I

How to use the Comparable CompareTo on Strings in Java

旧时模样 提交于 2020-01-10 09:24:55
问题 I can use it to sort by emp id but I'm not sure if it is possible to compare strings. I get an error the operator is undefined for strings. public int compareTo(Emp i) { if (this.getName() == ((Emp ) i).getName()) return 0; else if ((this.getName()) > ((Emp ) i).getName()) return 1; else return -1; 回答1: What you need to use is the compareTo() method of Strings. return this.getName().compareTo(i.getName()); That should do what you want. Usually when implementing the Comparable interface, you

Java “unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable”

社会主义新天地 提交于 2020-01-01 01:52:34
问题 I'm trying to implement a sorted list as a simple exercise in Java. To make it generic I have an add(Comparable obj) so I can use it with any class that implements the Comparable interface. But, when I use obj.compareTo(...) anywhere in the code I get "unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable" from the compiler (with -Xlint:unchecked option). The code works just fine but I can't figure out how to get rid of that annoying message. Any hints? 回答1: In

How to efficiently compare Sets? [duplicate]

一个人想着一个人 提交于 2019-12-31 02:30:31
问题 This question already has answers here : What is the fastest way to compare two sets in Java? (9 answers) Closed 3 years ago . Given two Sets: how to compare them efficiently in Java? (a) keep them as List s, sort them and compare them. ( Comparable ) (b) keep them as Set s and compare the hashCode of the Sets? background: many comparisons need to be done Sets are small (usually < 5 elements per set). 回答1: The proper way to compare two sets is to use the equals method. I would not worry about

Should Comparable ever compare to another type?

天涯浪子 提交于 2019-12-30 00:39:09
问题 I'm wondering if there's ever a valid use case for the following: class Base {} class A implements Comparable<Base> { //... } It seems to be a common pattern (see Collections for a number of examples) to accept a collection of type T , where T extends Comparable<? super T> . But it seems technically impossible to fulfill the contract of compareTo() when comparing to a base class, because there's no way to ensure that another class doesn't extend the base with a contradictory comparison.

Java, Why it is implied that objects are equal if compareTo() returns 0?

大城市里の小女人 提交于 2019-12-29 06:38:30
问题 Let's have a class Person . Person has a name and height. Equals and hashCode() takes into account only name. Person is comparable (or we implement comparator for it, does not matter which one). Persons are compared by height. It seems reasonable to expect a situation where two different persons can have same height, but eg. TreeSet behaves like comapareTo()==0 means equals, not merely same size. To avoid this, comparison can secondarily look at something else if size is the same, but then it

Java, Why it is implied that objects are equal if compareTo() returns 0?

女生的网名这么多〃 提交于 2019-12-29 06:38:08
问题 Let's have a class Person . Person has a name and height. Equals and hashCode() takes into account only name. Person is comparable (or we implement comparator for it, does not matter which one). Persons are compared by height. It seems reasonable to expect a situation where two different persons can have same height, but eg. TreeSet behaves like comapareTo()==0 means equals, not merely same size. To avoid this, comparison can secondarily look at something else if size is the same, but then it