Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,24 @@ build/
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
.idea/misc.xml
.idea/workspace.xml
.idea/uiDesigner.xml
.idea/vcs.xml
.idea/material_theme_project_new.xml
.idea/git_toolbox_prj.xml
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

# Extra IntelliJ-related configs
gradle.xml
aws.xml
.name

### Eclipse ###
.apt_generated
.classpath
Expand Down
6 changes: 6 additions & 0 deletions .idea/google-java-format.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/main/java/problem/easy/Problem1.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class Problem1 {
* @return 길이가 5 이상인 문자열만 포함하는 리스트
*/
public static List<String> filterStrings(List<String> strings) {
return null;
return strings.stream()
.filter(string -> string.length() >= 5)
.toList();
}
}
6 changes: 4 additions & 2 deletions src/main/java/problem/easy/Problem10.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import problem.easy.resources.Customer;

public class Problem10 {
Expand All @@ -13,7 +15,7 @@ public class Problem10 {
* @return 나이별로 그룹화된 Map
*/
public static Map<Integer, List<Customer>> groupCustomersByAge(List<Customer> customers) {
// 여기에 코드 작성
return null;
return customers.stream()
.collect(Collectors.groupingBy(Customer::getAge));
}
}
5 changes: 3 additions & 2 deletions src/main/java/problem/easy/Problem11.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public class Problem11 {
* @return 10보다 큰 첫 번째 수 (존재하지 않으면 Optional.empty())
*/
public static Optional<Integer> findFirstGreaterThanTen(Set<Integer> numbers) {
// 여기에 코드 작성
return Optional.empty();
return numbers.stream()
.filter(number -> number > 10)
.findFirst(); // TODO
}
}
8 changes: 6 additions & 2 deletions src/main/java/problem/easy/Problem12.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package problem.easy;

import java.util.Map;
import java.util.stream.Collectors;

public class Problem12 {

Expand All @@ -11,7 +12,10 @@ public class Problem12 {
* @return 문자열 길이를 값으로 가지는 새 Map
*/
public static Map<String, Integer> mapStringLength(Map<String, String> map) {
// 여기에 코드 작성
return null;
return map.entrySet().stream() // TODO entrySet: key 와 value 를 한 번에 꺼낼 수 있음
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> entry.getValue() != null ? entry.getValue().length() : 0
));
}
}
8 changes: 6 additions & 2 deletions src/main/java/problem/easy/Problem13.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ public class Problem13 {
* @return 각 요소에 2를 곱한 새 Queue
*/
public static Queue<Integer> doubleEach(Queue<Integer> numbers) {
// 여기에 코드 작성
return new LinkedList<>();
return new LinkedList<>(
numbers.stream()
.map(number -> number * 2)
.toList()
);
}

}
15 changes: 13 additions & 2 deletions src/main/java/problem/easy/Problem14.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package problem.easy;

import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;

