-
Notifications
You must be signed in to change notification settings - Fork 1
1️⃣ 자바8 스트림, 람다, Optional #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: this-is-spear
Are you sure you want to change the base?
Changes from 7 commits
cef9213
a9888c3
6eac39f
bcd9140
d57d1ac
cac2d45
4434455
5e0a84b
083322b
4096487
6ca54c8
486d574
7d783ea
eae04b8
c675df9
fac6f7a
16ddf56
ab45912
078122a
1d26d8f
a4e2280
eca06c8
2b39e03
656e032
ce9ffa2
b72fba9
4ff7092
3c1fc7b
bce2d18
b045fed
9c1b9af
d3483ac
e26737b
f5942ef
d7155f0
9d94f14
a2cdc07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package nextstep.fp; | ||
|
|
||
| public interface Condition { | ||
| boolean condition(Integer number); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,53 +4,51 @@ | |
|
|
||
| public class Lambda { | ||
| public static void printAllOld(List<Integer> numbers) { | ||
| System.out.println("printAllOld"); | ||
|
|
||
| for (int number : numbers) { | ||
| System.out.println(number); | ||
| } | ||
| print("printAllOld"); | ||
| numbers.forEach(System.out::println); | ||
| } | ||
|
|
||
| public static void printAllLambda(List<Integer> numbers) { | ||
| System.out.println("printAllLambda"); | ||
|
|
||
| print("printAllLambda"); | ||
| numbers.forEach(System.out::println); | ||
| } | ||
|
|
||
| public static void runThread() { | ||
| new Thread(new Runnable() { | ||
| @Override | ||
| public void run() { | ||
| System.out.println("Hello from thread"); | ||
| } | ||
| }).start(); | ||
| new Thread(() -> print("Hello from thread")).start(); | ||
| } | ||
|
|
||
|
|
||
| public static int sumAll(List<Integer> numbers) { | ||
| int total = 0; | ||
| for (int number : numbers) { | ||
| total += number; | ||
| } | ||
| return total; | ||
| return sumAll(numbers, Lambda::getAll); | ||
| } | ||
|
|
||
| public static int sumAllEven(List<Integer> numbers) { | ||
| int total = 0; | ||
| for (int number : numbers) { | ||
| if (number % 2 == 0) { | ||
| total += number; | ||
| } | ||
| } | ||
| return total; | ||
| return sumAll(numbers, Lambda::getEven); | ||
| } | ||
|
|
||
| public static int sumAllOverThree(List<Integer> numbers) { | ||
| int total = 0; | ||
| for (int number : numbers) { | ||
| if (number > 3) { | ||
| total += number; | ||
| } | ||
| } | ||
| return total; | ||
| return sumAll(numbers, Lambda::getOverThree); | ||
| } | ||
|
|
||
| private static int sumAll(List<Integer> numbers, Condition condition) { | ||
| return numbers.stream() | ||
| .filter(condition::condition) | ||
| .reduce(Integer::sum).orElse(0); | ||
| } | ||
|
|
||
| private static void print(String Hello_from_thread) { | ||
|
||
| System.out.println(Hello_from_thread); | ||
| } | ||
|
|
||
| private static boolean getAll(Integer integer) { | ||
| return true; | ||
| } | ||
|
|
||
| private static boolean getEven(Integer integer) { | ||
| return integer % 2 == 0; | ||
| } | ||
|
|
||
| private static boolean getOverThree(Integer integer) { | ||
| return integer > 3; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,15 +4,20 @@ | |
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Paths; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Comparator; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class StreamStudy { | ||
|
|
||
| public static long countWords() throws IOException { | ||
| String contents = new String(Files.readAllBytes(Paths | ||
| .get("src/main/resources/fp/war-and-peace.txt")), StandardCharsets.UTF_8); | ||
| .get("src/main/resources/fp/war-and-peace.txt")), StandardCharsets.UTF_8); | ||
| List<String> words = Arrays.asList(contents.split("[\\P{L}]+")); | ||
|
|
||
| long count = 0; | ||
|
|
@@ -24,21 +29,40 @@ public static long countWords() throws IOException { | |
|
|
||
| public static void printLongestWordTop100() throws IOException { | ||
| String contents = new String(Files.readAllBytes(Paths | ||
| .get("src/main/resources/fp/war-and-peace.txt")), StandardCharsets.UTF_8); | ||
| .get("src/main/resources/fp/war-and-peace.txt")), StandardCharsets.UTF_8); | ||
| List<String> words = Arrays.asList(contents.split("[\\P{L}]+")); | ||
|
|
||
| // TODO 이 부분에 구현한다. | ||
|
|
||
| Map<String, Integer> wordMap = new HashMap<>(); | ||
|
|
||
| for (String word : words) { | ||
|
||
| if (!wordMap.containsKey(word)) { | ||
| wordMap.put(word, 0); | ||
| } | ||
| wordMap.put(word, wordMap.get(word) + 1); | ||
| } | ||
|
|
||
| List<String> collect = wordMap.entrySet() | ||
| .stream().filter(entry -> entry.getValue() > 12) | ||
| .sorted(Comparator.comparing(entry -> - entry.getValue())) | ||
| .map(entry -> entry.getKey().toLowerCase()) | ||
| .distinct() | ||
| .collect(Collectors.toList()); | ||
|
|
||
| List<String> subList = new ArrayList<>(collect.subList(0, 100)); | ||
| System.out.println(subList); | ||
| System.out.println(subList.size()); | ||
| } | ||
|
|
||
| public static List<Integer> doubleNumbers(List<Integer> numbers) { | ||
| return numbers.stream().map(x -> 2 * x).collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public static long sumAll(List<Integer> numbers) { | ||
| return numbers.stream().reduce(0, (x, y) -> x + y); | ||
| return numbers.stream().reduce(0, Integer::sum); | ||
| } | ||
|
|
||
| public static long sumOverThreeAndDouble(List<Integer> numbers) { | ||
| return 0; | ||
| return Lambda.sumAllOverThree(numbers) * 2L; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| package nextstep.optional; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| enum Expression { | ||
| PLUS("+"), MINUS("-"), TIMES("*"), DIVIDE("/"); | ||
|
|
||
|
|
@@ -14,12 +16,11 @@ private static boolean matchExpression(Expression e, String expression) { | |
| } | ||
|
|
||
| static Expression of(String expression) { | ||
| for (Expression v : values()) { | ||
| if (matchExpression(v, expression)) { | ||
| return v; | ||
| } | ||
| } | ||
|
|
||
| throw new IllegalArgumentException(String.format("%s는 사칙연산에 해당하지 않는 표현식입니다.", expression)); | ||
| return Arrays.stream(values()) | ||
| .filter(expression1 -> matchExpression(expression1, expression)) | ||
| .findFirst() | ||
| .orElseThrow( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 줄 구분을 하니 깔끔하게 읽히네요 |
||
| () -> new IllegalArgumentException(String.format("%s는 사칙연산에 해당하지 않는 표현식입니다.", expression)) | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| package nextstep.optional; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public class User { | ||
| private String name; | ||
| private Integer age; | ||
|
|
@@ -25,15 +27,15 @@ public static boolean ageIsInRange1(User user) { | |
| boolean isInRange = false; | ||
|
|
||
| if (user != null && user.getAge() != null | ||
| && (user.getAge() >= 30 | ||
| && user.getAge() <= 45)) { | ||
| && (user.getAge() >= 30 | ||
| && user.getAge() <= 45)) { | ||
| isInRange = true; | ||
| } | ||
| return isInRange; | ||
| } | ||
|
|
||
| public static boolean ageIsInRange2(User user) { | ||
| return false; | ||
| return user != null && user.getAge() != null && (user.getAge() >= 30 && user.getAge() <= 45); | ||
|
||
| } | ||
|
|
||
| @Override | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| public class Users { | ||
| static final User DEFAULT_USER = new User("codesquad", 100); | ||
|
|
@@ -12,12 +13,7 @@ public class Users { | |
| new User("jk", 40), | ||
| new User("honux", 45)); | ||
|
|
||
| User getUser(String name) { | ||
| for (User user : users) { | ||
| if (user.matchName(name)) { | ||
| return user; | ||
| } | ||
| } | ||
| return DEFAULT_USER; | ||
| Optional<User> getUser(String name) { | ||
| return users.stream().filter(user -> user.matchName(name)).findFirst(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. orElse(DEFAULT_USER)가 이부분에 구현되어야 하지 않나요?!
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional로 값을 리턴해 NUll 값을 허용했는데, Users에서 Optional을 넘기는 방식이 옳은건지 궁금해지네요! |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
condition을 직접 주지 않고 메소드로 분리해 사용하셨군요! 분리하니 확장에 유리한 코드가 된 것 같습니다. 저도 참고하겠습니다 :)