1. [Functional Interface] : 인터페이스에는 Runnable, Comparator 등과 같은 하나의 추상 메서드 만 있습니다. Functional Interface 개체가 필요할 때마다 람다 식을 사용할 수 있습니다.
2. [Method Reference] : 람다 식의 매개 변수를 매개 변수로 메서드에 전달하면 실행 효과가 동일
하면 Method Reference를 사용하여 람다 식을 표현할 수 있으며 다음 두 메서드는 동일합니다.
(x) -> System.out.println(x)
System.out::println
메서드 참조는 주로 세 가지 형식으로 구성됩니다. 1.Object ::
instanceMethod
2.Class :: staticMethod
3.Class :: instanceMethod
처음 두 메서드의 경우 해당 람다 식 매개 변수와 메서드 매개 변수는 동일합니다. 예를 들면 다음과 같습니다.
System.out::println
(x) -> System.out.println(x)
수학 :: pow
(x, y) -> Math.pow(x, y)
세 번째 메서드의 경우 해당 람다 식의 본문에서 첫 번째 매개 변수가 개체로 사용되고 메서드가 호출되고 다른 매개 변수가
String::compareToIgnoreCase
(s1, s2) -> s1.compareToIgnoreCase(s2)
생성자 참조는 특수 메소드 인 new라는 점을 제외하면 메소드 참조와 유사합니다. 호출되는 생성자는 다음과 같은 컨텍스트에 따라 다릅니다.
List<String> labels = ...;
Stream<Button> stream = labels.stream().map(Button::new);
Button :: new는 (x)-> Button (x)와 동일하므로 호출 된 생성자는 Button (x);
단일 객체를 생성하는 것 외에도 객체의 배열을 생성 할 수 있습니다. 다음 두 가지 메서드는 동등한:
int[]::new
(x) -> new int[x]
======지도 ======
1.지도 순회
itemsMap.forEach((k,v)->System.out.println("Item : " + k + " Count : " + v));
====== 목록 ======
1. 목록 정렬
1), 목록 오름차순
//原始版
words.sort((Word first, Word second) -> first.getName().compareTo(second.getName()));
//精简版
Collections.sort(words, Comparator.comparing(Word::getName));
//精简版,推荐使用这种
words.sort(Comparator.comparing(Word::getName));
//多重排序(连个都升序)
words.sort(Comparator.comparing(Word::getName).thenComparingInt(Word::getCountry));
//多重排序(第一个升,第二个降)
words.sort(Comparator.comparing(Word::getName).reversed().thenComparingInt(Word::getCountry).reversed());
2), 내림차순 목록
words.sort(Comparator.comparing(Word::getName).reversed());
2. 목록 순회
words.forEach(System.out::println);
3. 매핑 할 목록 (키 중복 제거)
Map<String, Word> wordMapMap = wordList.stream().collect(Collectors.toMap(Word::getName, k -> k, (k1, k2) -> k1));
매핑 할 목록 (키를 반복 할 수 없음)
Map<Integer,Word> userMap = wordList.stream().collect(Collectors.toMap(Word::getCountry, k -> k));
4. 그룹
Map<String, List<Word>> groupMap = wordList.stream().collect(Collectors.groupingBy(Word::getName));
다중 그룹화
Map<String, Map<Integer, List<Word>>> map2 = words.stream().collect(Collectors.groupingBy(Word::getName,Collectors.groupingBy(Word::getCountry)));
5. 일치 필터링
List<Word> filterList = wordList.stream().filter(v -> v.getName().equals("tiger")).collect(Collectors.toList());
wordList.stream().filter(v -> v.getName().contains("老")).forEach(System.out::println);
List<Word> filterList = wordList.stream().filter(v -> v.getCountry() > 2).collect(Collectors.toList());
Predicate<Word> equals1 = v -> v.getCountry().equals(1);
Predicate<Word> contains1 = v -> v.getName().contains("tiger");
List<Word> filterList = wordList.stream().filter(contains1.and(equals1)).collect(Collectors.toList());
List<Double> filteredCost = cost.stream().filter(x -> x > 25.0).collect(Collectors.toList());
6, 목록 통계 요약
int sum = wordList.stream().mapToInt(Word::getCountry).sum();
double sum = words.stream().mapToDouble(Word::getCountry).sum();
7. 그룹 통계
Map<String, Long> collectCnt = wordList.stream().collect(Collectors.groupingBy(Word::getName, Collectors.counting()));
8. 그룹 통계
Map<String, LongSummaryStatistics> map3 = words.stream().collect(Collectors.groupingBy(Word::getName,Collectors.summarizingLong(Word::getCountry)));
여러 그룹화 통계
Map<String, Map<Integer, LongSummaryStatistics>> map3 = words.stream().collect(Collectors.groupingBy(Word::getName,Collectors.groupingBy(Word::getCountry,Collectors.summarizingLong(Word::getCountry))));
9. 중복 제거 (실제 상황에 따라 Word의 equals 및 hashCode 재 작성)
List<Word> list1= list.stream().distinct().collect(Collectors.toList());
10. 데이터 어셈블리
String result = words.stream().map(Word::getName).collect(Collectors.joining("," , "[" , "]"));
12. 속성 수집에 대한 개체 수집
List<String> result = words.stream().map(Word::getName).collect(Collectors.toList());
List<String> result = words.stream().map(y -> y.getName().concat(".jpg")).collect(Collectors.toList());
====== 어레이 ======
1. 배열 정렬
1), 오름차순 배열-> 원본 버전
Arrays.sort(people, (first, second) -> Integer.compare(first.length(), second.length()));
오름차순 배열-> 라이트 버전
Arrays.sort(people, Comparator.comparingInt(String::hashCode));
2), 배열 내림차순
Arrays.sort(people, (second, first) -> Integer.compare(first.length(), second.length()));
2. 배열은 특정 요소의 수를 계산
long num = Arrays.stream(name).filter(x -> x.equals("tiger")).count();
3. 배열이 필터링되고 컬렉션으로 변환됩니다.
List<String> filterList = Arrays.stream(name).filter(x -> !x.equals("赵强")).collect(Collectors.toList());
4. 배열 필터링 및 요소에 접미사 추가
List<String> filterList = Arrays.stream(name).filter(x -> !x.equals("tiger")).map(y -> y.concat(".jpg")).collect(Collectors.toList());
5. 배열 통계 요약
int num = Arrays.stream(arr).reduce(0, Integer::sum);
double sum = Arrays.asList(10.0, 20.0, 30.0).stream().map(x -> x + x * 0.05).reduce(Double::sum).get();
double sum = Stream.of(10.0, 20.0, 30.0).map(x -> x + x * 0.05).reduce(Double::sum).get();
int sum = Stream.of(1, 2, 3, 4, 5).map(x -> x + x * 5).reduce(Integer::sum).get();
====== 합계 ======
BinaryOperator<Integer> add = Integer::sum;
Integer x = add.apply(20, 30);
====== 스레드 ======
private static void lambdaRunnable() {
int age = 20;
Runnable r2 = () -> {
System.out.println(age);
System.out.println("Hello world two!"+age);
};
r2.run();
}