سؤال

I have two classes:

First implements Serializable {
    int month;
    int day;
    int hour;
    int minute;
}

Second implements Serializable, Comparable<?> {
    First object;
    int temperature;
}

I had to write objects of Second class to binary file. I know they need to implements Serializable interface to do so and I have no problem with that. The problem is that I have to create a version of this code that will make it possible to add new object of Second class to collection chronogically by date saved in First class. So I need to compare Integers (month, day, hour, minute). I know that I have to declare method CompareTo in Second class. If I had to compare Strings it would be something like (if minute was String):

public int compareTO(Second object) {
    return minute.compareTo(object.object.minute); 
}

and it's done. But what should I do with those Integers? And what should I write in a place of "?" in the code?

هل كانت مفيدة؟

المحلول 2

I assume that the actual question here is about how you should handle the multiple int values. You can simply do a lexicographic comparison:

public static int compareByTime(Second s0, Second s1)
{
    First f0 = s0.object;
    First f1 = s1.object;
    if (f0.month > f1.month) return 1;
    if (f0.month < f1.month) return -1;
    if (f0.day > f1.day) return 1;
    if (f0.day < f1.day) return -1;
    if (f0.hour > f1.hour) return 1;
    if (f0.hour < f1.hour) return -1;
    if (f0.minute > f1.minute) return 1;
    if (f0.minute < f1.minute) return -1;
    return 0;
}

In general, I'd recommend to not let a class implement Comparable. You should do this only if the class has a natural ordering. And for your Second class, this is certainly not the case. Using a Comparator is much more flexible in most cases. However, this method can be used either for implementing Comparable or for implementing a Comparator.


EDIT: Elaborating it a little in response to the comment:

This method is intended for the general comparison of two Second objects. You can either use it when you let your Second class implement the Comparable interface:

Second implements Serializable, Comparable<Second> 
{
    First object;
    int temperature;

    @Override
    public int compareTo(Second other)
    {
        return compareByTime(this, other); // The method from above 
    }
}

Or you can use it when you create a Comparator for Second objects:

class SecondComparator implements Comparator<Second>
{
    @Override
    public int compare(Second s0, Second s1)
    {
        return compareByTime(s0, s1); // The method from above 
    }
}

In both cases, you can insert new objects into a collection like a TreeSet<Second>, which is always sorted. Alternatively, if you used a List<Second>, you could, for example, do a binary search for the index of where to insert the object. (Or simply add the new object to the list, and sort the list afterwards, but this would be rather inefficient)

نصائح أخرى

Your class needs to be defined

class Second implements Serializable, Comparable<Second> {
      public int compareTo(Second other) {

In Java 7 you can use

return Integer.compare(minute, object.minute);

or for multiple fields you can do

int cmp = Integer.compare(month, other.month);
if (cmp != 0) return cmp;
cmp = Integer.compare(days, other.days);
if (cmp != 0) return cmp;
cmp = Integer.compare(hours, other.hours);
if (cmp != 0) return cmp;
return Integer.compare(minutes, other.minutes);

For earlier Java systems

return Double.compare(minute, object.minute);

Or

return minute > object.minute ? +1 : minute < object.minute ? -1 : 0;

In this case, if you know the range of your integers is less than 2 billion i.e. you can't get an overflow, you can use subtraction

return minute - object.minute; // if you know overflow is not possible.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top