반응형

오랜만에 stream sorted사용

사전순 정렬시

Comparator.naturalOrder() 혹은

String::compareTo 사용하면 된다고 함

import java.util.*;

public class Problem1181 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int cnt = Integer.parseInt(sc.nextLine());
        String[] words = new String[cnt];
        for(int i=0; i<cnt; i++){
            words[i] = sc.nextLine();
        }

        String[] newWords = Arrays.stream(words)
                        .distinct()
                        .sorted(Comparator.comparingInt(String::length)
                        .thenComparing(Comparator.naturalOrder()))
                        .toArray(String[]::new);

        for(String w : newWords){
            System.out.println(w);
        }
    }
    
}

 

 

반응형

+ Recent posts