File tree Expand file tree Collapse file tree 2 files changed +60
-0
lines changed
core-java-modules/core-java-23/src/main/java/com/baeldung/javafeatures Expand file tree Collapse file tree 2 files changed +60
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments