Skip to content

Commit bae5366

Browse files
Add example code (#18376)
1 parent adebbb8 commit bae5366

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
void main() {
2+
3+
System.out.println("Basics:");
4+
showBasicBehaviour();
5+
6+
// ---
7+
8+
List<List<String>> listOfLists = List.of((List.of("here")));
9+
10+
var findByBreak = findByBreak(listOfLists, "target");
11+
System.out.println("findByBreak: " + findByBreak);
12+
13+
var findByStreams = findByStreams(listOfLists, "target");
14+
System.out.println("findByStreams: " + findByStreams);
15+
}
16+
17+
private void showBasicBehaviour() {
18+
outer:
19+
// <-- // label
20+
for (int i = 0; i < 5; i++) {
21+
for (int j = 0; j < 5; j++) {
22+
System.out.println(i + ", " + j);
23+
if (j == 2) {
24+
break outer; // exits outer loop
25+
}
26+
}
27+
}
28+
}
29+
30+
private boolean findByBreak(List<List<String>> listOfLists, String target) {
31+
boolean containsExists = false;
32+
33+
outer:
34+
for (List<String> parent : listOfLists) {
35+
for (String child : parent) {
36+
if (child.contains(target)) {
37+
containsExists = true;
38+
break outer;
39+
}
40+
}
41+
}
42+
43+
return containsExists;
44+
}
45+
46+
private boolean findByStreams(List<List<String>> listOfLists, String target) {
47+
return listOfLists.stream()
48+
.flatMap(Collection::stream)
49+
.anyMatch(child -> child.contains(target));
50+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.javafeatures
2+
3+
fun main() {
4+
outer@ for (i in 1..5) {
5+
for (j in 1..5) {
6+
if (j == 2) break@outer
7+
println("$i, $j")
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)