diff --git a/core-java/src/test/java/com/baeldung/collection/WhenUsingTreeSet.java b/core-java/src/test/java/com/baeldung/collection/WhenUsingTreeSet.java new file mode 100644 index 0000000000..48aff4d437 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/collection/WhenUsingTreeSet.java @@ -0,0 +1,105 @@ +package com.baeldung.collection; + +import java.util.ConcurrentModificationException; +import java.util.Iterator; +import java.util.Set; +import java.util.TreeSet; + +import org.junit.Assert; +import org.junit.Test; + +public class WhenUsingTreeSet { + + @Test + public void whenAddingElement_shouldAddElement() { + Set treeSet = new TreeSet<>(); + Assert.assertTrue(treeSet.add("String Added")); + } + + @Test + public void whenCheckingForElement_shouldSearchForElement() { + Set treeSetContains = new TreeSet<>(); + treeSetContains.add("String Added"); + Assert.assertTrue(treeSetContains.contains("String Added")); + } + + @Test + public void whenRemovingElement_shouldRemoveElement() { + Set removeFromTreeSet = new TreeSet<>(); + removeFromTreeSet.add("String Added"); + Assert.assertTrue(removeFromTreeSet.remove("String Added")); + } + + @Test + public void whenClearingTreeSet_shouldClearTreeSet() { + Set clearTreeSet = new TreeSet<>(); + clearTreeSet.add("String Added"); + clearTreeSet.clear(); + Assert.assertTrue(clearTreeSet.isEmpty()); + } + + @Test + public void whenCheckingTheSizeOfTreeSet_shouldReturnThesize() { + Set treeSetSize = new TreeSet<>(); + treeSetSize.add("String Added"); + Assert.assertEquals(1, treeSetSize.size()); + } + + @Test + public void whenCheckingForEmptyTreeSet_shouldCheckForEmpty() { + Set emptyTreeSet = new TreeSet<>(); + Assert.assertTrue(emptyTreeSet.isEmpty()); + } + + @Test + public void whenIteratingTreeSet_shouldIterateTreeSetInAscendingOrder() { + Set treeSet = new TreeSet<>(); + treeSet.add("First"); + treeSet.add("Second"); + treeSet.add("Third"); + Iterator itr = treeSet.iterator(); + while (itr.hasNext()) { + System.out.println(itr.next()); + } + } + + @Test + public void whenIteratingTreeSet_shouldIterateTreeSetInDescendingOrder() { + TreeSet treeSet = new TreeSet<>(); + treeSet.add("First"); + treeSet.add("Second"); + treeSet.add("Third"); + Iterator itr = treeSet.descendingIterator(); + while (itr.hasNext()) { + System.out.println(itr.next()); + } + } + + @Test(expected = ConcurrentModificationException.class) + public void whenModifyingTreeSetWhileIterating_shouldThrowException() { + Set treeSet = new TreeSet<>(); + treeSet.add("First"); + treeSet.add("Second"); + treeSet.add("Third"); + Iterator itr = treeSet.iterator(); + while (itr.hasNext()) { + itr.next(); + treeSet.remove("Second"); + } + } + + @Test + public void whenRemovingElementUsingIterator_shouldRemoveElement() { + Set treeSet = new TreeSet<>(); + treeSet.add("First"); + treeSet.add("Second"); + treeSet.add("Third"); + Iterator itr = treeSet.iterator(); + while (itr.hasNext()) { + String element = itr.next(); + if (element.equals("Second")) + itr.remove(); + } + Assert.assertEquals(2, treeSet.size()); + } +}