Skip to content

Instantly share code, notes, and snippets.

@kubawieczorek
Last active December 15, 2019 11:55
Show Gist options
  • Save kubawieczorek/7fbbe7788552420b6bfa4c53ad297d3e to your computer and use it in GitHub Desktop.
Save kubawieczorek/7fbbe7788552420b6bfa4c53ad297d3e to your computer and use it in GitHub Desktop.
class Car {
private String carType;
public Car(String type) {
this.carType = type;
}
public String getCarType() {
return carType;
}
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Car)) {
return false;
}
Car car = (Car) o;
return car.carType.equals(carType);
}
@Override
public int hashCode() {
return Objects.hash(getCarType());
}
}
public class Main {
public static void main(String[] args) {
Map<String, Car> ownerToCar = new HashMap<>();
ownerToCar.put("JohnDoe", new Car("Fiat"));
ownerToCar.put("SuzanneThomson", new Car("Ferrari"));
System.out.print(ownerToCar.get("SuzanneThomson").getCarType()); // "Ferrari"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment