[JAVA-31848] Split-or-move-core-java-collections-5 (#16482)
This commit is contained in:
parent
3088ff9e3b
commit
0bc686a11e
|
@ -7,10 +7,8 @@
|
|||
- [Creating Custom Iterator in Java](https://www.baeldung.com/java-creating-custom-iterator)
|
||||
- [Difference Between Arrays.sort() and Collections.sort()](https://www.baeldung.com/java-arrays-collections-sort-methods)
|
||||
- [Skipping the First Iteration in Java](https://www.baeldung.com/java-skip-first-iteration)
|
||||
- [Remove Elements From a Queue Using Loop](https://www.baeldung.com/java-remove-elements-queue)
|
||||
- [Intro to Vector Class in Java](https://www.baeldung.com/java-vector-guide)
|
||||
- [Time Complexity of Java Collections Sort in Java](https://www.baeldung.com/java-time-complexity-collections-sort)
|
||||
- [Check if List Contains at Least One Enum](https://www.baeldung.com/java-list-check-enum-presence)
|
||||
- [Comparison of for Loops and Iterators](https://www.baeldung.com/java-for-loops-vs-iterators)
|
||||
- [PriorityQueue iterator() Method in Java](https://www.baeldung.com/java-priorityqueue-iterator)
|
||||
- [Immutable vs Unmodifiable Collection in Java](https://www.baeldung.com/java-collection-immutable-unmodifiable-differences)
|
||||
|
|
|
@ -4,4 +4,7 @@
|
|||
|
||||
### Relevant Articles:
|
||||
- [Iterator vs forEach() in Java](https://www.baeldung.com/java-iterator-vs-foreach)
|
||||
- [Remove Elements From a Queue Using Loop](https://www.baeldung.com/java-remove-elements-queue)
|
||||
- [Check if List Contains at Least One Enum](https://www.baeldung.com/java-list-check-enum-presence)
|
||||
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-5)
|
||||
|
|
|
@ -1,57 +1,57 @@
|
|||
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
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
|
@ -1,60 +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<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"));
|
||||
}
|
||||
|
||||
}
|
||||
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"));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue