Collections method sort with a custom Comparator object. - Java Collection Framework

Java examples for Collection Framework:Collections Utility Methods

Description

Collections method sort with a custom Comparator object.

Demo Code

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    List<Time> list = new ArrayList<>(); // create List

    list.add(new Time(6, 24, 34));
    list.add(new Time(18, 14, 58));
    list.add(new Time(16, 15, 34));
    list.add(new Time(12, 14, 58));
    list.add(new Time(6, 24, 22));

    // output List elements
    System.out.printf("Unsorted array elements:%n%s%n", list);

    // sort in order using a comparator
    Collections.sort(list, new TimeComparator());

    // output List elements
    System.out.printf("Sorted list elements:%n%s%n", list);
  }//from  ww w .j  a  v a 2 s.  co m
}

class Time {
  private int hour; // 0 - 23
  private int minute; // 0 - 59
  private int second; // 0 - 59

  // Time2 no-argument constructor:
  // initializes each instance variable to zero
  public Time() {
    this(0, 0, 0); // invoke Time2 constructor with three arguments
  }

  // Time2 constructor: hour supplied, minute and second defaulted to 0
  public Time(int h) {
    this(h, 0, 0); // invoke Time2 constructor with three arguments
  }

  // Time2 constructor: hour and minute supplied, second defaulted to 0
  public Time(int h, int m) {
    this(h, m, 0); // invoke Time2 constructor with three arguments
  }

  // Time2 constructor: hour, minute and second supplied
  public Time(int h, int m, int s) {
    setTime(h, m, s); // invoke setTime to validate time
  }

  // Time2 constructor: another Time2 object supplied
  public Time(Time time) {
    // invoke Time2 three-argument constructor
    this(time.getHour(), time.getMinute(), time.getSecond());
  }

  // Set Methods
  // set a new time value using universal time;
  // validate the data
  public void setTime(int h, int m, int s) {
    setHour(h); // set the hour
    setMinute(m); // set the minute
    setSecond(s); // set the second
  }

  // validate and set hour
  public void setHour(int h) {
    if (h >= 0 && h < 24)
      hour = h;
    else
      throw new IllegalArgumentException("hour must be 0-23");
  }

  // validate and set minute
  public void setMinute(int m) {
    if (m >= 0 && m < 60)
      minute = m;
    else
      throw new IllegalArgumentException("minute must be 0-59");
  }

  // validate and set second
  public void setSecond(int s) {
    if (s >= 0 && s < 60)
      second = ((s >= 0 && s < 60) ? s : 0);
    else
      throw new IllegalArgumentException("second must be 0-59");
  }

  // get hour value
  public int getHour() {
    return hour;
  }

  // get minute value
  public int getMinute() {
    return minute;
  }

  // get second value
  public int getSecond() {
    return second;
  }

  // convert to String in universal-time format (HH:MM:SS)
  public String toUniversalString() {
    return String.format("%02d:%02d:%02d", getHour(), getMinute(), getSecond());
  }

  // convert to String in standard-time format (H:MM:SS AM or PM)
  public String toString() {
    return String.format("%d:%02d:%02d %s",
        ((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12),
        getMinute(), getSecond(), (getHour() < 12 ? "AM" : "PM"));
  }
}

class TimeComparator implements java.util.Comparator<Time> {
  @Override
  public int compare(Time time1, Time time2) {
    int hourDifference = time1.getHour() - time2.getHour();

    if (hourDifference != 0) // test the hour first
      return hourDifference;

    int minuteDifference = time1.getMinute() - time2.getMinute();

    if (minuteDifference != 0) // then test the minute
      return minuteDifference;

    int secondDifference = time1.getSecond() - time2.getSecond();
    return secondDifference;
  }
}

Result


Related Tutorials