@@ -0,0 +1,2622 @@
+package org.luyue.examples.java.basics;%0A%0Aimport java.util.Arrays;%0Aimport java.util.Collections;%0Aimport java.util.Comparator;%0Aimport java.util.List;%0A%0Apublic class CompareObjects %7B%0A%0A    public static void main(String%5B%5D args) %7B%0A%0A        Car audiQ72014 = new Car(%22Audi%22, %22Q7%22, 2014);%0A        Car audiQ52015 = new Car(%22Audi%22, %22Q5%22, 2015);%0A        Car fordFocus2015 = new Car(%22Ford%22, %22Focus%22, 2015);%0A%0A        List%3CCar%3E cars = Arrays.asList(audiQ72014, audiQ52015, fordFocus2015);%0A%0A        Collections.sort(cars);%0A        print(%22sort by year%22, cars);%0A%0A        Collections.sort(cars, new MakerComparator());%0A        print(%22sort by maker%22, cars);%0A%0A        Collections.sort(cars, new ModelComparator());%0A        print(%22sort by model%22, cars);%0A    %7D%0A%0A    private static void print(String title, List%3CCar%3E cars) %7B%0A        System.out.println(%22************ %22 + title + %22 ****************%22);%0A        for (Car car : cars) %7B%0A            System.out.println(car);%0A        %7D%0A    %7D%0A%7D%0A%0Aclass Car implements Comparable%3CCar%3E %7B%0A%0A    private final String maker;%0A    private final String model;%0A    private final int year;%0A%0A    public String getMaker() %7B%0A        return maker;%0A    %7D%0A%0A    public String getModel() %7B%0A        return model;%0A    %7D%0A%0A    public int getYear() %7B%0A        return year;%0A    %7D%0A%0A    Car(String maker, String model, int year) %7B%0A        this.maker = maker;%0A        this.model = model;%0A        this.year = year;%0A    %7D%0A%0A    @Override%0A    public int compareTo(Car anotherCar) %7B%0A        if (this.year %3E anotherCar.year)%0A            return 1;%0A        else if (this.year %3C anotherCar.year)%0A            return -1;%0A        return 0;%0A    %7D%0A%0A    @Override%0A    public int hashCode() %7B%0A        final int prime = 31;%0A        int result = 1;%0A        result = prime * result + year;%0A        return result;%0A    %7D%0A%0A    @Override%0A    public boolean equals(Object obj) %7B%0A        if (this == obj)%0A            return true;%0A        if (obj == null)%0A            return false;%0A        if (getClass() != obj.getClass())%0A            return false;%0A        Car other = (Car) obj;%0A        if (year != other.year)%0A            return false;%0A        return true;%0A    %7D%0A%0A    @Override%0A    public String toString() %7B%0A        return %22Car %5Bmaker=%22 + maker + %22, model=%22 + model + %22, year=%22 + year + %22%5D%22;%0A    %7D%0A%7D%0A%0Aclass MakerComparator implements Comparator%3CCar%3E %7B%0A%0A    @Override%0A    public int compare(Car car1, Car car2) %7B%0A        return car1.getMaker().compareTo(car2.getMaker());%0A    %7D%0A%7D%0A%0Aclass ModelComparator implements Comparator%3CCar%3E %7B%0A%0A    @Override%0A    public int compare(Car car1, Car car2) %7B%0A        return car1.getModel().compareTo(car2.getModel());%0A    %7D%0A%7D

add comparable and comparator examples