ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • day31 - JAVA (자바, 스트림, 플랫맵, 필터)
    KIC/JAVA 2021. 7. 27. 15:30
    반응형

    [stream Ex]

    package javaPro.java_Stream;
    
    import java.util.Arrays;
    import java.util.Iterator;
    import java.util.List;
    import java.util.stream.Stream;
    
    public class StreamEx0 {
        public static void main(String[] args) {
            List<String> list = Arrays.asList("홍길동", "김삿갓", "이몽룡", "성춘향");
            System.out.println("외부 반복자 사용");
            Iterator it = list.iterator();
            while (it.hasNext()) {
                System.out.println(it.next());
            }
    
            System.out.println("내부 반복자 사용");
            //list.stream().forEach(s -> System.out.println(s));
            Stream<String> st = list.stream(); // List 객체를 Stream 객체 변환
            st.forEach(s -> System.out.println(s));
    
            System.out.println("다시 출력");
            list.stream().forEach(s -> System.out.println(s));
    
        }
    
    }

     

     

     

    [FlatMap Ex]

    package javaPro.java_Stream;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class FlatMapEx2 {
    	public static void main(String[] args) {
    		List<Student> studentList1 = Arrays.asList(
    				new Student("홍길동", 10),new Student("강나라", 20),
    				new Student("장일동", 30)	);
    		List<Student> studentList2 = Arrays.asList(
    				new Student("나자바", 10),new Student("신용권", 20),
    				new Student("유미선", 30)	);
    		
    		
    		List<List<Student>> stu = new ArrayList<List<Student>>();
    		         stu.add(studentList1);
    		         stu.add(studentList2);
    		long sum= stu.stream()
      //		.flatMap(s -> s.stream())
    			.flatMap(List::stream)
    		
    	//		.mapToInt(Student :: getScore)
    			.mapToInt(s -> s.getScore())
    			.count();
    		      System.out.println(sum);
    			//.forEach(score -> System.out.println(score));
    	}
    }

     

     

     

     

    [필터 ex]

    package javaPro.java_Stream;
    
    import java.util.Arrays;
    import java.util.List;
    
    class Member {
        public static int MALE = 0;
        public static int FEMALE = 1;
        private String name;
        private int sex;
        private int age;
    
        public Member(String name, int sex, int age) {
            this.name = name;
            this.sex = sex;
            this.age = age;
        }
    
        public int getSex() {
            return sex;
        }
    
        public int getAge() {
            return age;
        }
    }
    
    public class FilterEx4 {
        public static void main(String[] args) {
            List<Member> list = Arrays.asList(new Member("홍길동", Member.MALE, 30), new Member("김나리", Member.FEMALE, 20),
                    new Member("신용권", Member.MALE, 45), new Member("박수미", Member.FEMALE, 27));
    
            double ageAvg = list.stream().filter(m -> m.getSex() == Member.MALE).mapToInt(s -> s.getAge()).average()
                    .getAsDouble();
            System.out.println("남자 평균 나이: " + ageAvg);
        }
    }
    300x250

    댓글

Designed by Tistory.