From 80f0c86aa3da74b8e7a473b5ea12246586952761 Mon Sep 17 00:00:00 2001 From: Mo Helmy <135069400+BenHelmyBen@users.noreply.github.com> Date: Mon, 23 Oct 2023 05:12:19 +0300 Subject: [PATCH] This commit is related to the article BAEL-7089 (#15037) This commit aims to add a test class named "RemoveQueueElementsUnitTest" that provides code snippets for removing elements from a queue. --- .../RemoveQueueElementsUnitTest.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 core-java-modules/core-java-collections-5/src/test/java/com/baeldung/removequeueelements/RemoveQueueElementsUnitTest.java diff --git a/core-java-modules/core-java-collections-5/src/test/java/com/baeldung/removequeueelements/RemoveQueueElementsUnitTest.java b/core-java-modules/core-java-collections-5/src/test/java/com/baeldung/removequeueelements/RemoveQueueElementsUnitTest.java new file mode 100644 index 0000000000..66cfade866 --- /dev/null +++ b/core-java-modules/core-java-collections-5/src/test/java/com/baeldung/removequeueelements/RemoveQueueElementsUnitTest.java @@ -0,0 +1,60 @@ +package com.baeldung.removequeueelements; + +import org.junit.Test; + +import java.util.LinkedList; +import java.util.Queue; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class RemoveQueueElementsUnitTest { + @Test + public void givenQueueWithEvenAndOddNumbers_whenRemovingEvenNumbers_thenOddNumbersRemain() { + Queue queue = new LinkedList<>(); + Queue evenElementsQueue = new LinkedList<>(); + queue.add(1); + queue.add(2); + queue.add(3); + queue.add(4); + queue.add(5); + + while (queue.peek() != null) { + int element = queue.remove(); + if (element % 2 != 0) { + evenElementsQueue.add(element); + } + } + + assertEquals(3, evenElementsQueue.size()); + assertTrue(evenElementsQueue.contains(1)); + assertTrue(evenElementsQueue.contains(3)); + assertTrue(evenElementsQueue.contains(5)); + } + + @Test + public void givenStringQueue_whenRemovingStringsThatStartWithA_thenStringElementsRemain() { + Queue queue = new LinkedList<>(); + Queue stringElementsQueue = new LinkedList<>(); + queue.add("Apple"); + queue.add("Banana"); + queue.add("Orange"); + queue.add("Grape"); + queue.add("Mango"); + + + while (queue.peek() != null) { + String element = queue.remove(); + if (!element.startsWith("A")) { + stringElementsQueue.add(element); + } + } + + assertEquals(4, stringElementsQueue.size()); + assertTrue(stringElementsQueue.contains("Banana")); + assertTrue(stringElementsQueue.contains("Orange")); + assertTrue(stringElementsQueue.contains("Grape")); + assertTrue(stringElementsQueue.contains("Mango")); + } + +}