본문 바로가기

Java/활용

[Java] Stream기본 -스트림으로 List, Set, 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) List 에 학생 데이터 담기

A-1)

List<Student> list = Stream.of(
        stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10, stu11, stu12)
        .collect(Collectors.toList());

 

 

Q-2) Set 에 전공 데이터 담기

A-2)

Set<Major> set = Stream.of(
        stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10, stu11, stu12)
        .map(Student::getMajor)
        .collect(Collectors.toSet());

 

 

 

Q-3) Map 에 [이름-전공] 담기

참고. Function<T, R>
Function 는 apply() 메서드가 있는 함수형 인터페이스이다.  apply() 는 입력값와 출력값을 매핑하는 식으로 사용할 수 있다. 가령 제네릭 T 형을 받아 제네릭 R 형을 리턴하는 식이다.

 

 

A-3-1)

Function<Student, String> funcStuName = new Function<Student, String>() {
    @Override
    public String apply(Student student) {
        return student.getName();
    }
};

Function<Student, Major> funcStuMajor = student -> student.getMajor();

Map<String, Major> stuMap1 = Stream.of(
        stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10, stu11, stu12)
        .collect(Collectors.toMap(funcStuName, funcStuMajor));

 

 

A-3-2)

Function<Student, String> funcStuName2 = Student::getName;
Function<Student, Major> funcStuMajor2 = Student::getMajor;

Map<String, Major> stuMap2 = Stream.of(
        stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10, stu11, stu12)
        .collect(Collectors.toMap(funcStuName2, funcStuMajor2));

 

 

 

*참고. Map 데이터 확인

Set<String> stuKey = stuMap1.keySet();
for (String sname : stuKey) {
    Major major = stuMap1.get(sname);
    System.out.println("이름: " + sname + "\t | 전공: " + major);
}