Fixed the "Comparator and Comparable in Java" Article
This commit is contained in:
parent
94f50e785e
commit
41964ef72e
|
@ -45,7 +45,7 @@ public class Player implements Comparable<Player> {
|
|||
|
||||
@Override
|
||||
public int compareTo(Player otherPlayer) {
|
||||
return (this.getRanking() - otherPlayer.getRanking());
|
||||
return Integer.compare(getRanking(), otherPlayer.getRanking());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ public class PlayerAgeComparator implements Comparator<Player> {
|
|||
|
||||
@Override
|
||||
public int compare(Player firstPlayer, Player secondPlayer) {
|
||||
return (firstPlayer.getAge() - secondPlayer.getAge());
|
||||
return Integer.compare(firstPlayer.getAge(), secondPlayer.getAge());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ public class PlayerRankingComparator implements Comparator<Player> {
|
|||
|
||||
@Override
|
||||
public int compare(Player firstPlayer, Player secondPlayer) {
|
||||
return (firstPlayer.getRanking() - secondPlayer.getRanking());
|
||||
return Integer.compare(firstPlayer.getRanking(), secondPlayer.getRanking());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.comparator;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class AvoidingSubtractionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTwoPlayers_whenUsingSubtraction_thenOverflow() {
|
||||
Comparator<Player> comparator = (p1, p2) -> p1.getRanking() - p2.getRanking();
|
||||
Player player1 = new Player(59, "John", Integer.MAX_VALUE);
|
||||
Player player2 = new Player(67, "Roger", -1);
|
||||
|
||||
List<Player> players = Arrays.asList(player1, player2);
|
||||
players.sort(comparator);
|
||||
System.out.println(players);
|
||||
|
||||
assertEquals("John", players.get(0).getName());
|
||||
assertEquals("Roger", players.get(1).getName());
|
||||
}
|
||||
}
|
|
@ -1,14 +1,14 @@
|
|||
package com.baeldung.comparator;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class Java8ComparatorUnitTest {
|
||||
|
||||
|
@ -28,7 +28,7 @@ public class Java8ComparatorUnitTest {
|
|||
@Test
|
||||
public void whenComparing_UsingLambda_thenSorted() {
|
||||
System.out.println("************** Java 8 Comaparator **************");
|
||||
Comparator<Player> byRanking = (Player player1, Player player2) -> player1.getRanking() - player2.getRanking();
|
||||
Comparator<Player> byRanking = (Player player1, Player player2) -> Integer.compare(player1.getRanking(), player2.getRanking());
|
||||
|
||||
System.out.println("Before Sorting : " + footballTeam);
|
||||
Collections.sort(footballTeam, byRanking);
|
||||
|
|
Loading…
Reference in New Issue