java41 필터링 과 맵핑 ●필터링 package org.example; import java.util.Arrays; import java.util.List; public class Main4 { public static void main(String[] args) { Person p1 = new Person("홍길동",20); Person p2 = new Person("김길동",40); Person p3 = new Person("이길동",30); Person p4 = new Person("박길동",50); List list = Arrays.asList(p1,p2,p3,p4); list.stream().filter(person -> person.getAge()==20).forEach(System.out::println); // 필.. 2024. 2. 15. 스트림의 이해와 생성 ● 첫번째 예제 package org.example; import java.util.Arrays; import java.util.List; import java.util.stream.IntStream; import java.util.stream.Stream; public class Main { public static void main(String[] args) { int ar[] = {1, 2, 3, 4, 5}; IntStream stm1 = Arrays.stream(ar); stm1.forEach(System.out::println); // 매서드 참조형 식 // stm1.forEach(s -> System.out.println(s)); 이거와 똑같다 // Arrays.steam(ar).forEach.. 2024. 2. 15. 미리 정의되어 있는 함수형 인터페이스 ●Predicate package org.example; import java.util.Arrays; import java.util.List; import java.util.function.IntPredicate; import java.util.function.Predicate; public class Main { public static void main(String[] args) { List list = Arrays.asList(1,5,7,9,11,12); int s; s = sum(n -> n%2 ==0, list); System.out.println("짝수의 합"+s); s = sum(n -> n%2 != 0,list); System.out.println("혹수의 합"+s); } public st.. 2024. 2. 13. 람다와 함수형 인터페이스 ● 람다식이 두개 일 경우에는 중괄호 생략 불가 입니다. Comparator comp = (o1, o2) -> o1.length() - o2.length(); Collections.sort(list1,(o1, o2) -> o1.length() - o2.length() ); Collections.sort(list1,comp); 두개다 똑같은 방법이다. ● 매개변수가 둘 인 람다식 interface Calculate{ void cal(int a, int b); } public class Main3 { public static void main(String[] args) { Calculate c = (a, b) -> System.out.println(a+b); c.cal(4,3); c = (a, b) -> S.. 2024. 2. 8. 이전 1 2 3 4 5 6 7 ··· 11 다음