Skip to content

Commit c637c31

Browse files
Step1 (#2323)
* [feat] 스트림, 람다, Optional * comment --------- Co-authored-by: sophia.song(송지현)/kakao <[email protected]>
1 parent 7200b62 commit c637c31

File tree

7 files changed

+48
-48
lines changed

7 files changed

+48
-48
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package nextstep.fp;
2+
3+
@FunctionalInterface
4+
public interface Conditional {
5+
boolean check(Integer number);
6+
}

src/main/java/nextstep/fp/Lambda.java

+11-19
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,22 @@ public void run() {
2727
}
2828

2929
public static int sumAll(List<Integer> numbers) {
30-
int total = 0;
31-
for (int number : numbers) {
32-
total += number;
33-
}
34-
return total;
30+
return sumAll(numbers, n -> true);
3531
}
3632

3733
public static int sumAllEven(List<Integer> numbers) {
38-
int total = 0;
39-
for (int number : numbers) {
40-
if (number % 2 == 0) {
41-
total += number;
42-
}
43-
}
44-
return total;
34+
return sumAll(numbers, n -> n % 2 == 0);
4535
}
4636

4737
public static int sumAllOverThree(List<Integer> numbers) {
48-
int total = 0;
49-
for (int number : numbers) {
50-
if (number > 3) {
51-
total += number;
52-
}
53-
}
54-
return total;
38+
return sumAll(numbers, n -> n > 3);
5539
}
40+
41+
public static int sumAll(List<Integer> numbers, Conditional condition) {
42+
return numbers.stream()
43+
.filter(condition::check)
44+
.mapToInt(Integer::intValue)
45+
.sum();
46+
}
47+
5648
}

src/main/java/nextstep/fp/StreamStudy.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ public static void printLongestWordTop100() throws IOException {
2727
.get("src/main/resources/fp/war-and-peace.txt")), StandardCharsets.UTF_8);
2828
List<String> words = Arrays.asList(contents.split("[\\P{L}]+"));
2929

30-
// TODO 이 부분에 구현한다.
30+
Arrays.stream(contents.split("[\\P{L}]+"))
31+
.map(String::toLowerCase)
32+
.filter(word -> word.length() > 12)
33+
.distinct()
34+
.sorted((w1, w2) -> Integer.compare(w2.length(), w1.length()))
35+
.limit(100)
36+
.forEach(System.out::println);
3137
}
3238

3339
public static List<Integer> doubleNumbers(List<Integer> numbers) {
@@ -39,6 +45,9 @@ public static long sumAll(List<Integer> numbers) {
3945
}
4046

4147
public static long sumOverThreeAndDouble(List<Integer> numbers) {
42-
return 0;
48+
return numbers.stream()
49+
.filter(n -> n > 3)
50+
.map(n -> n * 2)
51+
.reduce(0, Integer::sum);
4352
}
44-
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package nextstep.optional;
22

3+
import java.util.Arrays;
4+
35
enum Expression {
46
PLUS("+"), MINUS("-"), TIMES("*"), DIVIDE("/");
57

@@ -14,12 +16,10 @@ private static boolean matchExpression(Expression e, String expression) {
1416
}
1517

1618
static Expression of(String expression) {
17-
for (Expression v : values()) {
18-
if (matchExpression(v, expression)) {
19-
return v;
20-
}
21-
}
22-
23-
throw new IllegalArgumentException(String.format("%s는 사칙연산에 해당하지 않는 표현식입니다.", expression));
19+
return Arrays.stream(values())
20+
.filter(e -> matchExpression(e, expression))
21+
.findFirst()
22+
.orElseThrow(() ->
23+
new IllegalArgumentException(String.format("%s는 사칙연산에 해당하지 않는 표현식입니다.", expression)));
2424
}
2525
}

src/main/java/nextstep/optional/User.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package nextstep.optional;
22

3+
import java.util.Optional;
4+
35
public class User {
46
private String name;
57
private Integer age;
@@ -33,7 +35,10 @@ public static boolean ageIsInRange1(User user) {
3335
}
3436

3537
public static boolean ageIsInRange2(User user) {
36-
return false;
38+
return Optional.ofNullable(user)
39+
.map(User::getAge)
40+
.filter(age -> age >= 30 && age <= 45)
41+
.isPresent();
3742
}
3843

3944
@Override

src/main/java/nextstep/optional/Users.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ public class Users {
1313
new User("honux", 45));
1414

1515
User getUser(String name) {
16-
for (User user : users) {
17-
if (user.matchName(name)) {
18-
return user;
19-
}
20-
}
21-
return DEFAULT_USER;
16+
return users.stream()
17+
.filter(user -> user.matchName(name))
18+
.findFirst()
19+
.orElse(DEFAULT_USER);
2220
}
2321
}

src/test/java/nextstep/fp/CarTest.java

+2-12
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,14 @@ public class CarTest {
88
@Test
99
public void 이동() {
1010
Car car = new Car("pobi", 0);
11-
Car actual = car.move(new MoveStrategy() {
12-
@Override
13-
public boolean isMovable() {
14-
return true;
15-
}
16-
});
11+
Car actual = car.move(() -> true);
1712
assertThat(actual).isEqualTo(new Car("pobi", 1));
1813
}
1914

2015
@Test
2116
public void 정지() {
2217
Car car = new Car("pobi", 0);
23-
Car actual = car.move(new MoveStrategy() {
24-
@Override
25-
public boolean isMovable() {
26-
return false;
27-
}
28-
});
18+
Car actual = car.move(() -> false);
2919
assertThat(actual).isEqualTo(new Car("pobi", 0));
3020
}
3121
}

0 commit comments

Comments
 (0)