Java 17 Recipes
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
5-11. Comparing Objects
Problem Your application requires comparing two or more objects to see whether they are the same. Solution 1 To determine whether the two object references point to the same object, use the == and != operators. The following solution demonstrates the comparison of two object references to determine whether they refer to the same object. public static void main(String[] args){ // Compare if two objects contain the same values Team team1 = new Team(); Team team2 = new Team(); team1.setName("Jokers"); team1.setCity("Crazyville"); team2.setName("Jokers"); team2.setCity("Crazyville"); if (team1 == team2){ System.out.println("These object references refer to the same object."); } else { System.out.println("These object references do NOT refer to the same object."); } // Compare two objects to see if they refer to the same object Team team3 = team1; Team team4 = team1; if (team3 == team4){ System.out.println("These object references refer to the same object."); Chapter 5 ObjeCt-Oriented java 188 } else { System.out.println("These object references do NOT refer to the same object."); } } The following are the results of running the code. These object references do NOT refer to the same object. These object references refer to the same object. Solution 2 To determine whether the two objects contain the same values, use the equals() method. The object being compared must implement equals() and hashCode()for this solution to work properly. The following is the code for the Team class that overrides these two methods. public class Team implements TeamType, Cloneable { private List players; private String name; private String city; // Used by the hashCode method for performance reasons private volatile int cachedHashCode = 0; /** * @return the players */ public List getPlayers() { return players; } /** * @param players the players to set */ public void setPlayers(List players) { this.players = players; } Chapter 5 ObjeCt-Oriented java 189 /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ @Override public void setName(String name) { this.name = name; } /** * @return the city */ public String getCity() { return city; } /** * @param city the city to set */ @Override public void setCity(String city) { this.city = city; } public String getFullName() { return this.name + " - " + this.city; } /** * Overrides Object's clone method * * @return */ public Object clone() { Chapter 5 ObjeCt-Oriented java 190 try { return super.clone(); } catch (CloneNotSupportedException ex) { return null; } } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj instanceof Team) { Team other = (Team) obj; return other.getName().equals(this.getName()) && other.getCity().equals(this.getCity()) && other.getPlayers().equals(this.getPlayers()); } else { return false; } } @Override public int hashCode() { int hashCode = cachedHashCode; if (hashCode == 0) { String concatStrings = name + city; if (players.size() > 0) { for (Player player : players) { concatStrings = concatStrings + player.getFirstName() + player.getLastName() + player.getPosition() + String.valueOf(player.getStatus()); } } Chapter 5 ObjeCt-Oriented java 191 hashCode = concatStrings.hashCode(); } return hashCode; } } The following solution demonstrates the comparison of two objects that contain the same values. public static void main(String[] args){ // Compare if two objects contain the same values Team team1 = new Team(); Team team2 = new Team(); // Build Player List Player newPlayer = new Player("Josh", "Juneau"); playerList.add(0, newPlayer); newPlayer = new Player("Jonathan", "Gennick"); playerList.add(1, newPlayer); newPlayer = new Player("Joe", "Blow"); playerList.add(1, newPlayer); newPlayer = new Player("John", "Smith"); playerList.add(1, newPlayer); newPlayer = new Player("Paul", "Bunyan"); playerList.add(1, newPlayer); team1.setName("Jokers"); team1.setCity("Crazyville"); team1.setPlayers(playerList); team2.setName("Jokers"); team2.setCity("Crazyville"); team2.setPlayers(playerList); if (team1.equals(team2)){ System.out.println("These object references contain the same values."); } else { Chapter 5 ObjeCt-Oriented java 192 System.out.println("These object references do NOT contain the same values."); } } The following are the results of running this code. These object references do NOT refer to the same object. These object references contain the same values. These object references refer to the same object. Download 3.2 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling