How to compare classes and inherited classes in Java

I have two classes - Task (which implements Comparable) and DeadlinedTask (where DeadlinedTask extends the task). And for each of them I wrote an overloaded compareTo function (each has compareTo (Task) and compareTo (DeadlinedTask)).

The idea is that I can sort regular tasks by category and DeadlinedTasks by time, but I also want all DeadlinedTasks files to be sorted over tasks.

When I call Collections.sort (myListOfTasks) in the list of only Tasks (no DeadlinedTasks), everything works like a charm. However, when I have a list of Tasks and DeadlinedTasks tasks, the order in which the objects change, but they are not completely sorted.

I tried returning numbers other than 1 on interclass comparisons (1, 1000, 1,000,000 all did the same). Is there a way to do this with compareTo and Collections.sort, is there any other java functionality that I can use, or do I need to write my own search function (as a comparator?)?

Task compareTo Methods:

public int compareTo(Task other){
    if(this.GetCategory().compareTo(other.GetCategory())==0)
        return this.GetName().compareTo(other.GetName());
    else 
        return this.GetCategory().compareTo(other.GetCategory());
}
public int compareTo(DeadlinedTask other){
    return 1;
}

DeadlinedTask compareTo Methods:

public int compareTo(Task other){
    return -1;
}
public int compareTo(DeadlinedTask other){
    if(this.GetDeadline().compareTo(other.GetDeadline())==0)
        return this.GetName().compareTo(other.GetName());
    else 
        return this.GetDeadline().compareTo(other.GetDeadline());
}

Thanks for any help

+3
source share
5 answers

... or do I need to write my own search function (as a comparator?)?

Yes. I think the best way.

equals compareTo false ( equals) throw ClassCastException ( compareTo), this.

equals compareTo , , :

  • a.equals(b) b.equals(a), ,
  • a.compareTo(b) b.compareTo(a) .

. , .

, , , Comparator.

+5

Comparable compareTo. Comparable generics,

public int compareTo(Object o)

, . Comparable<Task>,

public int compareTo(Task o)

compareTo(DeadlinedTask o) Comparable<Task>. "" , overloading.

(, Comparable<Task>, Comparable<DeadlineTask>).

, , Task.compareTo(Task o) instanceof ( ). , .

+2

, StevenC, , , , compareTo() , , :

public boolean compareTo(Object o) {
  // check for null
  boolean isSubtype = getClass().isAssignableFrom(o.getClass()) && getClass()!=o.getClass()
  if (isSubtype) return -((/*cast to this type*/) o).compareTo(this);
}

, , pf , .

+1

Comparable . , DeadlinedTask , compareTo .

compareTo DeadlinedTask, : if (t1.compareTo(t2) > 0), t2.compareTo(t1) < 0.

, Comparable Task . , Comparable, , DeadlinedTask ( OO):

public class Task implements Comparable<Task> {
    // ...
    public final int compareTo(Task t) {
        if (this instanceof DeadlinedTask) {
            if (t instanceof DeadlinedTask) {
                return ((DeadlinedTask) this).getDeadline().compareTo(((DeadlinedTask) t).getDeadline());
            }
            else {
                return -1;
            }
        }
        else if (t instanceof DeadlinedTask) {
            return 1;
        }
        else {
            return this.category.compareTo(t.category);
        }
    }
}

, Java (getDeadline(), getDeadline()) .

0

, , - ( ) te bulk compareTo (...), , , , sub :

public int Compare(Task t1, Task t2) {

    if (t1 instance of DeadlinedTask && !(t2 instanceof DeadlinedTask))
        return 1;
    else if (t2 instance of DeadlinedTask && !(t1 instanceof DeadlinedTask))
        return -1;
    else
        return t1.compareTo(t2);
}

, ? "" "" ? , , , lhs , (Task) ?? implements, ..

class Task implements Comparable<Task>, Comparable<DeadlinedTask>

, .. 1 1000000 , < 0, > 0 == 0 ( IS Comparator. , , , , ints, :

int compare (int a, int b) { return a - b; }
0

Source: https://habr.com/ru/post/1791815/


All Articles