adding new article BAEL-7497 source codes

This commit is contained in:
hajk1 2024-04-03 01:32:44 +03:30
parent a61328e328
commit 1864b8e90d
4 changed files with 131 additions and 0 deletions

View File

@ -0,0 +1,7 @@
=========
## Core Java Collections Cookbooks and Examples
### Relevant Articles:
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-5)

View File

@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<artifactId>core-java-collections-6</artifactId>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
<groupId>org.apache.maven.plugins</groupId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<artifactId>junit-platform-runner</artifactId>
<groupId>org.junit.platform</groupId>
<scope>test</scope>
<version>${junit-platform.version}</version>
</dependency>
<dependency>
<artifactId>junit-jupiter</artifactId>
<groupId>org.junit.jupiter</groupId>
<scope>test</scope>
<version>${junit.version}</version>
</dependency>
<dependency>
<artifactId>junit-vintage-engine</artifactId>
<groupId>org.junit.vintage</groupId>
<scope>test</scope>
<version>${junit.version}</version>
</dependency>
</dependencies>
<modelVersion>4.0.0</modelVersion>
<name>core-java-collections-6</name>
<packaging>jar</packaging>
<parent>
<artifactId>core-java-modules</artifactId>
<groupId>com.baeldung.core-java-modules</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<junit.version>5.10.2</junit.version>
</properties>
</project>

View File

@ -0,0 +1,69 @@
package com.baeldung.iteratorvsforeach;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
class IteratorVsForeachUnitTest {
private static Stream<Arguments> listProvider() {
return Stream.of(
Arguments.of(
List.of("String1", "String2", "unwanted"),
List.of("String1", "String2"))
);
}
@Test
public void givenEmptyCollection_whenUsingForEach_thenNoElementsAreIterated() {
List<String> names = Collections.emptyList();
StringBuilder stringBuilder = new StringBuilder();
names.forEach(stringBuilder::append);
assertEquals("", stringBuilder.toString());
}
@ParameterizedTest
@MethodSource("listProvider")
public void givenCollectionWithElements_whenRemovingElementDuringForEachIteration_thenElementIsRemoved(
List<String> input, List<String> expected) {
List<String> mutableList = new ArrayList<>(input);
// Separate collection for items to be removed
List<String> toRemove = new ArrayList<>();
// Using forEach to identify items to remove
input.forEach(item -> {
if (item.equals("unwanted")) {
toRemove.add(item);
}
});
// Removing the identified items from the original list
mutableList.removeAll(toRemove);
assertIterableEquals(expected, mutableList);
}
@ParameterizedTest
@MethodSource("listProvider")
public void givenCollectionWithElements_whenRemovingElementDuringIteratorIteration_thenElementIsRemoved(
List<String> input, List<String> expected) {
List<String> mutableList = new ArrayList<>(input);
Iterator<String> it = mutableList.iterator();
while (it.hasNext()) {
String item = it.next();
if (item.equals("unwanted")) {
it.remove(); // Safely remove item
}
}
assertIterableEquals(expected, mutableList);
}
}

View File

@ -89,6 +89,7 @@
<module>core-java-collections-3</module>
<module>core-java-collections-4</module>
<module>core-java-collections-5</module>
<module>core-java-collections-6</module>
<module>core-java-collections-conversions</module>
<module>core-java-collections-set-2</module>
<module>core-java-collections-list</module>