정렬, 역정렬, 조합해서 자유자재로 사용하기

라헬·2022년 12월 24일
0

먼저 역정렬은
정렬하는 함수에서 reverse를 사용하면 된다.

inventory.sort(comparing(Apple::getWeight).reversed()); 

무게를 내림차순으로 정렬한 것
이제 조건을 더 추가해보자.

inventory.sort(comparing(Apple::getWeight)
		.reversed() //무게를 내림차순으로 정렬함
        .thenComparing(Apple::getCountry)); // 두사과의 무게가 같으면 국가별로 정렬

그럼 초록색이 아닌 사과만 고르고 싶다면? negate를 사용하면 된다.

Predicate<Apple> notGreenApple = greenApple.negate();

그럼 빨간색이면서 150그램 이상의 사과 또는 그냥 초록색 사과를 가져와보자

Predicate<Apple> redAndHeavyAppleOrGreen = redApple.and(
				apple -> apple.getWeight() > 150 )
                .or(apple -> GREEN.equals(a.getColor)));
profile
성장하고 싶은 풀스택 개발자

0개의 댓글