public class Problem14 {

Expand All @@ -11,7 +13,16 @@ public class Problem14 {
* @return 길이가 3 이상인 키를 포함하는 새 Map
*/
public static Map<String, String> filterKeys(Map<String, String> map) {
// 여기에 코드 작성
return null;
return map.entrySet()
.stream()
.filter(
entry -> entry.getKey() != null && entry.getKey().length() >= 3 // TODO if 문을 별도로 사용할 필요가 없네?
)
.collect(Collectors.toMap(
Map.Entry::getKey, // key 생성 방법
Map.Entry::getValue, // value 생성 방법
(oldValue, newValue) -> oldValue, // key 충돌 시 처리 방법 (key가 중복되면 새 값은 버리고 기존 값은 유지)
Collections::emptyMap // 어떤 Map 구현체를 만들지 (ex. HashMap, LinkedHashMap, TreeMap, .. )
));
}
}
7 changes: 5 additions & 2 deletions src/main/java/problem/easy/Problem15.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class Problem15 {

Expand All @@ -12,7 +13,9 @@ public class Problem15 {
* @return 'apple'을 값으로 가지는 모든 키의 Set
*/
public static Set<String> keysForValue(Map<String, String> map, String value) {
// 여기에 코드 작성
return null;
return map.entrySet()
.stream().filter(entry -> entry.getValue().equals("apple"))
.map(Map.Entry::getKey)
.collect(Collectors.toSet());
}
}
7 changes: 5 additions & 2 deletions src/main/java/problem/easy/Problem16.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package problem.easy;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

public class Problem16 {

Expand All @@ -11,7 +13,8 @@ public class Problem16 {
* @return 생성된 HashSet
*/
public static Set<Integer> createHashSetFromStream(int[] numbers) {
// 여기에 코드 작성
return null;
return Arrays.stream(numbers)
.boxed()
.collect(Collectors.toSet());
}
}
11 changes: 9 additions & 2 deletions src/main/java/problem/easy/Problem17.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package problem.easy;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

public class Problem17 {

Expand All @@ -12,7 +14,12 @@ public class Problem17 {
* @return 생성된 HashMap
*/
public static Map<String, Integer> createHashMapFromStream(String[] strings) {
// 여기에 코드 작성
return null;
return Arrays.stream(strings)
.collect(
Collectors.toMap(
string -> string,
String::length
)
);
}
}
8 changes: 6 additions & 2 deletions src/main/java/problem/easy/Problem18.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package problem.easy;

import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.stream.Collectors;

public class Problem18 {

Expand All @@ -11,7 +14,8 @@ public class Problem18 {
* @return 생성된 PriorityQueue
*/
public static Queue<Integer> createPriorityQueueFromStream(int[] numbers) {
// 여기에 코드 작성
return null;
return Arrays.stream(numbers)
.boxed()
.collect(Collectors.toCollection(PriorityQueue::new));
}
}
14 changes: 12 additions & 2 deletions src/main/java/problem/easy/Problem19.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package problem.easy;

import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

public class Problem19 {

Expand All @@ -12,7 +15,14 @@ public class Problem19 {
* @return 생성된 TreeMap
*/
public static Map<String, Integer> createTreeMapFromStream(String[] strings) {
// 여기에 코드 작성
return null;
return Arrays.stream(strings)
.collect(
Collectors.toMap(
string -> string,
String::length,
(existing, replacement) -> existing,
TreeMap::new // TODO "어떤 구현체로 만들지"
)
);
}
}
6 changes: 4 additions & 2 deletions src/main/java/problem/easy/Problem2.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

// TODO
public class Problem2 {

/**
Expand All @@ -11,7 +12,8 @@ public class Problem2 {
* @return 각 요소를 제곱한 새 리스트
*/
public static List<Integer> squareNumbers(List<Integer> numbers) {
// 여기에 코드 작성
return null;
return numbers.stream()
.map(number -> number * number)
.toList();
}
}
15 changes: 13 additions & 2 deletions src/main/java/problem/easy/Problem20.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package problem.easy;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class Problem20 {

Expand All @@ -12,7 +15,15 @@ public class Problem20 {
* @return 생성된 LinkedHashMap
*/
public static Map<Integer, Integer> createLinkedHashMapFromStream(int[] numbers) {
// 여기에 코드 작성
return null;
return Arrays.stream(numbers)
.boxed()
.collect(
Collectors.toMap(
number -> number,
number -> number * number,
(olderValue, newValue) -> olderValue,
LinkedHashMap::new
)
);
}
}
5 changes: 3 additions & 2 deletions src/main/java/problem/easy/Problem21.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public class Problem21 {
* @return 변환된 문자열 리스트
*/
public static List<String> convertToStringList(List<Integer> numbers) {
// 여기에 코드 작성
return null;
return numbers.stream()
.map(String::valueOf)
.toList();
}
}
5 changes: 3 additions & 2 deletions src/main/java/problem/easy/Problem22.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public class Problem22 {
* @return 대문자로 변환된 문자열 리스트
*/
public static List<String> convertToUpperCase(List<String> strings) {
// 여기에 코드 작성
return null;
return strings.stream()
.map(String::toUpperCase)
.toList();
}
}
5 changes: 3 additions & 2 deletions src/main/java/problem/easy/Problem23.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public class Problem23 {
* @return 각 문자열의 길이 리스트
*/
public static List<Integer> calculateStringLengths(List<String> strings) {
// 여기에 코드 작성
return null;
return strings.stream()
.map(String::length)
.toList();
}
}
3 changes: 1 addition & 2 deletions src/main/java/problem/easy/Problem24.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class Problem24 {
* @return 주어진 정수가 리스트에 존재하면 true, 그렇지 않으면 false
*/
public static boolean containsNumber(List<Integer> numbers, int number) {
// 여기에 코드 작성
return false;
return numbers.contains(number);
}
}
4 changes: 2 additions & 2 deletions src/main/java/problem/easy/Problem25.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Problem25 {
* @return 모든 요소가 짝수이면 true, 그렇지 않으면 false
*/
public static boolean areAllEven(List<Integer> numbers) {
// 여기에 코드 작성
return false;
return numbers.stream()
.allMatch(number -> number % 2 == 0); // TODO 모든 요소 --> "allMatch"
}
}
6 changes: 3 additions & 3 deletions src/main/java/problem/easy/Problem26.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
public class Problem26 {

/**
* 주어진 정수 리스트에서 하나 이상의 요소가 10보다 큰지 확인합니다.
* 주어진 정수 리스트에서 하나 이상의 요소가 10보다 큰지 확인합니다. --> anyMatch
*
* @param numbers 정수 리스트
* @return 하나 이상의 요소가 10보다 크면 true, 그렇지 않으면 false
*/
public static boolean hasGreaterThanTen(List<Integer> numbers) {
// 여기에 코드 작성
return false;
return numbers.stream()
.anyMatch(number -> number > 10);
}
}
5 changes: 3 additions & 2 deletions src/main/java/problem/easy/Problem27.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public class Problem27 {
* @return 평균 값 (리스트가 비어있으면 OptionalDouble.empty())
*/
public static OptionalDouble calculateAverage(List<Integer> numbers) {
// 여기에 코드 작성
return OptionalDouble.empty();
return numbers.stream()
.mapToInt(Integer::intValue) // TODO 정수하면 mapToInt + Integer::intValue
.average();
}
}
5 changes: 3 additions & 2 deletions src/main/java/problem/easy/Problem28.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package problem.easy;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;

Expand All @@ -12,7 +13,7 @@ public class Problem28 {
* @return 가장 긴 문자열 (리스트가 비어있으면 Optional.empty())
*/
public static Optional<String> findLongestString(List<String> strings) {
// 여기에 코드 작성
return Optional.empty();
return strings.stream()
.max(Comparator.comparingInt(String::length)); // TODO .max(String::compareTo) 사전 순 최대값
}
}
Loading