As I see you have wrong implementation of compare method. Could you update it to?
@Override
public int compareTo(User user) {
return Integer.compare(age, user.age);
}
Answer from Roma Khomyshyn on Stack OverflowAs I see you have wrong implementation of compare method. Could you update it to?
@Override
public int compareTo(User user) {
return Integer.compare(age, user.age);
}
What you're doing with the TreeSet is unnecessary. I'm not sure they're guaranteed to have certain ordering when iterated.
Just replace your sort method with
Collections.sort(list)
And my guess as to why an element is being dropped is your compareTo method never returns a 1 in any case, so elements are always considered to be less than or equal to other elements, which is probably screwing with the TreeSet.
Videos
Help with overriding compareTo()
comparison - Tricky compareTo, inheritance, easy to extend - Java - Software Engineering Stack Exchange
How to override compare method in Comparator
should i override the compareTo() method?
Hello my good people, this is my first Java program ever and I ran into an issue I don't know how to fix, even after a bunch of research. So I'm trying to override the compareTo method, so I can sort a list of objects by name, but when I put that list into Collections.sort, it shows an error saying "no instance(s) of type variable(s) T exist so that MyClass conforms to Comparable<? super T>" Here's the relevant code, I can post more if you want:
Comparable interface:
public interface Comparable<Jed> {
int compareTo(Jed jed);
}Class Jed, that implements Comparable:
public class Jed implements Comparable<Jed> {
String ime;
@Override
public int compareTo(Jed jed){
return this.ime.compareTo(jed.getIme());
}
}The class which holds the list that I want sorted:
public class Racun {
private List<Jed> seznamJedi;
public void sort(){
Collections.sort(seznamJedi);
} I hope I didn't miss anything relevant, but yeah, any help would be greatly appreciated and maybe an explanation on why this is happening.
You could write a general compareTo method in class A, which uses a key computed by a method compareKey which is also in A, but can be overridden by each subclass to make as fine a distinction as it wants. If you make it return a float, you can keep subdividing the comparison criterion whenever a new level of inheriting classes is added (within reason, obviously).
I think what you want is sort of multiple dispatch which is ususally implemented in Java using the Visitor-Pattern but you would have to change the code when adding new classes.
Since you don't really need full-blown multiple dispatch I think the following might be a good solution. Write your compareTo methods like this: (This is the one in TestB)
@Override
public int compareTo(TestA o) {
if(o instanceof TestB) {
// add your code instead
System.out.println("Compare Bs");
return 0;
} else {
return super.compareTo(o);
}
}
Since the call to x.compareTo is resolved at run-time you always run the compareTo function of the most specific class. By checking if the other argument is instanceof the current class you can detect when you have to move up the hierarchy which you can do calling super.compareTo.
Let me know if you have any further questions. :)
Edit: I assumed what you wanted to do was compare as the most specific common ancestor as per your comment on the original question.
i don't know if i should write it as compareTO(Object UNP) or compareTo(RentalCars UNP) also am i using the equals() method correctly or am i missing something?
/*
Class Car(implements the interface: Comparable<> )
- Attributes:
o Unique number plate (final) : String
o Brand: String
o Rental rate: double
o Extra three (from your choice)
- Methods:
o Constructors: zero-arg and multi-arg
o getters
o setters (if needed)
o toString()
o equals(): compare plate numbers
o compareTo(): compare plate numbers
*/
import java.io.*;
import java.util.*;
// i know i should change it from Rentalcars to RentalCar
public class RentalCars implements Comparable<RentalCars> {
//UNP is the unique plat numbers
final String UNP;
private String brandName;
private double rentalRates;
private int wheelDrive;
private String color;
private int milage;
public RentalCars(){
this(null,null, 0.0, 0, null, 0);
}
public RentalCars(String UNP, String brandName, double rentalRates, int wheelDrive,
String color, int milage){
this.UNP = UNP;
this.brandName=brandName;
this.rentalRates=rental_Rates;
this.wheelDrive=wheel_Drive;
this.color=color;
this.milage=milage;
}
public String getUNP() {
return UNP;
}
public String getbrandName() {
return brandName;
}
public void setbrandname(String brandName) {
this.brandName=brandName;
}
public double getrentalRates() {
return rentalRates;
}
public void setrental_Rates(double rentalRates) {
this.rentalRates=rentalRates;
}
public int getwheelDrive() {
return wheelDrive;
}
public void setwheel_Drive(int wheelDrive) {
this.wheelDrive=wheelDrive;
}
public String getcolor() {
return color;
}
public void setcolor(String color) {
this.color=color;
}
public int getmilage() {
return milage;
}
public void setmilage(int milage) {
this.milage=milage;
}
@Override
public String toString()
{
return "the Number Plate of the car is "+UNP+"the Car brand is "+brandName+
"the rent rate of this car is "+rental_Rates+"the wheel drive is "+
wheel_Drive+"the color of the car is "+color+"the milage is "+milage;
}
public boolean equals(RentalCars obj){
RentalCars rc = (RentalCars) obj;
if(UNP != rc.UNP)
return false;
return true;
}
public int compareTo(RentalCars UNP){
}
}