Last active
December 15, 2019 11:55
-
-
Save kubawieczorek/7fbbe7788552420b6bfa4c53ad297d3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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