본문 바로가기

Java/활용

[Java] Stream기본 -sort() 메서드로 값 정렬하기

샘플로 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)

Comparator<Student> comp1 = new Comparator<Student>() {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.getName().compareTo(o2.getName());
    }
};

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

 

 

A-1-2)

Comparator<Student> comp2 = (o1, o2) -> o1.getName().compareTo(o2.getName());

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

 

 

A-1-3)

Comparator<Student> comp3 = Comparator.comparing(Student::getName);

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

 

 

A-1-4)

Stream.of(stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10, stu11, stu12)
        .sorted(Comparator.comparing(Student::getName))
        .forEach(a -> System.out.println(a.getName()));

 

 

A-1-5)

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

 

 

Q-2) 전체 학생 이름을 역순으로 정렬해서 출력하기

 

A-2-1)

Comparator<Student> comp5 = (o1, o2) -> o2.getName().compareTo(o1.getName());

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

 

 

A-2-2)

Stream.of(stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10, stu11, stu12)
        .sorted((o1, o2) -> o2.getName().compareTo(o1.getName()))
        .map(Student::getName)
        .forEach(System.out::println);

 

 

 

Q-3) 학년 순서대로 정렬해서 이름 출력하기

 

Q-3-1)

Stream.of(stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10, stu11, stu12)
        .sorted((o1, o2) -> Integer.compare(o1.getGrade(), o2.getGrade()))
        .map(Student::getName)
        .forEach(System.out::println);

 

 

Q-3-2)

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