Sanny wrote
I was going through TreeMaps and HashMaps Looks like they are same
thing.
Basically I want them to sort a data and then resort them when a value
is changed.
import java.util.Comparator;
public class CarComparisons {
public static void main(String[] args) {
Car car1 = new Car();
//What is the value of car1?
boolean comparison1 = (car1 == null);
//What is the value of comparison1?
boolean comparison2 = (car1 == car1);
//What is the value of comparison2?
boolean comparison3 = (car1.equals(null));
//What is the value of comparison3?
boolean comparison4 = (car1.equals(car1));
//What is the value of comparison4?
IntegerCar car2 =
new IntegerCar(Integer.valueOf(1));
//What is the value of car2?
car2.setNumber(Integer.valueOf(2));
//What is the value of car3?
IntegerCar car3 =
new IntegerCar(Integer.valueOf(2));
// What is the value of car3
boolean comparison5 = car2.equals(car3);
//What is the value of comparison4?
int comparison6 =
car2.getNumber().compareTo(car3.getNumber());
// What is the value of comparison6?
IntegerValueComparableCar car5 =
new IntegerValueComparableCar(Integer.valueOf(1));
IntegerValueComparableCar car6 =
new IntegerValueComparableCar(Integer.valueOf(2));
int comparison9 = (car5.compareTo(car6));
// What is the value of comparison9?
car5.setNumber(Integer.valueOf(2));
int comparison10 = (car5.compareTo(car6));
// What is the value of comparison10?
TwoValueCar car7 =
new TwoValueCar(Integer.valueOf(1), "chevrolet");
TwoValueCar car8 =
new TwoValueCar(Integer.valueOf(2), "ford");
NumberComparator numberComparator =
new NumberComparator();
NameComparator nameComparator =
new NameComparator();
NumberNameComparator numberNameComparator =
new NumberNameComparator(false);
TwoValueCar[] carArray = {car7, car8};
// now it's play time
// make some Sets, Lists, Maps
// add, remove, iterate, compare, find, etc...
}
public static class Car
extends Object {
}
public static class IntegerCar
extends Car {
private Integer number;
public IntegerCar(Integer number) {
this.number = number;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
}
public static class IntegerValueComparableCar
extends IntegerCar
implements Comparable<IntegerCar> {
public IntegerValueComparableCar(Integer number) {
super(number);
}
@Override
public int compareTo(IntegerCar that) {
return super.number.compareTo(that.number);
}
}
public static class TwoValueCar
extends IntegerCar {
String name;
public TwoValueCar(Integer number) {
super(number);
this.name = "chevrolet";
}
public TwoValueCar(Integer number, String name) {
super(number);
this.name = name;
}
public Integer getNumber() {
return super.number;
}
public String getName() {
return name;
}
public void setNumber(Integer number) {
super.number = number;
}
public void setName(String name) {
this.name = name;
}
}
public static class NumberComparator
implements Comparator<TwoValueCar> {
@Override
public int compare(TwoValueCar car0, TwoValueCar car1) {
return car0.getNumber().compareTo(car1.getNumber());
}
}
public static class NameComparator
implements Comparator<TwoValueCar> {
@Override
public int compare(TwoValueCar car0, TwoValueCar car1) {
return car0.getName().compareTo(car1.getName());
}
}
public static class NumberNameComparator
implements Comparator<TwoValueCar> {
private final boolean compareByNumber;
public NumberNameComparator(boolean compareByNumber) {
this.compareByNumber = compareByNumber;
}
@Override
public int compare(TwoValueCar car0, TwoValueCar car1) {
if (compareByNumber) {
return car0.getNumber().compareTo(car1.getNumber());
}
else {
return car0.name.compareToIgnoreCase(car1.name);
}
}
}
public static class CarDriver {
public final String height;
public CarDriver(String height) {
this.height = height;
}
}
}