distinct 중복제거
//컬렉션에서 Stream 생성
List<String> list = Arrays.asList("홍길동", "이순신", "유관순", "이순신");
//1. 중간처리 (중복제거- distinct)
list.stream()
.distinct()
.forEach(System.out::println); //홍길동, 이순신, 유관순
filter 필터링
//익명함수
Predicate<String> predicate = new Predicate<String>() {
@Override
public boolean test(String t) {
return t.startsWith("이");
}
};
//filter
list.stream()
.filter(predicate)
.forEach(System.out::println);
//람다표현식
//모두 다 리턴타입이 stream
list.stream()
.filter(t->t.startsWith("이"))
.forEach(System.out::println);
혼합 (distinct + filter)
list.stream()
.distinct()
.filter(t->t.startsWith("이"))
.forEach(System.out::println);
map(Function), mapToInt(ToIntFunction), mapToDouble(ToDoubleFunction), mapToLong(ToLongFunction) : 요소를 가공하여 새로운 스트림을 반환
class Student{
String username;
int score;
public Student() {}
public Student(String username, int score) {
this.username = username;
this.score = score;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "Student [username=" + username + ", score=" + score + "]";
}
//생성
List<Student> list = Arrays.asList(new Student("홍길동", 90),
new Student("이순신", 80),
new Student("유관순", 50));
//가공1: 이름에서 성만 출력
//익명클래스
Function<Student, String> mapper = new Function<Student, String>(){
@Override
public String apply(Student t) {
return t.getUsername().substring(0, 1);
}
};
list.stream()
.map(mapper)
.forEach(System.out::println);
System.out.println();
//람다표현식
list.stream()
.map(t->t.getUsername().substring(0, 1))
.forEach(System.out::println);
System.out.println();
mapToInt
ToIntFunction<Student> mapper3 = new ToIntFunction<Student>() {
@Override
public int applyAsInt(Student t) {
return t.getScore();
}
};
list.stream()
.mapToInt(mapper3)
.forEach(System.out::println);
System.out.println();
//람다표현식
list.stream()
.map(t->t.getScore())
.forEach(System.out::println);
System.out.println();
//method reference
list.stream()
.map(Student::getScore)
.forEach(System.out::println);
System.out.println();
flatmap(Function<T,Stream>)
요소를 가공하여 복수 개의 요소들로 구성된 새로운 스트림을 반환
List<String> list = Arrays.asList("hello world", "happy virus");
//실습1 : 공백문자 기준으로 분리
Function<String, Stream<? extends String>> mapper
= new Function<String, Stream<? extends String>>() {
@Override
public Stream<? extends String> apply(String t) {
String[] names = t.split(" ");
return Arrays.stream(names);
}
};
list.stream()
.flatMap(mapper)
.forEach(System.out::println);
System.out.println();
//람다 표현식
list.stream()
.flatMap(t->Arrays.stream(t.split(" ")))
.forEach(System.out::println);
System.out.println();
//실습2: 쉼표 기준으로 분리 + 정수값으로 출력
List<String> list2 = Arrays.asList("10,20,30","40,50,60");
Function<String, IntStream> mapper2 = new Function<String, IntStream>(){
@Override
public IntStream apply(String t) {
String[] arr = t.split(",");
// String[] -> int[] ★★★
int [] nums = Arrays.stream(arr).mapToInt(Integer::parseInt).toArray();
return Arrays.stream(nums);
}
};
list2.stream()
.flatMapToInt(mapper2)
.forEach(System.out::println);
System.out.println();
//람다 표현식
list2.stream()
.flatMapToInt(t->Arrays.stream(Arrays.stream(t.split(",")).mapToInt(Integer::parseInt).toArray()))
.forEach(System.out::println);
System.out.println();
'Programming Language > JAVA' 카테고리의 다른 글
자바 스트림 API- 중간처리(3)_boxed/asDoubleStream() (0) | 2023.09.07 |
---|---|
자바 스트림 API - 중간처리(2)_sorted/skip/limit (0) | 2023.09.07 |
자바 스트림 API - 스트림 생성 (0) | 2023.09.07 |
자바 스트림 API (java stream API) (0) | 2023.09.07 |
람다표현식과 표준 API 함수적 인터페이스 (0) | 2023.09.06 |