티스토리 뷰

class Person{
    String username;
    int score;

    public Person() {}

    public Person(String username, int score) {
        this.username = username;
        this.score = score;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student [username=" + username + ", score=" + score + "]";
    }


}//end class





sorted : 정렬

List<Person> list = Arrays.asList(new Person("홍길동1", 100),
                                                                  new Person("홍길동2", 80),
                                                                  new Person("홍길동3", 85),
                                                                  new Person("홍길동4", 90),
                                                                  new Person("홍길동5", 70));


오름차순

//오름차순: Person클래스의 score값 기준
        list.stream()
//            .sorted(Comparator.comparing(Person::getScore))
                .sorted(Comparator.comparingInt(Person::getScore))
                .forEach(System.out::println);
        System.out.println();

//Student [username=홍길동5, score=70]
//Student [username=홍길동2, score=80]
//Student [username=홍길동3, score=85]
//Student [username=홍길동4, score=90]
//Student [username=홍길동1, score=100]


내림차순

//내림차순: Person클래스의 score값 기준
        list.stream()
                .sorted(Comparator.comparing(Person::getScore, Comparator.reverseOrder()))
                .forEach(System.out::println);
        System.out.println();

//Student [username=홍길동1, score=100]
//Student [username=홍길동4, score=90]
//Student [username=홍길동3, score=85]
//Student [username=홍길동2, score=80]
//Student [username=홍길동5, score=70]





---




skip(n) : n개가 skip됨

list.stream()
//            .sorted(Comparator.comparing(Person::getScore))
                .sorted(Comparator.comparingInt(Person::getScore))
                .skip(2)
                .forEach(System.out::println);
        System.out.println();

//Student [username=홍길동3, score=85]
//Student [username=홍길동4, score=90]
//Student [username=홍길동1, score=100]





limit(n) : n개 얻기

list.stream()
//            .sorted(Comparator.comparing(Person::getScore))
//            .sorted(Comparator.comparingInt(Person::getScore))
            .limit(3)
            .forEach(System.out::println);
        System.out.println();

//Student [username=홍길동1, score=100]
//Student [username=홍길동2, score=80]
//Student [username=홍길동3, score=85]
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함