[람다 스트림] [문자열] [자바] [백준] - 2870 수학숙제

SlowAnd·2024년 1월 12일
0

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

package boj.String;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class boj_2870 {
    public static void main(String[] args) throws IOException {
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(r.readLine());

        List<String> lines = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            lines.add(r.readLine());
        }

        Pattern pattern = Pattern.compile("\\d+");

        List<BigInteger> numbers = lines.stream()
                .flatMap(line -> {
                    Matcher matcher = pattern.matcher(line);
                    List<BigInteger> nums = new ArrayList<>();
                    while (matcher.find()) {
                        nums.add(new BigInteger(matcher.group()));
                    }
                    return nums.stream();
                })
                .sorted()
                .collect(Collectors.toList());

        numbers.forEach(System.out::println);
    }
}

0개의 댓글