Created
April 20, 2023 15:24
-
-
Save joakime/dfa3c186d835a0010e5e7d0b74a81692 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package collection; | |
import java.util.HashSet; | |
import java.util.Set; | |
import java.util.stream.Collectors; | |
import org.junit.jupiter.api.Assertions; | |
import org.junit.jupiter.api.Test; | |
public class SetUnionDemo | |
{ | |
@Test | |
public void overlappingSets() | |
{ | |
Set<String> aSet = Set.of("hello", "world", "over"); | |
Set<String> bSet = Set.of("over", "and", "out"); | |
Set<String> zz = union(aSet, bSet); | |
Assertions.assertEquals(5, zz.size()); | |
String result = zz.stream().sorted().collect(Collectors.joining(",")); | |
Assertions.assertEquals("and,hello,out,over,world", result); | |
} | |
@Test | |
public void nullEntryASet_nullBSet() | |
{ | |
Set<String> aSet = new HashSet<>(); | |
aSet.add("hello"); | |
aSet.add(null); | |
aSet.add("world"); | |
aSet.add(null); | |
aSet.add("over"); | |
Set<String> bSet = null; | |
Set<String> zz = union(aSet, bSet); | |
Assertions.assertEquals(3, zz.size()); | |
String result = zz.stream().sorted().collect(Collectors.joining(",")); | |
Assertions.assertEquals("hello,over,world", result); | |
} | |
@Test | |
public void nullASet_BSet() | |
{ | |
Set<String> aSet = null; | |
Set<String> bSet = Set.of("bb", "zz", "aaa"); | |
Set<String> zz = union(aSet, bSet); | |
Assertions.assertEquals(3, zz.size()); | |
String result = zz.stream().sorted().collect(Collectors.joining(",")); | |
Assertions.assertEquals("aaa,bb,zz", result); | |
} | |
@Test | |
public void nullASet_nullBSet() | |
{ | |
Set<String> aSet = null; | |
Set<String> bSet = null; | |
Set<String> zz = union(aSet, bSet); | |
Assertions.assertNull(zz); | |
} | |
@Test | |
public void emptyASet_emptyBSet() | |
{ | |
Set<String> aSet = Set.of(); | |
Set<String> bSet = Set.of(); | |
Set<String> zz = union(aSet, bSet); | |
Assertions.assertNull(zz); | |
} | |
@Test | |
public void nullEntryASet_emptyBSet() | |
{ | |
Set<String> aSet = new HashSet<>(); | |
aSet.add(null); | |
Set<String> bSet = Set.of(); | |
Set<String> zz = union(aSet, bSet); | |
Assertions.assertNull(zz); | |
} | |
@Test | |
public void emptyASet_nullEntryBSet() | |
{ | |
Set<String> aSet = Set.of(); | |
Set<String> bSet = new HashSet<>(); | |
bSet.add(null); | |
Set<String> zz = union(aSet, bSet); | |
Assertions.assertNull(zz); | |
} | |
public static Set<String> union(Set<String> setA, Set<String> setB) | |
{ | |
if ((setA == null) && (setB == null)) | |
return null; | |
/* | |
if (setA == null || setA.isEmpty()) | |
return setB; // doesn't remove null entries from setB | |
if (setB == null || setB.isEmpty()) | |
return setA; // doesn't remove null entries from setA | |
*/ | |
Set<String> result = new HashSet<>(); | |
if (setA != null) result.addAll(setA); | |
if (setB != null) result.addAll(setB); | |
result.remove(null); // remove null entry (if any) | |
if (result.isEmpty()) | |
return null; // return null if the result is empty? | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment