BAEL-2837 Add sample Set operations in Java (#6697)

* BAEL-2837: Add sample set operations in Java

* BAEL-2837 add new module core-java-collections-set

* BAEL-2837 Updated readme.md file

* BAEL-2837 Tidy dependencies in pom.xml

* BAEL-2837 tidy white space in pom.xml

* BAEL-2837 Rename unit test class

* BAEL-2837 Removed unused logback-xml
This commit is contained in:
pcoates33 2019-04-22 18:12:56 +01:00 committed by Josh Cummings
parent 49a806e787
commit b63f8882ea
4 changed files with 134 additions and 0 deletions

View File

@ -0,0 +1,6 @@
=========
## Core Java Sets Cookbooks and Examples
### Relevant Articles:
- [Set Operations in Java](http://www.baeldung.com/set-operations-in-java)

View File

@ -0,0 +1,33 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-collections-set</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-collections-set</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version>
</dependency>
</dependencies>
<properties>
<commons-collections4.version>4.3</commons-collections4.version>
<guava.version>27.1-jre</guava.version>
</properties>
</project>

View File

@ -0,0 +1,93 @@
package com.baeldung.set;
import static org.junit.Assert.*;
import org.apache.commons.collections4.SetUtils;
import org.junit.Test;
import com.google.common.collect.Sets;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class SetOperationsUnitTest {
private Set<Integer> setA = setOf(1,2,3,4);
private Set<Integer> setB = setOf(2,4,6,8);
private static Set<Integer> setOf(Integer... values) {
return new HashSet<Integer>(Arrays.asList(values));
}
@Test
public void givenTwoSets_WhenWeRetainAll_ThenWeIntersectThem() {
Set<Integer> intersectSet = new HashSet<>(setA);
intersectSet.retainAll(setB);
assertEquals(setOf(2,4), intersectSet);
}
@Test
public void givenTwoSets_WhenWeAddAll_ThenWeUnionThem() {
Set<Integer> unionSet = new HashSet<>(setA);
unionSet.addAll(setB);
assertEquals(setOf(1,2,3,4,6,8), unionSet);
}
@Test
public void givenTwoSets_WhenRemoveAll_ThenWeGetTheDifference() {
Set<Integer> differenceSet = new HashSet<>(setA);
differenceSet.removeAll(setB);
assertEquals(setOf(1,3), differenceSet);
}
@Test
public void givenTwoStreams_WhenWeFilterThem_ThenWeCanGetTheIntersect() {
Set<Integer> intersectSet = setA.stream()
.filter(setB::contains)
.collect(Collectors.toSet());
assertEquals(setOf(2,4), intersectSet);
}
@Test
public void givenTwoStreams_WhenWeConcatThem_ThenWeGetTheUnion() {
Set<Integer> unionSet = Stream.concat(setA.stream(), setB.stream())
.collect(Collectors.toSet());
assertEquals(setOf(1,2,3,4,6,8), unionSet);
}
@Test
public void givenTwoStreams_WhenWeFilterThem_ThenWeCanGetTheDifference() {
Set<Integer> differenceSet = setA.stream()
.filter(val -> !setB.contains(val))
.collect(Collectors.toSet());
assertEquals(setOf(1,3), differenceSet);
}
@Test
public void givenTwoSets_WhenWeUseApacheCommonsIntersect_ThenWeGetTheIntersect() {
Set<Integer> intersectSet = SetUtils.intersection(setA, setB);
assertEquals(setOf(2,4), intersectSet);
}
@Test
public void givenTwoSets_WhenWeUseApacheCommonsUnion_ThenWeGetTheUnion() {
Set<Integer> unionSet = SetUtils.union(setA, setB);
assertEquals(setOf(1,2,3,4,6,8), unionSet);
}
@Test
public void givenTwoSets_WhenWeUseGuavaIntersect_ThenWeGetTheIntersect() {
Set<Integer> intersectSet = Sets.intersection(setA, setB);
assertEquals(setOf(2,4), intersectSet);
}
@Test
public void givenTwoSets_WhenWeUseGuavaUnion_ThenWeGetTheUnion() {
Set<Integer> unionSet = Sets.union(setA, setB);
assertEquals(setOf(1,2,3,4,6,8), unionSet);
}
}

View File

@ -389,6 +389,7 @@
<module>core-java-collections</module>
<module>core-java-collections-map</module>
<module>core-java-collections-list</module>
<module>core-java-collections-set</module>
<module>core-java-concurrency-basic</module>
<module>core-java-concurrency-collections</module>
<module>core-java-io</module>
@ -1059,6 +1060,7 @@
<module>core-java-collections</module>
<module>core-java-collections-map</module>
<module>core-java-collections-list</module>
<module>core-java-collections-set</module>
<module>core-java-concurrency-basic</module>
<module>core-java-concurrency-collections</module>
<module>core-java-io</module>