GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [last-iteration] detect the last iteration (#15414) · edizor/tutorials@6a9b005

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • core-java-modules
    • core-java-loops
      • README.md
      • pom.xml
      • src/test/java/com/baeldung/loops
        • TheLastIterationInForEachUnitTest.java
    • pom.xml
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎core-java-modules/core-java-loops/README.md‎

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## Relevant Articles
Collapse file

‎core-java-modules/core-java-loops/pom.xml‎

Copy file name to clipboard
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>core-java-loops</artifactId>
7+
<name>core-java-loops</name>
8+
<packaging>jar</packaging>
9+
10+
<parent>
11+
<groupId>com.baeldung.core-java-modules</groupId>
12+
<artifactId>core-java-modules</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
</parent>
15+
16+
</project>
Collapse file

‎core-java-modules/core-java-loops/src/test/java/com/baeldung/loops/TheLastIterationInForEachUnitTest.java‎

Copy file name to clipboard
+93Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.baeldung.loops;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import java.util.HashMap;
7+
import java.util.Iterator;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.stream.IntStream;
11+
12+
import org.junit.jupiter.api.Test;
13+
14+
public class TheLastIterationInForEachUnitTest {
15+
16+
//@formatter:off
17+
private static final List<String> MOVIES = List.of(
18+
"Titanic",
19+
"The Deer Hunter",
20+
"Lord of the Rings",
21+
"One Flew Over the Cuckoo's Nest",
22+
"No Country For Old Men");
23+
//@formatter:on
24+
25+
@Test
26+
void whenUsingForEach_thenGetTheLastElementAfterTheLoop() {
27+
String myLastMovie = "";
28+
for (String movie : MOVIES) {
29+
// ... work with movie
30+
myLastMovie = movie;
31+
}
32+
assertEquals("No Country For Old Men", myLastMovie);
33+
}
34+
35+
@Test
36+
void whenLoopingWithIndexes_thenGetExpectedResult() {
37+
int size = MOVIES.size();
38+
String myLastMovie = null;
39+
for (int i = 0; i < size; i++) {
40+
String movie = MOVIES.get(i);
41+
// ... work with movie
42+
if (i == size - 1) {
43+
myLastMovie = movie;
44+
}
45+
}
46+
assertEquals("No Country For Old Men", myLastMovie);
47+
}
48+
49+
@Test
50+
void whenUsingIntStream_thenGetExpectedResult() {
51+
int size = MOVIES.size();
52+
final Map<Integer, String> myLastMovie = new HashMap<>();
53+
IntStream.range(0, size)
54+
.forEach(idx -> {
55+
String movie = MOVIES.get(idx);
56+
// ... work with movie
57+
if (idx == size - 1) {
58+
myLastMovie.put(idx, movie);
59+
}
60+
});
61+
assertEquals(1, myLastMovie.size());
62+
assertTrue(myLastMovie.containsKey(size - 1));
63+
assertTrue(myLastMovie.containsValue("No Country For Old Men"));
64+
}
65+
66+
@Test
67+
void whenUsingCounter_thenGetExpectedResult() {
68+
int size = MOVIES.size();
69+
String myLastMovie = null;
70+
int cnt = 0;
71+
for (String movie : MOVIES) {
72+
// ... work with movie
73+
if (++cnt == size) {
74+
myLastMovie = movie;
75+
}
76+
}
77+
assertEquals("No Country For Old Men", myLastMovie);
78+
}
79+
80+
@Test
81+
void whenUsingIterator_thenGetExpectedResult() {
82+
String movie;
83+
String myLastMovie = null;
84+
for (Iterator<String> it = MOVIES.iterator(); it.hasNext(); ) {
85+
movie = it.next();
86+
// ... work with movie
87+
if (!it.hasNext()) { // the last element
88+
myLastMovie = movie;
89+
}
90+
}
91+
assertEquals("No Country For Old Men", myLastMovie);
92+
}
93+
}
Collapse file

‎core-java-modules/pom.xml‎

Copy file name to clipboardExpand all lines: core-java-modules/pom.xml
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
<module>core-java-lang-syntax</module>
156156
<module>core-java-lang-syntax-2</module>
157157
<module>core-java-locale</module>
158+
<module>core-java-loops</module>
158159
<module>core-java-networking</module>
159160
<module>core-java-networking-2</module>
160161
<module>core-java-networking-4</module>

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.