-
Notifications
You must be signed in to change notification settings - Fork 1
feat: apply lambda, stream, Optional #1
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: oliviarla
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -27,30 +27,23 @@ public void run() { | |
| } | ||
|
|
||
| public static int sumAll(List<Integer> numbers) { | ||
| int total = 0; | ||
| for (int number : numbers) { | ||
| total += number; | ||
| } | ||
| return total; | ||
| return numbers.stream().reduce(Integer::sum).get(); | ||
| } | ||
|
|
||
| public interface Conditional { | ||
| boolean test(Integer number); | ||
| } | ||
|
|
||
| public static int sumAll(List<Integer> numbers, | ||
| Conditional c) { | ||
| // c.test(number)를 활용해 구현할 수 있다. | ||
| return numbers.stream().filter(c::test).reduce(Integer::sum).get(); | ||
| } | ||
| 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, (integer)->{return integer%2==0;}); | ||
| } | ||
|
|
||
| public static int sumAllOverThree(List<Integer> numbers) { | ||
| int total = 0; | ||
| for (int number : numbers) { | ||
| if (number > 3) { | ||
| total += number; | ||
| } | ||
| } | ||
| return total; | ||
| return sumAll(numbers, (integer)->{return integer>3;}); | ||
|
Comment on lines
42
to
+47
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. 조건들도 메서드로 선언해서 깔끔하게 관리하는 것도 좋아보여요! |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| import java.nio.file.Files; | ||
| import java.nio.file.Paths; | ||
| import java.util.Arrays; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
|
|
@@ -28,6 +29,7 @@ public static void printLongestWordTop100() throws IOException { | |
| List<String> words = Arrays.asList(contents.split("[\\P{L}]+")); | ||
|
|
||
| // TODO 이 부분에 구현한다. | ||
| words.stream().filter(w -> w.length() > 12).sorted(Comparator.comparing(String::length).reversed()).distinct().limit(100).map(String::toLowerCase).forEach(System.out::println); | ||
|
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. 이 방식으로 조건들을 한 번에 해결할 수 있군요! 멋집니다. 👍 |
||
| } | ||
|
|
||
| public static List<Integer> doubleNumbers(List<Integer> numbers) { | ||
|
|
@@ -39,6 +41,7 @@ public static long sumAll(List<Integer> numbers) { | |
| } | ||
|
|
||
| public static long sumOverThreeAndDouble(List<Integer> numbers) { | ||
| return 0; | ||
| return doubleNumbers(numbers.stream().filter(x -> x>3).collect(Collectors.toList())) | ||
| .stream().reduce(Integer::sum).get(); | ||
| } | ||
| } | ||
| } | ||
| 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; | ||||||
|
|
@@ -33,7 +35,7 @@ public static boolean ageIsInRange1(User user) { | |||||
| } | ||||||
|
|
||||||
| public static boolean ageIsInRange2(User user) { | ||||||
| return false; | ||||||
| return Optional.ofNullable(user).map(x -> x.getAge()).filter(x -> x >=30 && x <= 45).isPresent(); | ||||||
|
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. 람다를 추가적으로 사용할 수 있습니다.
Suggested change
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.
|
||||||
| } | ||||||
|
|
||||||
| @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); | ||
|
|
@@ -13,11 +14,6 @@ public class Users { | |
| new User("honux", 45)); | ||
|
|
||
| User getUser(String name) { | ||
| for (User user : users) { | ||
| if (user.matchName(name)) { | ||
| return user; | ||
| } | ||
| } | ||
| return DEFAULT_USER; | ||
| return users.stream().filter(user -> user.matchName(name)).findAny().orElse(DEFAULT_USER); | ||
|
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.
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -8,24 +8,14 @@ public class CarTest { | |||||
| @Test | ||||||
| public void 이동() { | ||||||
| Car car = new Car("pobi", 0); | ||||||
| Car actual = car.move(new MoveStrategy() { | ||||||
| @Override | ||||||
| public boolean isMovable() { | ||||||
| return true; | ||||||
| } | ||||||
| }); | ||||||
| Car actual = car.move(() -> {return true;}); | ||||||
|
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.
Suggested change
|
||||||
| assertThat(actual).isEqualTo(new Car("pobi", 1)); | ||||||
| } | ||||||
|
|
||||||
| @Test | ||||||
| public void 정지() { | ||||||
| Car car = new Car("pobi", 0); | ||||||
| Car actual = car.move(new MoveStrategy() { | ||||||
| @Override | ||||||
| public boolean isMovable() { | ||||||
| return false; | ||||||
| } | ||||||
| }); | ||||||
| Car actual = car.move(() ->{return false;}); | ||||||
|
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.
Suggested change
|
||||||
| assertThat(actual).isEqualTo(new Car("pobi", 0)); | ||||||
| } | ||||||
| } | ||||||
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 인터페이스를 내부에 선언하신 이유가 있으실까요?
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.
그리고 test 말고도 좋은 메서드 이름이 있을 것 같아요!