I have two classes (Car and Bus) like below
with the same property (name)
Class 1
class Car {
public String name;
}
Class 2
class Bus {
public String name;
}
and i have an array of objects of the two classes
ArrayList<Object> vehicles = new ArrayList<>();
vehicles.add(new Car("Fiat"));
vehicles.add(new Car("Citroen"));
vehicles.add(new Bus("Ford"));
vehicles.add(new Bus("Toyota"));
How do I order the array by the name property alphabetically if they are 2 different classes?
CodePudding user response:
One option is to have the classes implement a common interface:
interface NamedVehicle {
String getName();
}
class Car implements NamedVehicle {
public String name;
@Override public String getName() { return name; }
}
class Bus implements NamedVehicle {
public String name;
@Override public String getName() { return name; }
}
And then store a list of interface references instead of Object
:
final List<NamedVehicle> vehicles = new ArrayList<>(Arrays.asList(
new Car("Fiat"),
new Car("Citroen"),
new Bus("Ford"),
new Bus("Toyota")
));
vehicles.sort(Comparator.comparing(NamedVehicle::getName));
The above would be the recommended option (keywords "program against an interface", "information hiding", "don't expose fields"). If you cannot introduce an interface, it is going to be more tricky and ugly, but it's doable.
final List<Object> vehicles = new ArrayList<>(Arrays.asList(
new Car("Fiat"),
new Car("Citroen"),
new Bus("Ford"),
new Bus("Toyota")
));
vehicles.sort(Comparator.comparing(o -> {
if (o instanceof Car) { return ((Car)o).name; }
if (o instanceof Bus) { return ((Bus)o).name; }
return ""; // you cannot guarantee that the list will only contain Buses and Cars (it is <Object>, after all), so you have to return some dummy value here or throw an exception.
}));
CodePudding user response:
class Car extends Vehicle {
public Car(String name) {
super(name);
}
}
class Bus extends Vehicle {
public Bus(String name) {
super(name);
}
}
class Vehicle {
public String name;
public Vehicle(String name) {
this.name = name;
}
public Vehicle() {
}
}
ArrayList<Vehicle> vehicles = new ArrayList<>();
vehicles.add(new Car("Fiat"));
vehicles.add(new Car("Citroen"));
vehicles.add(new Bus("Ford"));
vehicles.add(new Bus("Toyota"));
vehicles.sort(Comparator.comparing(vehicle -> vehicle.name));