comparable

Comparing Long values using Collections.sort(object)

放肆的年华 提交于 2019-12-01 02:28:18
I'm trying to sort a simple list of objects by a long - the below isn't working because one of the long strings is pushed to the top simply because it starts with a lower number. So I'm looking for a way to sort these by the actual long values directly The current obj implementation looks something like the below. In the class I'm using this I call Collections.sort(trees); public class Tree implements Comparable<Tree> { public String dist; //value is actually Long public int compareTo(Tree o) { return this.dist.compareTo(o.dist); } } why not actually store a long in there: public class Tree

Using compareTo and Collections.sort

微笑、不失礼 提交于 2019-11-30 23:16:39
I have a franchise class with owner(owner of franchise's name), state(2-character string for the state where the franchise is located), and sales (total sales for the day) public class Franchise implements Comparable <Franchise> { final String owner; final String state; final double sales; protected Franchise(String owner, String state, double sales ) { this.owner = owner; this.state = state; this.sales = sales; } public String toString() { String str = state + ", " + sales + ", " + owner; return str; } public String getState() { return state; } public double getSales() { return sales; }

Comparing Long values using Collections.sort(object)

有些话、适合烂在心里 提交于 2019-11-30 22:44:32
问题 I'm trying to sort a simple list of objects by a long - the below isn't working because one of the long strings is pushed to the top simply because it starts with a lower number. So I'm looking for a way to sort these by the actual long values directly The current obj implementation looks something like the below. In the class I'm using this I call Collections.sort(trees); public class Tree implements Comparable<Tree> { public String dist; //value is actually Long public int compareTo(Tree o)

What does “Mutually Comparable” mean?

佐手、 提交于 2019-11-30 20:36:18
问题 I read that : whenever a collection need to be sorted, the elements must be mutually comparable. I wrote the below code and it worked correctly. Can you please tell how class b and class c are mutually comparable and what is the meaning of being "mutually comparable"? import java.util.ArrayList; import java.util.Collections; class b implements Comparable<c> { String str1; b(String str1) { this.str1 = str1; } public int compareTo(c object) { return str1.compareTo(object.str1); } } class c

Using compareTo and Collections.sort

五迷三道 提交于 2019-11-30 18:33:50
问题 I have a franchise class with owner(owner of franchise's name), state(2-character string for the state where the franchise is located), and sales (total sales for the day) public class Franchise implements Comparable <Franchise> { final String owner; final String state; final double sales; protected Franchise(String owner, String state, double sales ) { this.owner = owner; this.state = state; this.sales = sales; } public String toString() { String str = state + ", " + sales + ", " + owner;

Java, how to use compareTo to sort an Arraylist

心已入冬 提交于 2019-11-30 18:13:02
问题 Im trying to figure out how to sort an ArrayList using comparable, my code looks like this: public class playerComparsion{ public static void main(String[] args){ ArrayList<Object> list = new ArrayList<Object>(); Player p1 = new Players(1,92,Zlatan); Player p2 = new Players(2,92,Hazard); Player p3 = new Players(1,82,Klose); list.add(p1); list.add(p2); list.add(p3); } } class Players implements Comparable{ int position; String name; int rating; public Players(int i, int j, String string) {

Generic class that conforms to Comparable in Swift

与世无争的帅哥 提交于 2019-11-30 13:50:00
I'm attempting to create a simple generic node class that conforms to the Comparable protocol so that I can easily compare nodes without accessing their key. When I attempt to write the < and == functions, however, the compiler doesn't seem to like it. The < and == functions expect a type when defining the Node parameters. This was simple in Java where you defined equality and < internally to the class. Swift asks for it globally. Any thoughts ? Example: func < (lhs:Node<E:Comparable>, rhs:Node<E:Comparable>) -> Bool { return lhs.key < rhs.key } func == (lhs:Node<E:Comparable>, rhs:Node<E

How to implement an interface with an enum, where the interface extends Comparable?

。_饼干妹妹 提交于 2019-11-30 12:35:26
Consider this code: public interface Foo extends Comparable<Foo> {} public enum FooImpl implements Foo {} Due to the restrictions of type erasure, I receive the following error: java.lang.Comparable cannot be inherited with different arguments: <Foo> and <FooImpl> I have the following requirements: FooImpl needs to be an enum, because I need to use it as a default value in annotations. The contract of my interface is that it needs to be comparable. I already tried using generic bounds in the interface, but this is not supported in Java. Enums implement Comparable, so FooImpl ends up extending

how can I implement Comparable more than once?

半城伤御伤魂 提交于 2019-11-30 11:44:39
I'm upgrading some code to Java 5 and am clearly not understanding something with Generics. I have other classes which implement Comparable once, which I've been able to implement. But now I've got a class which, due to inheritance, ends up trying to implement Comparable for 2 types. Here's my situation: I've got the following classes/interfaces: interface Foo extends Comparable<Foo> interface Bar extends Comparable<Bar> abstract class BarDescription implements Bar class FooBar extends BarDescription implements Foo With this, I get the error 'interface Comparable cannot be implemented more

can StringBuffer objects be keys in TreeSet in Java?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 09:47:32
问题 I have the following code where I am trying to put the StringBuffer objects as keys in a TreeSet. The reason I do this is to see if I can put mutable objects as keys. I do not get any compile error. but when I run this code, I get the error that is below the code. specially, I get this java.lang.StringBuffer cannot be cast to java.lang.Comparable . what does this error indicate? from javadoc I see that StringBuffer class is declared final ( public final class StringBuffer ), doesn't that mean