filter()
: 걸러내기
peek()
: forEach()
랑 비슷, 작업 중간에 잘 처리됐나 확인하기
✨✨✨map()
&flatMap()
skip()
Stream<T> skip(long n)
limit()
Stream<T> limit(long maxSize)
예)
IntStream intStream = IntStream.rangeClosed(1, 10);
//1 2 3 4 5 6 7 8 9 10
intStream.skip(3).limit(5).forEach(System.out::print);
45678
filter()
Stream<T> filter(Predicate<? super T> predicate)
distinct()
Stream<T> distict()
예)
IntStream intStream = IntStream.rangeClosed(1, 10);
//1 2 3 4 5 6 7 8 9 10
intStream.filter(i->i%2==0).forEach(System.out::print);
intStream.filter(i->i%2!=0 && i%3!=0).forEach(System.out::print);
//***&& 일 경우 filter()를 나눠서 해도 된다!
intStream.filter(i->i%2!=0).filter(i->i%3!=0).forEach(System.out::print);
246810
157
157
예)
IntStream intStream = IntStream.of(1,2,2,6,3,4,5,5,3,6);
intStream.distinct().forEach(System.out::print);
126345
sorted()
Stream<T> sorted()
Stream<T> sorted(Comparator<? super T> comparator)
Comparator.comparing()
으로 정렬기준 제공
Comparator comparing(Function<T,U> keyExtractor)
Comparator comparing(Function<T,U> keyExtractor, Comparator<U> keyComparator)
예)
Student::getBan
== (Student s) -> s.getBan()
thenComparing()
- 정렬기준이 여러개 일 때
Comparator thenComparing(Comparator<T> other)
Comparator thenComparing(Function<T,U> keyExtractor)
Comparator thenComparing(Function<T,U> keyExtractor, Comparator<U> keyComp)
예)
studentStream.sorted(Comparator.comparing(Student::getBan) //반별로 정렬
.thenComparing(Student::getTotalScore) //총점별로 정렬
.thenComparing(Student::getName)) //이름별로 정렬
.forEach(System.out::println);
ex14_05
import java.util.Comparator;
import java.util.stream.Stream;
public class Ex14_05 {
public static void main(String[] args) {
Stream<Student> stuStream = Stream.of(
new Student("이자바", 3, 300),
new Student("김자바", 1, 200),
new Student("안자바", 2, 100),
new Student("박자바", 2, 150),
new Student("소자바", 1, 200),
new Student("나자바", 3, 290),
new Student("감자바", 3, 180)
);
// stuStream.sorted().forEach(System.out::println); //기본정렬:총점 내림차순
stuStream.sorted(Comparator.comparing(Student::getBan) //반별 정렬
.thenComparing(Comparator.naturalOrder())) //기본정렬(총점 내림차순)
// .thenComparing(Comparator.naturalOrder()).reversed()) //기본 반대정렬(총점 올림차순)
// .thenComparing(Comparator.reverseOrder())) //기본 반대정렬(총점 올림차순)
.forEach(System.out::println);
}//main
}
class Student implements Comparable<Student>{
String name;
int ban;
int totalScore;
public String getName() {return name;}
public int getBan() {return ban;}
public int getTotalScore() {return totalScore;}
Student(String name, int ban, int totalScore) {
this.name = name;
this.ban = ban;
this.totalScore = totalScore;
}
//****기본정렬을 총점 내림차순으로 한다.
//오버라이드 필수로 해야한다!!
@Override
public int compareTo(Student s) {
return s.totalScore - this.totalScore;
}
//***
public String toString() {
return String.format("[%s, %d, %d]", name, ban, totalScore);
}
}
stuStream.sorted().forEach(System.out::println); //기본정렬:총점 내림차순
[이자바, 3, 300]
[나자바, 3, 290]
[김자바, 1, 200]
[소자바, 1, 200]
[감자바, 3, 180]
[박자바, 2, 150]
[안자바, 2, 100]
stuStream.sorted(Comparator.comparing(Student::getBan)) //반별 정렬
.forEach(System.out::println);
[김자바, 1, 200]
[소자바, 1, 200]
[안자바, 2, 100]
[박자바, 2, 150]
[이자바, 3, 300]
[나자바, 3, 290]
[감자바, 3, 180]
stuStream.sorted(Comparator.comparing(Student::getBan) //반별 정렬
.thenComparing(Comparator.naturalOrder())) //기본정렬(총점 내림차순)
.forEach(System.out::println);
[김자바, 1, 200]
[소자바, 1, 200]
[박자바, 2, 150]
[안자바, 2, 100]
[이자바, 3, 300]
[나자바, 3, 290]
[감자바, 3, 180]
stuStream.sorted(Comparator.comparing(Student::getBan) //반별 정렬
.thenComparing(Comparator.reverseOrder())) //기본 반대정렬(총점 올림차순)
.forEach(System.out::println);
[김자바, 1, 200]
[소자바, 1, 200]
[안자바, 2, 100]
[박자바, 2, 150]
[감자바, 3, 180]
[나자바, 3, 290]
[이자바, 3, 300]
reversed()
stuStream.sorted(Comparator.comparing(Student::getBan) //반별 정렬 +
.thenComparing(Comparator.naturalOrder()).reversed()) //기본 정렬(총점 내림차순) -> 합쳐서 역순)
.forEach(System.out::println);
[감자바, 3, 180]
[나자바, 3, 290]
[이자바, 3, 300]
[안자바, 2, 100]
[박자바, 2, 150]
[김자바, 1, 200]
[소자바, 1, 200]
reversed()
: ✨✨앞의 모든 것에 적용stuStream.sorted(Comparator.comparing(Student::getBan).reversed() //반별 정렬을 역순
.thenComparing(Comparator.naturalOrder())) //기본 정렬(총점 내림차순)
.forEach(System.out::println);
[이자바, 3, 300]
[나자바, 3, 290]
[감자바, 3, 180]
[박자바, 2, 150]
[안자바, 2, 100]
[김자바, 1, 200]
[소자바, 1, 200]
.thenComparing(Comparator.naturalOrder()).reversed()) //기본 반대정렬(총점 올림차순)
.forEach(System.out::println);
[김자바, 1, 200]
[소자바, 1, 200]
[안자바, 2, 100]
[박자바, 2, 150]
[감자바, 3, 180]
[나자바, 3, 290]
[이자바, 3, 300]
앞에 한번, 그리고 전체적으로 다시 한번 적용!!
✨✨reversed()
: 맨앞 또는 전체에 한번에 역순정렬 할 때!!
💖💖💖reverseOrder()
: 하나만 역순정렬 할 때!!