Skip to content

Commit 31de9bb

Browse files
author
Eugen Paraschiv
committed
finishing the guava ordering examples
1 parent 2a54424 commit 31de9bb

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

guava/src/test/java/org/baeldung/guava/collections/GuavaOrderingExamplesTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.baeldung.guava.collections;
22

3+
import static org.hamcrest.Matchers.equalTo;
34
import static org.hamcrest.Matchers.nullValue;
45
import static org.junit.Assert.assertFalse;
56
import static org.junit.Assert.assertThat;
@@ -11,6 +12,7 @@
1112

1213
import org.junit.Test;
1314

15+
import com.google.common.base.Functions;
1416
import com.google.common.collect.Lists;
1517
import com.google.common.collect.Ordering;
1618
import com.google.common.primitives.Ints;
@@ -59,6 +61,16 @@ public final void whenSortingWithNaturalOrdering_thenCorectlySorted() {
5961
assertTrue(Ordering.natural().isOrdered(toSort));
6062
}
6163

64+
// checking string ordering
65+
66+
@Test
67+
public final void givenCollectionContainsDuplicates_whenCheckingStringOrdering_thenNo() {
68+
final List<Integer> toSort = Arrays.asList(3, 5, 4, 2, 1, 2);
69+
Collections.sort(toSort, Ordering.natural());
70+
71+
assertFalse(Ordering.natural().isStrictlyOrdered(toSort));
72+
}
73+
6274
// custom - by length
6375

6476
@Test
@@ -124,4 +136,44 @@ public final void givenUnorderdList_whenUsingToStringForSortingObjects_thenSorte
124136
assertTrue(expectedOrder.isOrdered(toSort));
125137
}
126138

139+
// binary search
140+
141+
@Test
142+
public final void whenPerformingBinarySearch_thenFound() {
143+
final List<Integer> toSort = Arrays.asList(1, 2, 11);
144+
Collections.sort(toSort, Ordering.usingToString());
145+
final int found = Ordering.usingToString().binarySearch(toSort, 2);
146+
147+
System.out.println(found);
148+
}
149+
150+
// min/max without actually sorting
151+
152+
@Test
153+
public final void whenFindingTheMinimalElementWithoutSorting_thenFound() {
154+
final List<Integer> toSort = Arrays.asList(2, 1, 11, 100, 8, 14);
155+
final int found = Ordering.natural().min(toSort);
156+
assertThat(found, equalTo(1));
157+
}
158+
159+
@Test
160+
public final void whenFindingTheFirstFewElements_thenCorrect() {
161+
final List<Integer> toSort = Arrays.asList(2, 1, 11, 100, 8, 14);
162+
final List<Integer> leastOf = Ordering.natural().leastOf(toSort, 3);
163+
final List<Integer> expected = Lists.newArrayList(1, 2, 8);
164+
assertThat(expected, equalTo(leastOf));
165+
}
166+
167+
// order the results of a Function
168+
169+
@Test
170+
public final void givenListOfNumbers_whenRunningAToStringFunctionThenSorting_thenCorrect() {
171+
final List<Integer> toSort = Arrays.asList(2, 1, 11, 100, 8, 14);
172+
final Ordering<Object> ordering = Ordering.natural().onResultOf(Functions.toStringFunction());
173+
final List<Integer> sortedCopy = ordering.sortedCopy(toSort);
174+
175+
final List<Integer> expected = Lists.newArrayList(1, 100, 11, 14, 2, 8);
176+
assertThat(expected, equalTo(sortedCopy));
177+
}
178+
127179
}

0 commit comments

Comments
 (0)