Merge pull request #9852 from alimate/BAEL-4536
BAEL-4536: Improvement - compareTo Implementations
This commit is contained in:
commit
2ac7e8af89
@ -5,7 +5,16 @@ import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
@ -138,7 +147,7 @@ public class JavaSortingUnitTest {
|
||||
HashSet<Integer> descSortedIntegersSet = new LinkedHashSet<>(Arrays.asList(255, 200, 123, 89, 88, 66, 7, 5, 1));
|
||||
|
||||
ArrayList<Integer> list = new ArrayList<>(integersSet);
|
||||
list.sort((i1, i2) -> i2 - i1);
|
||||
list.sort(Comparator.reverseOrder());
|
||||
integersSet = new LinkedHashSet<>(list);
|
||||
|
||||
assertTrue(Arrays.equals(integersSet.toArray(), descSortedIntegersSet.toArray()));
|
||||
|
@ -37,7 +37,7 @@ public class Human {
|
||||
|
||||
public static int compareByNameThenAge(final Human lhs, final Human rhs) {
|
||||
if (lhs.name.equals(rhs.name)) {
|
||||
return lhs.age - rhs.age;
|
||||
return Integer.compare(lhs.age, rhs.age);
|
||||
} else {
|
||||
return lhs.name.compareTo(rhs.name);
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ public class Java8SortUnitTest {
|
||||
final List<Human> humans = Lists.newArrayList(new Human("Sarah", 12), new Human("Sarah", 10), new Human("Zack", 12));
|
||||
humans.sort((lhs, rhs) -> {
|
||||
if (lhs.getName().equals(rhs.getName())) {
|
||||
return lhs.getAge() - rhs.getAge();
|
||||
return Integer.compare(lhs.getAge(), rhs.getAge());
|
||||
} else {
|
||||
return lhs.getName().compareTo(rhs.getName());
|
||||
}
|
||||
|
@ -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…
x
Reference in New Issue
Block a user