본문 바로가기

Java/활용

[Java] Stream기본 -filter(), map() 메서드로 필요한 값만 가져오기

샘플로 Student 클래스 준비

class Student {
        private final int sno;
        private final String name;
        private final Major major;
        private final int grade;

        public Student(int sno, String name, Major major, int grade) {
            this.sno = sno;
            this.name = name;
            this.major = major;
            this.grade = grade;
        }

        public int getSno() {
            return sno;
        }

        public String getName() {
            return name;
        }

        public Major getMajor() {
            return major;
        }

        public int getGrade() {
            return grade;
        }
    }

    private enum Major {
        ECONOMICS, POLITICS, PSYCHOLOGY, EDUCATION, LAWS
    }
}

 

Student 객체 준비

Student stu1 = new Student(1, "jimin", Major.ECONOMICS, 1);
Student stu2 = new Student(2, "jun", Major.POLITICS, 2);
Student stu3 = new Student(3, "mike", Major.PSYCHOLOGY, 3);
Student stu4 = new Student(4, "kevin", Major.ECONOMICS, 1);
Student stu5 = new Student(5, "kim", Major.PSYCHOLOGY, 2);
Student stu6 = new Student(6, "hwang", Major.EDUCATION, 3);
Student stu7 = new Student(7, "jo", Major.LAWS, 1);
Student stu8 = new Student(8, "yuna", Major.PSYCHOLOGY, 2);
Student stu9 = new Student(9, "chanSung", Major.POLITICS, 3);
Student stu10 = new Student(10, "soYoung", Major.LAWS, 1);
Student stu11 = new Student(11, "sujin", Major.ECONOMICS, 2);
Student stu12 = new Student(12, "james", Major.LAWS, 3);

 

 


 

 

Q-1) 스트림 생성하기

 

A-1-1)

Arrays.asList(stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10, stu11, stu12)
        .stream();

 

A-1-2)

Student[] s = {stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10, stu11, stu12};
Arrays.stream(s);

 

A-1-3)

Arrays.stream(
	new Student[] {stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10, stu11, stu12}
);

 

A-1-4)

Stream.of(stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10, stu11, stu12);

 

 

 

Q-2) 2학년 학생만 이름 출력하기

 

A-2-1)

Stream.of(stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10, stu11, stu12)
        .filter(a -> a.grade == 2)
        .forEach(a -> System.out.println(a.getName()));

 

A-2-2)

Arrays.asList(stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10, stu11, stu12)
        .stream()
        .filter(stu -> stu.grade == 2)
        .map(stu -> stu.getName())
        .forEach(stu -> System.out.println(stu));

 

A-2-3)

Stream.of(stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10, stu11, stu12)
        .filter(a -> a.grade == 2)
        .map(Student::getName)
        .forEach(System.out::println);

 

 

 

 

Q-3) 2학년 학생만 List에 저장하고 이름 확인하기

A-3-1)

List<Student> list = Stream.of(
	stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10, stu11, stu12)
        .filter(a -> a.grade == 2)
        .collect(Collectors.toList());
list.forEach(a -> System.out.println(a.getName()));

 

 

 

Q-4) 경제학 전공 학생만 가져오기 

 

참고. Predicate<T>
Predicate 는 test() 메서드가 있는 함수형 인터페이스이다.  test() 는 입력값 제네릭 T의 참 혹은 거짓을 판단한다.


A-4-1)

Predicate<Student> pre1 = new Predicate<Student>() {
    @Override
    public boolean test(Student student) {
        if (student.getMajor() == Major.ECONOMICS) {
           return true;
        }
        return false;
    }
};

Stream.of(stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10, stu11, stu12)
        .filter(pre1)
        .map(Student::getName)
        .forEach(System.out::println);

 

A-4-2)

Predicate<Student> pre2 = new Predicate<Student>() {
    @Override
    public boolean test(Student student) {
        return student.getMajor() == Major.ECONOMICS;
    }
};

Stream.of(stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10, stu11, stu12)
        .filter(pre2)
        .map(Student::getName)
        .forEach(System.out::println);

 

 

A-4-3)

Predicate<Student> pre3 = stu -> stu.getMajor() == Major.ECONOMICS;

Stream.of(stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10, stu11, stu12)
        .filter(pre3)
        .map(Student::getName)
        .forEach(System.out::println);

 

 

 

Q-5) 1학년 학생 전공 데이터에서 중복 제외하기

 

A-5-1)

Stream.of(stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10, stu11, stu12)
        .filter(s -> s.getGrade() == 1)
        .map(Student::getMajor)
        .distinct()
        .forEach(System.out::println);