comparable

how to compare on multiple classes in java?

感情迁移 提交于 2019-12-23 17:31:10
问题 Right now, I have written at comparator that sorts an array of Integers and Strings. As you can see from the code, if the two classes aren't the same, then the String class takes the greater than value. However, this only allows for two classes. What if I want to add another primitive type to my array, such as Float? I'd have to add more code to to if-else statement. Is there a way to implement compare without having to add a statement for each additional class I want to compare? import java

Java: Integer obj can't cast to Comparable

喜你入骨 提交于 2019-12-23 17:07:37
问题 I'm having problems trying to pass an Integer object from a driver class as an argument for function of a SortedArray Generic class I created. From my driver class, I convert the user's int input into an Integer object to be cast onto Comparable of my SortedArray class. I continue to receive the error: "Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to Comparable". I took a look at some of my classmates' source codes only to find little difference in

Min / Max function with two Comparable

与世无争的帅哥 提交于 2019-12-23 10:59:19
问题 I've got the following snippet: Comparable<C> a = ...; Comparable<C> b = ...; Comparable<C> min = a.compareTo(b) <= 0 ? a : b; This is similar to Math.min(a, b) , but defined on Comparable . I know that the ternary operator is already quite short, but I can't inline the expressions for a and b and I think that min(a, b) resp. max(a, b) is easier to understand. I know that there are several stream resp. collection functions for a set of values like: Stream.of(a, b).min(Comparator.naturalOrder(

Java Syntax for a list of comparable objects

三世轮回 提交于 2019-12-22 05:20:49
问题 I'm writing a method that takes as its only parameter a list of comparable objects and doesn't return anything. I am uncertain as to the syntax that it should have: public static void methodName(List<Comparable<Object>> list) { // Do some stuff } I think this is wrong because of the <Object> as a type for Comparable, which would mean the list can take an Integer and a Boolean as objects, but I don't want that. I want the list to take only one type, but that type has to implement the

Cannot invoke compareTo(double) on the primitive type double

不羁的心 提交于 2019-12-22 04:18:27
问题 The line return array[index1].compareTo(array[index2]); provides an error "Cannot invoke compareTo(double) on the primitive type double" . How to solve this issue? /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ /*:: This function implements a comparator of double values :*/ /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ private class ArrayIndexComparator implements Comparator<Integer> { private final double[] array; public ArrayIndexComparator

How to create SortedSet (e.g. TreeSet) for elements of type BitSet

浪尽此生 提交于 2019-12-21 05:01:05
问题 I have a number (power(2,k)) of BitSet objects and I want to store them in a SortedSet . I use the code: Set <BitSet> S= new TreeSet<>(); However, I am getting this error: java.lang.ClassCastException: java.util.BitSet cannot be cast to java.lang.Comparable How do I implement comparable interface? Or is there any other way to sort these elements of type BitSet ? 回答1: There are two ways to use a TreeSet . Have it contain objects that implement Comparable Have a custom Comparator object that

Using Comparable

空扰寡人 提交于 2019-12-20 06:56:05
问题 Looking for assistance for following issue running into have following comparable method below that want to print largest value of goals scored from my Hockey Player ArrayList. I'm unable to store values from the for each loop successfully to pass it to the comparable method. Any suggestion what I'm doing wrong? public static Comparable maximum(Comparable first, Comparable second, Comparable third) { if (first.compareTo(second) > 0) { if (first.compareTo(third) > 0) { return first; } return

Using Comparable

断了今生、忘了曾经 提交于 2019-12-20 06:52:03
问题 Looking for assistance for following issue running into have following comparable method below that want to print largest value of goals scored from my Hockey Player ArrayList. I'm unable to store values from the for each loop successfully to pass it to the comparable method. Any suggestion what I'm doing wrong? public static Comparable maximum(Comparable first, Comparable second, Comparable third) { if (first.compareTo(second) > 0) { if (first.compareTo(third) > 0) { return first; } return

equals method contract with comparable interface

自作多情 提交于 2019-12-20 05:43:08
问题 I have a custom class like Person: public class Person { int age; String name; } Now I want to sort Person class objects based on age . So I will use Comparable interface and implement compareTo() method. And compareTo will have logic to compare person object based on just age . So if I do : Collections.sort(list); // where list is a list of person I will get sorted person list based on age . But I read somewhere, we need to override equals() method as well when we do Comparable

Why isn't Collections.binarySearch() working with this comparable?

馋奶兔 提交于 2019-12-20 03:53:23
问题 I have this Player class which implements the Comparable interface. Then I have an ArrayList of Player s. I'm trying to use binarySearch() on the list of Player s to find one Player , but Java is giving me a " cannot find symbol: method binarySearch(java.util.ArrayList< Player>,Player) ". This the Player class: class Player implements Comparable { private String username; private String password; Statistics stats; //Constructor, creates a new Player with a supplied username Player(String name