Feature/bael 4280 diff between lists (#9600)
* BAEL-4280: Initial commit for finding diff between lists * BAEL-4280: Format * BAEL-4280: Refactor * BAEL-4280: Refactor not to use private methods * BAEL-4280: Refactor based on editor comments * BAEL-4280: Review round 2 * BAEL-4280: Use assertj * BAEL-4280: Use assertj * BAEL-4280: Shorter names for tests * BAEL-4280: Remove HashSet as its not used anymore * BAEL-4280: Use containsExactlyInAnyOrder for Set example * BAEL-4280: Remove distinct method call * BAEL-4280: Move impl to test * BAEL-4280: Use containsExactlyInAnyOrder * BAEL-4280: Rename test methods
This commit is contained in:
parent
22da30ca46
commit
ded13f14b2
|
@ -21,6 +21,12 @@
|
|||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
|
|
|
@ -0,0 +1,96 @@
|
|||
package com.baeldung.list.difference;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class FindDifferencesBetweenListsUnitTest {
|
||||
|
||||
private static final List<String> listOne = Arrays.asList("Jack", "Tom", "Sam", "John", "James", "Jack");
|
||||
private static final List<String> listTwo = Arrays.asList("Jack", "Daniel", "Sam", "Alan", "James", "George");
|
||||
|
||||
@Test
|
||||
public void givenLists_whenUsingPlainJavaImpl_thenDifferencesAreFound() {
|
||||
List<String> differences = new ArrayList<>(listOne);
|
||||
differences.removeAll(listTwo);
|
||||
assertEquals(2, differences.size());
|
||||
assertThat(differences).containsExactly("Tom", "John");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenReverseLists_whenUsingPlainJavaImpl_thenDifferencesAreFound() {
|
||||
List<String> differences = new ArrayList<>(listTwo);
|
||||
differences.removeAll(listOne);
|
||||
assertEquals(3, differences.size());
|
||||
assertThat(differences).containsExactly("Daniel", "Alan", "George");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLists_whenUsingJavaStreams_thenDifferencesAreFound() {
|
||||
List<String> differences = listOne.stream()
|
||||
.filter(element -> !listTwo.contains(element))
|
||||
.collect(Collectors.toList());
|
||||
assertEquals(2, differences.size());
|
||||
assertThat(differences).containsExactly("Tom", "John");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenReverseLists_whenUsingJavaStreams_thenDifferencesAreFound() {
|
||||
List<String> differences = listTwo.stream()
|
||||
.filter(element -> !listOne.contains(element))
|
||||
.collect(Collectors.toList());
|
||||
assertEquals(3, differences.size());
|
||||
assertThat(differences).containsExactly("Daniel", "Alan", "George");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLists_whenUsingGoogleGuava_thenDifferencesAreFound() {
|
||||
List<String> differences = new ArrayList<>(Sets.difference(Sets.newHashSet(listOne), Sets.newHashSet(listTwo)));
|
||||
assertEquals(2, differences.size());
|
||||
assertThat(differences).containsExactlyInAnyOrder("Tom", "John");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenReverseLists_whenUsingGoogleGuava_thenDifferencesAreFound() {
|
||||
List<String> differences = new ArrayList<>(Sets.difference(Sets.newHashSet(listTwo), Sets.newHashSet(listOne)));
|
||||
assertEquals(3, differences.size());
|
||||
assertThat(differences).containsExactlyInAnyOrder("Daniel", "Alan", "George");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLists_whenUsingApacheCommons_thenDifferencesAreFound() {
|
||||
List<String> differences = new ArrayList<>((CollectionUtils.removeAll(listOne, listTwo)));
|
||||
assertEquals(2, differences.size());
|
||||
assertThat(differences).containsExactly("Tom", "John");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenReverseLists_whenUsingApacheCommons_thenDifferencesAreFound() {
|
||||
List<String> differences = new ArrayList<>((CollectionUtils.removeAll(listTwo, listOne)));
|
||||
assertEquals(3, differences.size());
|
||||
assertThat(differences).containsExactly("Daniel", "Alan", "George");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLists_whenUsingPlainJavaImpl_thenDifferencesWithDuplicatesAreFound() {
|
||||
List<String> differences = new ArrayList<>(listOne);
|
||||
listTwo.forEach(differences::remove);
|
||||
assertThat(differences).containsExactly("Tom", "John", "Jack");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLists_whenUsingApacheCommons_thenDifferencesWithDuplicatesAreFound() {
|
||||
List<String> differences = new ArrayList<>(CollectionUtils.subtract(listOne, listTwo));
|
||||
assertEquals(3, differences.size());
|
||||
assertThat(differences).containsExactly("Tom", "John", "Jack");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue