[BAEL-9555] - Created a core-java-modules folder
This commit is contained in:
11
core-java-modules/core-java-collections-set/README.md
Normal file
11
core-java-modules/core-java-collections-set/README.md
Normal file
@@ -0,0 +1,11 @@
|
||||
=========
|
||||
|
||||
## Core Java Sets Cookbooks and Examples
|
||||
|
||||
### Relevant Articles:
|
||||
- [Set Operations in Java](http://www.baeldung.com/set-operations-in-java)
|
||||
- [HashSet and TreeSet Comparison](http://www.baeldung.com/java-hashset-vs-treeset)
|
||||
- [A Guide to HashSet in Java](http://www.baeldung.com/java-hashset)
|
||||
- [A Guide to TreeSet in Java](http://www.baeldung.com/java-tree-set)
|
||||
- [Initializing HashSet at the Time of Construction](http://www.baeldung.com/java-initialize-hashset)
|
||||
- [Guide to EnumSet](https://www.baeldung.com/java-enumset)
|
||||
33
core-java-modules/core-java-collections-set/pom.xml
Normal file
33
core-java-modules/core-java-collections-set/pom.xml
Normal 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>
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.enumset;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
|
||||
public class EnumSets {
|
||||
|
||||
public enum Color {
|
||||
RED, YELLOW, GREEN, BLUE, BLACK, WHITE
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
EnumSet<Color> allColors = EnumSet.allOf(Color.class);
|
||||
System.out.println(allColors);
|
||||
|
||||
EnumSet<Color> noColors = EnumSet.noneOf(Color.class);
|
||||
System.out.println(noColors);
|
||||
|
||||
EnumSet<Color> blackAndWhite = EnumSet.of(Color.BLACK, Color.WHITE);
|
||||
System.out.println(blackAndWhite);
|
||||
|
||||
EnumSet<Color> noBlackOrWhite = EnumSet.complementOf(blackAndWhite);
|
||||
System.out.println(noBlackOrWhite);
|
||||
|
||||
EnumSet<Color> range = EnumSet.range(Color.YELLOW, Color.BLUE);
|
||||
System.out.println(range);
|
||||
|
||||
EnumSet<Color> blackAndWhiteCopy = EnumSet.copyOf(EnumSet.of(Color.BLACK, Color.WHITE));
|
||||
System.out.println(blackAndWhiteCopy);
|
||||
|
||||
List<Color> colorsList = new ArrayList<>();
|
||||
colorsList.add(Color.RED);
|
||||
EnumSet<Color> listCopy = EnumSet.copyOf(colorsList);
|
||||
System.out.println(listCopy);
|
||||
|
||||
EnumSet<Color> set = EnumSet.noneOf(Color.class);
|
||||
set.add(Color.RED);
|
||||
set.add(Color.YELLOW);
|
||||
set.contains(Color.RED);
|
||||
set.forEach(System.out::println);
|
||||
set.remove(Color.RED);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package com.baeldung.collection;
|
||||
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class WhenUsingHashSet {
|
||||
|
||||
@Test
|
||||
public void whenAddingElement_shouldAddElement() {
|
||||
Set<String> hashset = new HashSet<>();
|
||||
Assert.assertTrue(hashset.add("String Added"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingForElement_shouldSearchForElement() {
|
||||
Set<String> hashsetContains = new HashSet<>();
|
||||
hashsetContains.add("String Added");
|
||||
Assert.assertTrue(hashsetContains.contains("String Added"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingTheSizeOfHashSet_shouldReturnThesize() {
|
||||
Set<String> hashSetSize = new HashSet<>();
|
||||
hashSetSize.add("String Added");
|
||||
Assert.assertEquals(1, hashSetSize.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingForEmptyHashSet_shouldCheckForEmpty() {
|
||||
Set<String> emptyHashSet = new HashSet<>();
|
||||
Assert.assertTrue(emptyHashSet.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRemovingElement_shouldRemoveElement() {
|
||||
Set<String> removeFromHashSet = new HashSet<>();
|
||||
removeFromHashSet.add("String Added");
|
||||
Assert.assertTrue(removeFromHashSet.remove("String Added"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenClearingHashSet_shouldClearHashSet() {
|
||||
Set<String> clearHashSet = new HashSet<>();
|
||||
clearHashSet.add("String Added");
|
||||
clearHashSet.clear();
|
||||
Assert.assertTrue(clearHashSet.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIteratingHashSet_shouldIterateHashSet() {
|
||||
Set<String> hashset = new HashSet<>();
|
||||
hashset.add("First");
|
||||
hashset.add("Second");
|
||||
hashset.add("Third");
|
||||
Iterator<String> itr = hashset.iterator();
|
||||
while (itr.hasNext()) {
|
||||
System.out.println(itr.next());
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = ConcurrentModificationException.class)
|
||||
public void whenModifyingHashSetWhileIterating_shouldThrowException() {
|
||||
Set<String> hashset = new HashSet<>();
|
||||
hashset.add("First");
|
||||
hashset.add("Second");
|
||||
hashset.add("Third");
|
||||
Iterator<String> itr = hashset.iterator();
|
||||
while (itr.hasNext()) {
|
||||
itr.next();
|
||||
hashset.remove("Second");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRemovingElementUsingIterator_shouldRemoveElement() {
|
||||
Set<String> hashset = new HashSet<>();
|
||||
hashset.add("First");
|
||||
hashset.add("Second");
|
||||
hashset.add("Third");
|
||||
Iterator<String> itr = hashset.iterator();
|
||||
while (itr.hasNext()) {
|
||||
String element = itr.next();
|
||||
if (element.equals("Second"))
|
||||
itr.remove();
|
||||
}
|
||||
Assert.assertEquals(2, hashset.size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
package com.baeldung.collection;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.Iterator;
|
||||
import java.util.NavigableSet;
|
||||
import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class WhenUsingTreeSet {
|
||||
|
||||
private static class Element {
|
||||
private Integer id;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return id.toString();
|
||||
}
|
||||
}
|
||||
|
||||
private Comparator<Element> comparator = (ele1, ele2) -> {
|
||||
return ele1.getId()
|
||||
.compareTo(ele2.getId());
|
||||
};
|
||||
|
||||
@Test
|
||||
public void whenAddingElement_shouldAddElement() {
|
||||
Set<String> treeSet = new TreeSet<>();
|
||||
Assert.assertTrue(treeSet.add("String Added"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingForElement_shouldSearchForElement() {
|
||||
Set<String> treeSetContains = new TreeSet<>();
|
||||
treeSetContains.add("String Added");
|
||||
Assert.assertTrue(treeSetContains.contains("String Added"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRemovingElement_shouldRemoveElement() {
|
||||
Set<String> removeFromTreeSet = new TreeSet<>();
|
||||
removeFromTreeSet.add("String Added");
|
||||
Assert.assertTrue(removeFromTreeSet.remove("String Added"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenClearingTreeSet_shouldClearTreeSet() {
|
||||
Set<String> clearTreeSet = new TreeSet<>();
|
||||
clearTreeSet.add("String Added");
|
||||
clearTreeSet.clear();
|
||||
Assert.assertTrue(clearTreeSet.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingTheSizeOfTreeSet_shouldReturnThesize() {
|
||||
Set<String> treeSetSize = new TreeSet<>();
|
||||
treeSetSize.add("String Added");
|
||||
Assert.assertEquals(1, treeSetSize.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingForEmptyTreeSet_shouldCheckForEmpty() {
|
||||
Set<String> emptyTreeSet = new TreeSet<>();
|
||||
Assert.assertTrue(emptyTreeSet.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIteratingTreeSet_shouldIterateTreeSetInAscendingOrder() {
|
||||
Set<String> treeSet = new TreeSet<>();
|
||||
treeSet.add("First");
|
||||
treeSet.add("Second");
|
||||
treeSet.add("Third");
|
||||
Iterator<String> itr = treeSet.iterator();
|
||||
while (itr.hasNext()) {
|
||||
System.out.println(itr.next());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIteratingTreeSet_shouldIterateTreeSetInDescendingOrder() {
|
||||
TreeSet<String> treeSet = new TreeSet<>();
|
||||
treeSet.add("First");
|
||||
treeSet.add("Second");
|
||||
treeSet.add("Third");
|
||||
Iterator<String> itr = treeSet.descendingIterator();
|
||||
while (itr.hasNext()) {
|
||||
System.out.println(itr.next());
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = ConcurrentModificationException.class)
|
||||
public void whenModifyingTreeSetWhileIterating_shouldThrowException() {
|
||||
Set<String> treeSet = new TreeSet<>();
|
||||
treeSet.add("First");
|
||||
treeSet.add("Second");
|
||||
treeSet.add("Third");
|
||||
Iterator<String> itr = treeSet.iterator();
|
||||
while (itr.hasNext()) {
|
||||
itr.next();
|
||||
treeSet.remove("Second");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRemovingElementUsingIterator_shouldRemoveElement() {
|
||||
Set<String> treeSet = new TreeSet<>();
|
||||
treeSet.add("First");
|
||||
treeSet.add("Second");
|
||||
treeSet.add("Third");
|
||||
Iterator<String> itr = treeSet.iterator();
|
||||
while (itr.hasNext()) {
|
||||
String element = itr.next();
|
||||
if (element.equals("Second"))
|
||||
itr.remove();
|
||||
}
|
||||
Assert.assertEquals(2, treeSet.size());
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void whenAddingNullToNonEmptyTreeSet_shouldThrowException() {
|
||||
Set<String> treeSet = new TreeSet<>();
|
||||
treeSet.add("First");
|
||||
treeSet.add(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingFirstElement_shouldReturnFirstElement() {
|
||||
TreeSet<String> treeSet = new TreeSet<>();
|
||||
treeSet.add("First");
|
||||
Assert.assertEquals("First", treeSet.first());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingLastElement_shouldReturnLastElement() {
|
||||
TreeSet<String> treeSet = new TreeSet<>();
|
||||
treeSet.add("First");
|
||||
treeSet.add("Last");
|
||||
Assert.assertEquals("Last", treeSet.last());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingComparator_shouldSortAndInsertElements() {
|
||||
Set<Element> treeSet = new TreeSet<>(comparator);
|
||||
Element ele1 = new Element();
|
||||
ele1.setId(100);
|
||||
Element ele2 = new Element();
|
||||
ele2.setId(200);
|
||||
|
||||
treeSet.add(ele1);
|
||||
treeSet.add(ele2);
|
||||
|
||||
System.out.println(treeSet);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingHeadSet_shouldReturnElementsLessThanSpecifiedElement() {
|
||||
Set<Element> treeSet = new TreeSet<>(comparator);
|
||||
Element ele1 = new Element();
|
||||
ele1.setId(100);
|
||||
Element ele2 = new Element();
|
||||
ele2.setId(200);
|
||||
|
||||
treeSet.add(ele1);
|
||||
treeSet.add(ele2);
|
||||
|
||||
System.out.println(treeSet);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingSubSet_shouldReturnSubSetElements() {
|
||||
SortedSet<Integer> treeSet = new TreeSet<>();
|
||||
treeSet.add(1);
|
||||
treeSet.add(2);
|
||||
treeSet.add(3);
|
||||
treeSet.add(4);
|
||||
treeSet.add(5);
|
||||
treeSet.add(6);
|
||||
|
||||
Set<Integer> expectedSet = new TreeSet<>();
|
||||
expectedSet.add(2);
|
||||
expectedSet.add(3);
|
||||
expectedSet.add(4);
|
||||
expectedSet.add(5);
|
||||
|
||||
Set<Integer> subSet = treeSet.subSet(2, 6);
|
||||
Assert.assertEquals(expectedSet, subSet);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingHeadSet_shouldReturnHeadSetElements() {
|
||||
SortedSet<Integer> treeSet = new TreeSet<>();
|
||||
treeSet.add(1);
|
||||
treeSet.add(2);
|
||||
treeSet.add(3);
|
||||
treeSet.add(4);
|
||||
treeSet.add(5);
|
||||
treeSet.add(6);
|
||||
|
||||
Set<Integer> subSet = treeSet.headSet(6);
|
||||
Assert.assertEquals(subSet, treeSet.subSet(1, 6));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingTailSet_shouldReturnTailSetElements() {
|
||||
NavigableSet<Integer> treeSet = new TreeSet<>();
|
||||
treeSet.add(1);
|
||||
treeSet.add(2);
|
||||
treeSet.add(3);
|
||||
treeSet.add(4);
|
||||
treeSet.add(5);
|
||||
treeSet.add(6);
|
||||
|
||||
Set<Integer> subSet = treeSet.tailSet(3);
|
||||
Assert.assertEquals(subSet, treeSet.subSet(3, true, 6, true));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.java.set;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Sets;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class HashSetInitalizingUnitTest {
|
||||
@Test
|
||||
public void whenUsingJava_usingArraysStaticMethod_thenCorrectSize() {
|
||||
Set<String> set = new HashSet<>(Arrays.asList("a", "b", "c"));
|
||||
assertEquals(3, set.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJava_usingAnonymousClass_thenCorrectSize() {
|
||||
Set<String> set = new HashSet<String>(){{
|
||||
add("a");
|
||||
add("b");
|
||||
add("c");
|
||||
}};
|
||||
assertEquals(3, set.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJava_creatingSingletonSet_thenCorrectSize() {
|
||||
Set<String> set = Collections.singleton("a");
|
||||
assertEquals(1, set.size());
|
||||
}
|
||||
|
||||
public static final <T> Set<T> newHashSet(T... objs) {
|
||||
Set<T> set = new HashSet<T>();
|
||||
Collections.addAll(set, objs);
|
||||
return set;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJava_usingCustomStaticUtilMethod_thenCorrectSize() {
|
||||
Set<String> set = newHashSet("a","b","c");
|
||||
assertEquals(3, set.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJava8_usingCollectOnStream_thenCorrectSize() {
|
||||
Set<String> set = Stream.of("a", "b", "c").collect(Collectors.toCollection(HashSet::new));
|
||||
assertEquals(3, set.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingGoogleGuava_createMutableSet_thenCorrectSize() {
|
||||
Set<String> set = Sets.newHashSet("a", "b", "c");
|
||||
assertEquals(3, set.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingGoogleGuava_createImmutableSet_thenCorrectSize() {
|
||||
Set<String> set = ImmutableSet.of("a", "b", "c");
|
||||
assertEquals(3, set.size());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.baeldung.java.set;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class SetUnitTest {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(SetUnitTest.class);
|
||||
|
||||
@Test
|
||||
public void givenTreeSet_whenRetrievesObjects_thenNaturalOrder() {
|
||||
Set<String> set = new TreeSet<>();
|
||||
set.add("Baeldung");
|
||||
set.add("is");
|
||||
set.add("Awesome");
|
||||
assertEquals(3, set.size());
|
||||
assertTrue(set.iterator()
|
||||
.next()
|
||||
.equals("Awesome"));
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenTreeSet_whenAddNullObject_thenNullPointer() {
|
||||
Set<String> set = new TreeSet<>();
|
||||
set.add("Baeldung");
|
||||
set.add("is");
|
||||
set.add(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHashSet_whenAddNullObject_thenOK() {
|
||||
Set<String> set = new HashSet<>();
|
||||
set.add("Baeldung");
|
||||
set.add("is");
|
||||
set.add(null);
|
||||
assertEquals(3, set.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHashSetAndTreeSet_whenAddObjects_thenHashSetIsFaster() {
|
||||
|
||||
long hashSetInsertionTime = measureExecution(() -> {
|
||||
Set<String> set = new HashSet<>();
|
||||
set.add("Baeldung");
|
||||
set.add("is");
|
||||
set.add("Awesome");
|
||||
});
|
||||
|
||||
long treeSetInsertionTime = measureExecution(() -> {
|
||||
Set<String> set = new TreeSet<>();
|
||||
set.add("Baeldung");
|
||||
set.add("is");
|
||||
set.add("Awesome");
|
||||
});
|
||||
|
||||
LOG.debug("HashSet insertion time: {}", hashSetInsertionTime);
|
||||
LOG.debug("TreeSet insertion time: {}", treeSetInsertionTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHashSetAndTreeSet_whenAddDuplicates_thenOnlyUnique() {
|
||||
Set<String> set = new HashSet<>();
|
||||
set.add("Baeldung");
|
||||
set.add("Baeldung");
|
||||
assertTrue(set.size() == 1);
|
||||
|
||||
Set<String> set2 = new TreeSet<>();
|
||||
set2.add("Baeldung");
|
||||
set2.add("Baeldung");
|
||||
assertTrue(set2.size() == 1);
|
||||
}
|
||||
|
||||
@Test(expected = ConcurrentModificationException.class)
|
||||
public void givenHashSet_whenModifyWhenIterator_thenFailFast() {
|
||||
Set<String> set = new HashSet<>();
|
||||
set.add("Baeldung");
|
||||
Iterator<String> it = set.iterator();
|
||||
|
||||
while (it.hasNext()) {
|
||||
set.add("Awesome");
|
||||
it.next();
|
||||
}
|
||||
}
|
||||
|
||||
private static long measureExecution(Runnable task) {
|
||||
long startTime = System.nanoTime();
|
||||
task.run();
|
||||
long endTime = System.nanoTime();
|
||||
long executionTime = endTime - startTime;
|
||||
LOG.debug(String.valueOf(executionTime));
|
||||
return executionTime;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user