[last-iteration] detect the last iteration (#15414)

* [last-iteration] detect the last iteration

* [last-iteration] add core-java-loops module
This commit is contained in:
Kai Yuan 2023-12-16 00:47:36 +01:00 committed by GitHub
parent b04c7afcf5
commit 72b6c78646
4 changed files with 111 additions and 0 deletions

View File

@ -0,0 +1 @@
## Relevant Articles

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-loops</artifactId>
<name>core-java-loops</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,93 @@
package com.baeldung.loops;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;
import org.junit.jupiter.api.Test;
public class TheLastIterationInForEachUnitTest {
//@formatter:off
private static final List<String> MOVIES = List.of(
"Titanic",
"The Deer Hunter",
"Lord of the Rings",
"One Flew Over the Cuckoo's Nest",
"No Country For Old Men");
//@formatter:on
@Test
void whenUsingForEach_thenGetTheLastElementAfterTheLoop() {
String myLastMovie = "";
for (String movie : MOVIES) {
// ... work with movie
myLastMovie = movie;
}
assertEquals("No Country For Old Men", myLastMovie);
}
@Test
void whenLoopingWithIndexes_thenGetExpectedResult() {
int size = MOVIES.size();
String myLastMovie = null;
for (int i = 0; i < size; i++) {
String movie = MOVIES.get(i);
// ... work with movie
if (i == size - 1) {
myLastMovie = movie;
}
}
assertEquals("No Country For Old Men", myLastMovie);
}
@Test
void whenUsingIntStream_thenGetExpectedResult() {
int size = MOVIES.size();
final Map<Integer, String> myLastMovie = new HashMap<>();
IntStream.range(0, size)
.forEach(idx -> {
String movie = MOVIES.get(idx);
// ... work with movie
if (idx == size - 1) {
myLastMovie.put(idx, movie);
}
});
assertEquals(1, myLastMovie.size());
assertTrue(myLastMovie.containsKey(size - 1));
assertTrue(myLastMovie.containsValue("No Country For Old Men"));
}
@Test
void whenUsingCounter_thenGetExpectedResult() {
int size = MOVIES.size();
String myLastMovie = null;
int cnt = 0;
for (String movie : MOVIES) {
// ... work with movie
if (++cnt == size) {
myLastMovie = movie;
}
}
assertEquals("No Country For Old Men", myLastMovie);
}
@Test
void whenUsingIterator_thenGetExpectedResult() {
String movie;
String myLastMovie = null;
for (Iterator<String> it = MOVIES.iterator(); it.hasNext(); ) {
movie = it.next();
// ... work with movie
if (!it.hasNext()) { // the last element
myLastMovie = movie;
}
}
assertEquals("No Country For Old Men", myLastMovie);
}
}

View File

@ -155,6 +155,7 @@
<module>core-java-lang-syntax</module> <module>core-java-lang-syntax</module>
<module>core-java-lang-syntax-2</module> <module>core-java-lang-syntax-2</module>
<module>core-java-locale</module> <module>core-java-locale</module>
<module>core-java-loops</module>
<module>core-java-networking</module> <module>core-java-networking</module>
<module>core-java-networking-2</module> <module>core-java-networking-2</module>
<module>core-java-networking-4</module> <module>core-java-networking-4</module>