[람다 스트림] [문자열] [자바] [백준] - 1159 농구 경기

SlowAnd·2024년 1월 8일
0
post-thumbnail

https://www.acmicpc.net/problem/1159

특정 요소 카운팅 + 조건 필터링

package boj.String;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class boj_1159 {
    public static void main(String[] args) throws IOException {
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        int count = Integer.parseInt(r.readLine());
        StringBuilder result = new StringBuilder();
        
        List<String> arr = new ArrayList<>();
        while(count-->0){
            arr.add(r.readLine());
        }


        Map<String, Long> collect = arr.stream().map(i -> i.substring(0, 1))
                .collect(Collectors.groupingBy(i -> i, Collectors.counting()));

        collect.entrySet().stream()
                .filter(entry -> entry.getValue() >= 5)
                .map(Map.Entry::getKey)
                .sorted()
                .forEach(result::append);

        if (result.length() == 0) {
            result.append("PREDAJA");
        }

        System.out.println(result);
    }
}

0개의 댓글