From 7ab9be3c1f643d088e4abc5ddc05dca5468afd65 Mon Sep 17 00:00:00 2001 From: Olu <56642018+olu-damilare@users.noreply.github.com> Date: Fri, 28 Oct 2022 06:58:17 +0100 Subject: [PATCH] A Guide to LinkedHashSet in Java (#12932) * Creating a Deep vs Shallow Copy of an Object in Java * completed tests for LinkedHashSet * update * updated PR --- .../linkedhashset/LinkedHashSetUnitTest.java | 141 ++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 core-java-modules/core-java-collections-set-2/src/test/java/com/baeldung/linkedhashset/LinkedHashSetUnitTest.java diff --git a/core-java-modules/core-java-collections-set-2/src/test/java/com/baeldung/linkedhashset/LinkedHashSetUnitTest.java b/core-java-modules/core-java-collections-set-2/src/test/java/com/baeldung/linkedhashset/LinkedHashSetUnitTest.java new file mode 100644 index 0000000000..89b91ba16f --- /dev/null +++ b/core-java-modules/core-java-collections-set-2/src/test/java/com/baeldung/linkedhashset/LinkedHashSetUnitTest.java @@ -0,0 +1,141 @@ +package com.baeldung.linkedhashset; + +import org.junit.jupiter.api.Test; + +import java.util.*; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.junit.Assert.*; + + +public class LinkedHashSetUnitTest{ + + + @Test + void whenCreatingLinkedHashSet_shouldBeEmpty(){ + Set linkedHashSet = new LinkedHashSet<>(); + assertTrue(linkedHashSet.isEmpty()); + } + + @Test + void whenCreatingLinkedHashSetWithInitialCapacity_shouldBeEmpty(){ + LinkedHashSet linkedHashSet = new LinkedHashSet<>(20); + assertTrue(linkedHashSet.isEmpty()); + } + + @Test + void whenCreatingLinkedHashSetWithExistingCollection_shouldContainAllElementOfCollection(){ + Collection data = Arrays.asList("first", "second", "third", "fourth", "fifth"); + LinkedHashSet linkedHashSet = new LinkedHashSet<>(data); + + assertFalse(linkedHashSet.isEmpty()); + assertEquals(linkedHashSet.size(), data.size()); + assertTrue(linkedHashSet.containsAll(data) && data.containsAll(linkedHashSet)); + } + + @Test + void whenCreatingLinkedHashSetWithInitialCapacityAndLoadFactor_shouldBeEmpty(){ + LinkedHashSet linkedHashSet = new LinkedHashSet<>(20, 3); + assertTrue(linkedHashSet.isEmpty()); + } + + @Test + void whenAddingElement_shouldAddElement(){ + Set linkedHashSet = new LinkedHashSet<>(); + assertTrue(linkedHashSet.add(0)); + assertFalse(linkedHashSet.add(0)); + assertTrue(linkedHashSet.contains(0)); + + } + + @Test + void whenAddingCollection_shouldAddAllContentOfCollection(){ + Collection data = Arrays.asList(1,2,3); + LinkedHashSet linkedHashSet = new LinkedHashSet<>(); + + assertTrue(linkedHashSet.addAll(data)); + assertTrue(data.containsAll(linkedHashSet) && linkedHashSet.containsAll(data)); + } + + @Test + void whenAddingCollectionWithDuplicateElements_shouldMaintainUniqueValuesInSet(){ + LinkedHashSet linkedHashSet = new LinkedHashSet<>(); + linkedHashSet.add(2); + Collection data = Arrays.asList(1, 1, 2, 3); + + assertTrue(linkedHashSet.addAll(data)); + assertEquals(linkedHashSet.size(), 3); + assertTrue(data.containsAll(linkedHashSet) && linkedHashSet.containsAll(data)); + } + + @Test + void whenIteratingWithIterator_assertThatElementIsPresent(){ + LinkedHashSet linkedHashSet = new LinkedHashSet<>(); + linkedHashSet.add(0); + linkedHashSet.add(1); + linkedHashSet.add(2); + + Iterator iterator = linkedHashSet.iterator(); + for (int i = 0; i < linkedHashSet.size(); i++) { + int nextData = iterator.next(); + assertEquals(nextData, i); + } + } + + @Test + void whenIteratingWithSpliterator_assertThatElementIsPresent(){ + LinkedHashSet linkedHashSet = new LinkedHashSet<>(); + linkedHashSet.add(0); + linkedHashSet.add(1); + linkedHashSet.add(2); + + Spliterator spliterator = linkedHashSet.spliterator(); + AtomicInteger counter = new AtomicInteger(); + spliterator.forEachRemaining(data -> { + assertEquals((int)data, counter.get()); + counter.getAndIncrement(); + }); + } + + @Test + void whenRemovingAnElement_shouldRemoveElement(){ + Collection data = Arrays.asList("first", "second", "third", "fourth", "fifth"); + LinkedHashSet linkedHashSet = new LinkedHashSet<>(data); + + assertTrue(linkedHashSet.remove("second")); + assertFalse(linkedHashSet.contains("second")); + } + + @Test + void whenRemovingAnElementGreaterThanTwo_shouldRemoveElement(){ + LinkedHashSet linkedHashSet = new LinkedHashSet<>(); + linkedHashSet.add(0); + linkedHashSet.add(1); + linkedHashSet.add(2); + linkedHashSet.add(3); + linkedHashSet.add(4); + + linkedHashSet.removeIf(data -> data > 2); + assertFalse(linkedHashSet.contains(3)); + assertFalse(linkedHashSet.contains(4)); + } + + @Test + void whenRemovingAnElementWithIterator_shouldRemoveElement(){ + LinkedHashSet linkedHashSet = new LinkedHashSet<>(); + linkedHashSet.add(0); + linkedHashSet.add(1); + linkedHashSet.add(2); + + Iterator iterator = linkedHashSet.iterator(); + int elementToRemove = 1; + assertTrue(linkedHashSet.contains(elementToRemove)); + while(iterator.hasNext()){ + if(elementToRemove == iterator.next()){ + iterator.remove(); + } + } + assertFalse(linkedHashSet.contains(elementToRemove)); + } + +}