[JAVA-31848] Split-or-move-core-java-collections-5 (#16482)
This commit is contained in:
-57
@@ -1,57 +0,0 @@
|
||||
package com.baeldung.checkiflistcontainsenum;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class CheckIfListContainsEnumUnitTest {
|
||||
private final List<Map<String, Object>> data = new ArrayList<>();
|
||||
|
||||
public CheckIfListContainsEnumUnitTest() {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("Name", "John");
|
||||
map.put("Age", 25);
|
||||
map.put("Position", Position.DEVELOPER);
|
||||
|
||||
data.add(map);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDataList_whenUsingLoop_thenCheckIfListContainsEnum() {
|
||||
boolean containsEnumValue = false;
|
||||
|
||||
for (Map<String, Object> entry : data) {
|
||||
Object positionValue = entry.get("Position");
|
||||
if (Arrays.asList(Position.values()).contains(positionValue)) {
|
||||
containsEnumValue = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Assert.assertTrue(containsEnumValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDataList_whenUsingStream_thenCheckIfListContainsEnum() {
|
||||
boolean containsEnumValue = data.stream()
|
||||
.map(entry -> entry.get("Position"))
|
||||
.anyMatch(position -> Arrays.asList(Position.values()).contains(position));
|
||||
|
||||
Assert.assertTrue(containsEnumValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDataList_whenUsingDisjointMethod_thenCheckIfListContainsEnum() {
|
||||
List<Position> positionValues = data.stream()
|
||||
.map(entry -> (Position) entry.get("Position"))
|
||||
.toList();
|
||||
|
||||
boolean containsEnumValue = !Collections.disjoint(Arrays.asList(Position.values()), positionValues);
|
||||
Assert.assertTrue(containsEnumValue);
|
||||
}
|
||||
|
||||
public enum Position {
|
||||
DEVELOPER, MANAGER, ANALYST
|
||||
}
|
||||
}
|
||||
-60
@@ -1,60 +0,0 @@
|
||||
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<Integer> queue = new LinkedList<>();
|
||||
Queue<Integer> oddElementsQueue = 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) {
|
||||
oddElementsQueue.add(element);
|
||||
}
|
||||
}
|
||||
|
||||
assertEquals(3, oddElementsQueue.size());
|
||||
assertTrue(oddElementsQueue.contains(1));
|
||||
assertTrue(oddElementsQueue.contains(3));
|
||||
assertTrue(oddElementsQueue.contains(5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringQueue_whenRemovingStringsThatStartWithA_thenStringElementsRemain() {
|
||||
Queue<String> queue = new LinkedList<>();
|
||||
Queue<String> 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"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user