Skip to content

Commit 93ecfad

Browse files
thakursantoshzhendrikse
authored andcommitted
Code for BAEL-1306 - thakursantosh/[email protected] (eugenp#2940)
* New code for First Article 'Types of Bean Injection' * Adding code for BAEL-1306 * Code changes for BAEL-1306 * Removed code of my evaluation article
1 parent c65795c commit 93ecfad

2 files changed

Lines changed: 177 additions & 0 deletions

File tree

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package com.baeldung.breakcontinue;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.Iterator;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Map.Entry;
9+
10+
/**
11+
* @author Santosh
12+
*
13+
*/
14+
public class BreakContinue {
15+
16+
public static int unlabeledBreak() {
17+
String searchName = "Wilson";
18+
int counter = 0;
19+
List<String> names = Arrays.asList("John", "Peter", "Robert", "Wilson", "Anthony", "Donald", "Richard");
20+
21+
for (String name : names) {
22+
counter++;
23+
if (name.equalsIgnoreCase(searchName)) {
24+
break;
25+
}
26+
}
27+
28+
return counter;
29+
}
30+
31+
public static int unlabeledBreakNestedLoops() {
32+
String searchName = "Wilson";
33+
int counter = 0;
34+
Map<String, List<String>> nameMap = new HashMap<>();
35+
nameMap.put("Grade1", Arrays.asList("John", "Peter", "Robert", "Wilson"));
36+
nameMap.put("Grade2", Arrays.asList("Anthony", "Donald", "Richard", "Arnold"));
37+
nameMap.put("Grade3", Arrays.asList("Wilson", "Michael", "Stephen", "Ryan"));
38+
39+
Iterator<Entry<String, List<String>>> iterator = nameMap.entrySet()
40+
.iterator();
41+
Entry<String, List<String>> entry = null;
42+
List<String> names = null;
43+
while (iterator.hasNext()) {
44+
entry = iterator.next();
45+
names = entry.getValue();
46+
for (String name : names) {
47+
if (name.equalsIgnoreCase(searchName)) {
48+
counter++;
49+
break;
50+
}
51+
}
52+
}
53+
54+
return counter;
55+
}
56+
57+
public static int labeledBreak() {
58+
String searchName = "Wilson";
59+
int counter = 0;
60+
Map<String, List<String>> nameMap = new HashMap<>();
61+
nameMap.put("Grade1", Arrays.asList("John", "Peter", "Robert", "Wilson"));
62+
nameMap.put("Grade2", Arrays.asList("Anthony", "Donald", "Richard", "Arnold"));
63+
nameMap.put("Grade3", Arrays.asList("Wilson", "Michael", "Stephen", "Ryan"));
64+
65+
Iterator<Entry<String, List<String>>> iterator = nameMap.entrySet()
66+
.iterator();
67+
Entry<String, List<String>> entry = null;
68+
List<String> names = null;
69+
compare:
70+
while (iterator.hasNext()) {
71+
entry = iterator.next();
72+
names = entry.getValue();
73+
for (String name : names) {
74+
if (name.equalsIgnoreCase(searchName)) {
75+
counter++;
76+
break compare;
77+
}
78+
}
79+
}
80+
81+
return counter;
82+
}
83+
84+
public static int unlabeledContinue() {
85+
String searchName = "Wilson";
86+
int counter = 0;
87+
Map<String, List<String>> nameMap = new HashMap<>();
88+
nameMap.put("Grade1", Arrays.asList("John", "Wilson", "Robert", "Wilson"));
89+
nameMap.put("Grade2", Arrays.asList("Anthony", "Donald", "Wilson", "Arnold"));
90+
nameMap.put("Grade3", Arrays.asList("Wilson", "Michael", "Wilson", "Ryan"));
91+
92+
Iterator<Entry<String, List<String>>> iterator = nameMap.entrySet()
93+
.iterator();
94+
Entry<String, List<String>> entry = null;
95+
List<String> names = null;
96+
while (iterator.hasNext()) {
97+
entry = iterator.next();
98+
names = entry.getValue();
99+
for (String name : names) {
100+
if (!name.equalsIgnoreCase(searchName)) {
101+
continue;
102+
}
103+
104+
counter++;
105+
}
106+
}
107+
108+
return counter;
109+
}
110+
111+
public static int labeledContinue() {
112+
String searchName = "Wilson";
113+
int counter = 0;
114+
Map<String, List<String>> nameMap = new HashMap<>();
115+
nameMap.put("Grade1", Arrays.asList("John", "Wilson", "Robert", "Wilson"));
116+
nameMap.put("Grade2", Arrays.asList("Anthony", "Donald", "Wilson", "Arnold"));
117+
nameMap.put("Grade3", Arrays.asList("Wilson", "Michael", "Wilson", "Ryan"));
118+
119+
Iterator<Entry<String, List<String>>> iterator = nameMap.entrySet()
120+
.iterator();
121+
Entry<String, List<String>> entry = null;
122+
List<String> names = null;
123+
compare:
124+
while (iterator.hasNext()) {
125+
entry = iterator.next();
126+
names = entry.getValue();
127+
for (String name : names) {
128+
if (name.equalsIgnoreCase(searchName)) {
129+
counter++;
130+
continue compare;
131+
}
132+
}
133+
}
134+
135+
return counter;
136+
}
137+
138+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.breakcontinue;
2+
3+
import static com.baeldung.breakcontinue.BreakContinue.labeledBreak;
4+
import static com.baeldung.breakcontinue.BreakContinue.labeledContinue;
5+
import static com.baeldung.breakcontinue.BreakContinue.unlabeledBreak;
6+
import static com.baeldung.breakcontinue.BreakContinue.unlabeledBreakNestedLoops;
7+
import static com.baeldung.breakcontinue.BreakContinue.unlabeledContinue;
8+
import static org.junit.Assert.assertEquals;
9+
10+
import org.junit.Test;
11+
12+
public class BreakContinueTest {
13+
14+
@Test
15+
public void whenUnlabeledBreak_ThenEqual() {
16+
assertEquals(4, unlabeledBreak());
17+
}
18+
19+
@Test
20+
public void whenUnlabeledBreakNestedLoops_ThenEqual() {
21+
assertEquals(2, unlabeledBreakNestedLoops());
22+
}
23+
24+
@Test
25+
public void whenLabeledBreak_ThenEqual() {
26+
assertEquals(1, labeledBreak());
27+
}
28+
29+
@Test
30+
public void whenUnlabeledContinue_ThenEqual() {
31+
assertEquals(5, unlabeledContinue());
32+
}
33+
34+
@Test
35+
public void whenLabeledContinue_ThenEqual() {
36+
assertEquals(3, labeledContinue());
37+
}
38+
39+
}

0 commit comments

Comments
 (0)