본 글은 우아한 테크코스 프리코스 1주차 미션 중 공부한 내용을 기록한 것이다.
-> 우아한 테크코스 프리코스 1주차 미션 java-onboarding
-> 필자가 제출한 코드
-> 1주차 미션 회고
배열 스트림
Stream<String> stringStream = Arrays.stream(new String[]{"a", "b"});
컬렉션 스트림
List<String> list = new ArrayList<>(List.of("a", "b"));
Stream<String> stream = list.stream();
Stream.builder()
Stream<String> builderStream = Stream.<String>builder()
.add("a").add("b")
.build();
Stream.generate()
Stream<String> generateStream = Stream.generate(() -> "a").limit(4);
// "a" 가 4개 들어감
int numberOfFalse = 10;
List<Boolean> booleanList = new ArrayList<>();
booleanList = Stream.generate(() -> false).limit(numberOfFalse)
.collect(Collectors.toList());
Stream.iterate()
Stream<Integer> iterateStream = Stream.iterate(1, n -> n + 1).limit(4);
// 1, n : 1은 초기 값, 이후 n이 리턴 형태에 맞춰 반환된다.
// 1, 2, 3, 4
int numberOfIds = 10;
List<Integer> idList = new ArrayList<>();
idList = Stream.iterate(1, n -> n + 1).limit(numberOfIds).collect(Collectors.toList());
기본 타입형 스트림 (IntStream, LongStream, DoubleStream)
IntStream intStream = IntStream.range(1, 5);
LongStream longStream = LongStream.range(1, 5);
//1, 2, 3, 4
IntStream intStream = IntStream.range(1, 5);
Stream<Integer> integerStream = intStream.boxed();
int numberOfIds = 10;
List<Integer> idList = new ArrayList<>();
idList = IntStream.range(1, numberOfIds+1).boxed().collect(Collector.toList());
스트림 연결하기
Stream<Integer> stream1 = Stream.of(1, 2, 3);
Stream<Integer> stream2 = Stream.of(4, 5, 6);
Stream<Integer> concatStream = Stream.concat(stream1, stream2);
//1, 2, 3, 4, 5, 6
Filtering (.filter())
List<Integer> numbers = new ArrayList<>(List.of(1, 2, 3, 11, 12, 13));
Stream<Integer> integerStream = numbers.stream().filter(number -> number > 10);
// 11, 12, 13
List<String> words = new ArrayList<>(List.of("aaa", "abc", "dfe"));
Stream<String> filtered = words.stream().filter(word -> word.contains("a"));
// "aaa", "abc"
Mapping (.map())
List<String> strings = new ArrayList<>(List.of("a", "b", "c"));
// 아래 두 방법 동일한 결과를 갖는다.
Stream<String> upper = strings.stream().map(s -> s.toUpperCase());
Stream<String> upper = strings.stream().map(String::toUpperCase);
//A, B, C
List<String> sortedRecommendList = sortedRecommendScore.stream()
.map(Map.Entry::getKey).collect(Collectors.toList());
List<String> sortedRecommendList = sortedRecommendScore.stream()
.map(score -> score.getKey()).collect(Collectors.toList());
Sorting (.sorted())
List<Integer> numbers = new ArrayList<>(List.of(3, 1, 2, 4));
Stream<Integer> sorted = numbers.stream().sorted();
//1, 2, 3, 4
List<Integer> numbers = new ArrayList<>(List.of(3, 1, 2, 4));
Stream<Integer> sorted = numbers.stream().sorted(Comparator.reverseOrder());
//4, 3, 2, 1
List<String> strings = new ArrayList<>(List.of("aa", "aaa", "a", "aaaa"));
// 아래는 모두 같은 방법들이다.
// 방법 1
Stream<String> sorted = strings.stream().sorted(Comparator.comparingInt(String::length));
// 방법 2
Comparator<String> comparator = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if (o1.length() > o2.length()) {
return 1;
} else if (o1.length() < o2.length()) {
return -1;
} else {
return 0;
}
}
};
Stream<String> sorted = strings.stream().sorted(comparator);
// 방법 3
Stream<String> sorted = strings.stream().sorted((s1, s2) -> s1.length() - s2.length());
// "a", "aa", "aaa", "aaaa"
Iterating (.peek())
List<String> strings1 = new ArrayList<>(List.of("aa", "aaa", "a", "aaaa"));
// 아래 모두 같은 방법들
// 방법 1
List<String> sorted11 = strings.stream()
.sorted(Comparator.comparingInt(String::length))
.peek(System.out::println)
.collect(Collectors.toList());
//방법 2
List<String> sorted11 = strings.stream()
.sorted(Comparator.comparingInt(String::length))
.peek(s -> System.out.println(s))
.collect(Collectors.toList());
long count = IntStream.of(1, 2, 3).min();
long count = IntStream.of(1, 2, 3).max();
long count = IntStream.of(1, 2, 3).count();
long count = IntStream.of(1, 2, 3).sum();
long count = IntStream.of(1, 2, 3).average();
Collectors.toList()
List<Integer> intList = new ArrayList<>(List.of(1, 2, 11, 12));
List<Integer> overTen = intList.stream().filter(n -> n > 10).collect(Collectors.toList());
List<String> sortedRecommendList = sortedRecommendScore.stream()
.map(Map.Entry::getKey).collect(Collectors.toList());
Collectors.joining()
List<Integer> intList = new ArrayList<>(List.of(1, 2, 11, 12));
String overTenString = intList.stream()
.filter(n -> n > 10)
.map(n -> n.toString()
.collect(Collectors.joining());
// 1112
List<Integer> intList = new ArrayList<>(List.of(1, 2, 11, 12));
String overTenString = intList.stream()
.filter(n -> n > 10)
.map(n -> n.toString()
.collect(Collectors.joining(", ", "[", "]"));
// [11, 12]
anyMatch
List<String> strings = new ArrayList<>(List.of("abcd", "abcd", "abc"));
boolean anyMatch = strings.stream().anyMatch(s -> s.length() == 3);
// true
allMatch
List<String> strings = new ArrayList<>(List.of("abcd", "abcd", "abc"));
boolean allMatch = strings.stream().allMatch(s -> s.contains("abc"));
// true
noneMatch
List<String> strings = new ArrayList<>(List.of("abcd", "abcd", "abc"));
boolean noneMatch = strings.stream().noneMatch(s -> s.contains("e"));
// true
forEach
peek의 최종 작업 단계이다.
peek는 중간에 가공 단계로 삽입될 수 있고, forEach는 최종으로 삽입될 수 있다.
List<String> strings = new ArrayList<>(List.of("abcd", "abcd", "abc"));
strings.stream().forEach(System.out::println);