Java Reference
In-Depth Information
}
}
The following solution demonstrates the comparison of two objects that contain the
same values.
// 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 {
System.out.println("These object references do NOT
contain the same values.");
}
The results of running this code:
Search WWH ::




Custom Search