Solved trying to play around with Comparators and Comparables

Discussion in 'Plugin Development' started by xize, Nov 6, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    xize

    Hello,

    so since I'm playing a bit around with the Comparator and Comparables in my Object wrapper I wonder if this would affect while iterating over List or LinkedList with a Iterator, this may sound a bit confusing so lets try to explain it throughout the code.

    this is how my wrapper is now of course I need todo more things than just this but Im trying to prototype a bit:

    Code:
    public class DataWrapper implements Comparator<Date>, Comparable<Date> {
        
        private final HashMap<Location, String> locations;
        private final Date[] dates;
        
        public DataWrapper(HashMap<Location, String> locations, Date[] dates) {
            this.locations = locations;
            this.dates = dates;
        }
        
        public boolean isDateNow() {
            Date date = new Date(System.currentTimeMillis());
            SimpleDateFormat fmt = new SimpleDateFormat("ddMMyyyy");
            for(Date d : dates) {
                if(fmt.format(date).equals(fmt.format(d))) {
                    return true;
                }
            }
            return false;
        }
        
        private boolean isDateNow(Date date) {
            SimpleDateFormat fmt = new SimpleDateFormat("ddMMyyyy");
            for(Date d : dates) {
                if(fmt.format(date).equals(fmt.format(d))) {
                    return true;
                }
            }
            return false;
        }
        
        public void sentBlocks() {
            
        }
        
        public void unsetBlocks() {
            
        }
        @Override
        public int compare(Date o1, Date o2) {
            return o1.compareTo(o2);
        }
        @Override
        public int compareTo(Date o) {
            if(isDateNow(o)) {
                return 0;
            }
            return 1;
        }
    }
    
    now my intention or theory is, is that the compare method of the Comparable implementation will return 1 when its not the date so I geuss that the order will change and the ones with 0 get a higher priority in the List or LinkedList (perhaps I got this wrong in my mind and is 0 lower priority and 1 higher but thats not the point).

    basicly my intention is that I want to make a automated Iterator based on the date, however Im not sure once when the Iterator is instanced, that (if) it is possible to the order to automaticly updates it self (as in a modulair way rather than stacking it up and forgetting about it) I know a LinkedList can also Iterate back and from there it is possible to re-iterate but Im absolutely not sure about the technical part of this, I hope someone can help me:)

    thanks.
     
  2. Offline

    Totom3

    It all depends on the List implementation you are using. In the case of an ArrayList and LinkedList, it doesn't matter which element can be considered greater because these implementations do not sort their elements depending on that.

    If you want a Collection in which the elements are sorted depending on the return value of compareTo(), then TreeSet is what you are looking for. It sorts the elements for you and does not allow duplicates.
     
    xize and xTrollxDudex like this.
  3. Offline

    xize

    Totom3 aha that clears alot of confusing for me, very informative thanks:)
     
Thread Status:
Not open for further replies.

Share This Page