Skip to content

Commit f0cc0c8

Browse files
authored
BAEL-9062 (#18346)
1 parent 724d222 commit f0cc0c8

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.baeldung.comparelong;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.Collections;
8+
import java.util.Comparator;
9+
import java.util.List;
10+
import java.util.stream.Collectors;
11+
12+
import org.junit.Test;
13+
14+
public class CompareLongWithSortUnitTest {
15+
16+
@Test
17+
public void givenLongList_whenUseCollectionSortToCompare_thenReturnAsc() {
18+
List<Long> numbers = new ArrayList<>(Arrays.asList(500L, 200L, 800L));
19+
Collections.sort(numbers);
20+
assertEquals(Arrays.asList(200L, 500L, 800L), numbers);
21+
}
22+
23+
@Test
24+
public void givenLongList_whenUseCollectionSortToCompareWithReverseComparator_thenReturnDsc() {
25+
List<Long> numbers = new ArrayList<>(Arrays.asList(500L, 200L, 800L));
26+
Collections.sort(numbers, Comparator.reverseOrder());
27+
assertEquals(Arrays.asList(800L, 500L, 200L), numbers);
28+
}
29+
30+
@Test
31+
public void givenLongList_whenUseCustomComparator_thenReturnExpectedResult() {
32+
List<Long> numbers = new ArrayList<>(Arrays.asList(500L, 200L, 800L));
33+
long target = 400L;
34+
Collections.sort(numbers, (a, b) -> {
35+
long distanceA = Math.abs(a - target);
36+
long distanceB = Math.abs(b - target);
37+
return Long.compare(distanceA, distanceB);
38+
});
39+
assertEquals(Arrays.asList(500L, 200L, 800L), numbers);
40+
}
41+
42+
@Test
43+
public void givenLongList_whenUseMultipleCriteriaToCompare_thenReturnExpectedResult() {
44+
List<Long> numbers = new ArrayList<>(Arrays.asList(-500L, 200L, -800L, 300L));
45+
Collections.sort(numbers, Comparator.comparingLong(n -> Math.abs((Long) n))
46+
.thenComparingLong(n -> (Long) n));
47+
48+
assertEquals(Arrays.asList(200L, 300L, -500L, -800L), numbers);
49+
}
50+
51+
@Test
52+
public void givenLongListWithNull_whenUsingNullsFirst_thenReturnNullAtFirstPosition() {
53+
List<Long> numbers = new ArrayList<>(Arrays.asList(500L, null, 200L, 800L));
54+
Collections.sort(numbers, Comparator.nullsFirst(Comparator.naturalOrder()));
55+
assertEquals(Arrays.asList(null, 200L, 500L, 800L), numbers);
56+
}
57+
58+
@Test
59+
public void givenLongListWithNull_whenUsingNullsLast_thenReturnNullAtLastPosition() {
60+
List<Long> numbers = new ArrayList<>(Arrays.asList(500L, null, 200L, 800L));
61+
Collections.sort(numbers, Comparator.nullsLast(Comparator.naturalOrder()));
62+
assertEquals(Arrays.asList(200L, 500L, 800L, null), numbers);
63+
}
64+
65+
@Test
66+
public void givenObjectWithLongAttribute_whenComparingObjectWithLongValue_thenReturnExpectedResult() {
67+
List<Transaction> transactions = new ArrayList<>();
68+
transactions.add(new Transaction(500L));
69+
transactions.add(new Transaction(200L));
70+
transactions.add(new Transaction(800L));
71+
Collections.sort(transactions, Comparator.comparingLong(Transaction::getAmount));
72+
assertEquals(Arrays.asList(200L, 500L, 800L), transactions.stream()
73+
.map(Transaction::getAmount)
74+
.collect(Collectors.toList()));
75+
}
76+
77+
}
78+
79+
class Transaction {
80+
81+
private Long amount;
82+
83+
public Transaction(Long amount) {
84+
this.amount = amount;
85+
}
86+
87+
public Long getAmount() {
88+
return amount;
89+
}
90+
}

0 commit comments

Comments
 (0)