본문 바로가기
스터디

[모던 자바 인 액션]6장. Collector 인터페이스 활용 예제

by eunoo 2023. 7. 21.

1. reverse 구현

public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

        List<Integer> result = numbers.stream()
                .collect(new ReverseCollector<>());

        System.out.println(result);  // [5, 4, 3, 2, 1]
}
package modernjavainaction.chap06;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class ReverseCollector<T> implements Collector<T,List<T>,List<T>> {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1,2,3,4,5);
        List<Integer> result = numbers.stream().collect(new ReverseCollector<Integer>());

        System.out.println(result);
    }
    @Override
    public Supplier<List<T>> supplier() {
        return ArrayList::new;
    }

    @Override
    public BiConsumer<List<T>, T> accumulator() {
        BiConsumer<List<T>,T> d = (list, item) -> {
            list.add(0, item);
        };
        return d;
    }

    @Override
    public BinaryOperator<List<T>> combiner() {
        return (list1, list2) -> {
            list1.addAll(list2);
            return list1;
        };
    }

    @Override
    public Function<List<T>, List<T>> finisher() {
        return Function.identity();
    }

    @Override
    public Set<Characteristics> characteristics() {
        return Collections.unmodifiableSet(EnumSet
                .of(Characteristics.IDENTITY_FINISH,Characteristics.CONCURRENT));
    }
}

 

2. String concat 구현

public static void main(String[] args) {
        List<String> words = Arrays.asList("Hello", " ", "world", "!");

        String result = words.stream()
                .collect(new StringConcatCollector());

        System.out.println(result);  // Hello world!
}
package modernjavainaction.chap06;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;

public class StringConcatCollector implements Collector<CharSequence,StringBuilder,String> {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("Hello", " ", "world","!");

        String result = words.stream().collect(new StringConcatCollector());
        System.out.println(result);
    }
    @Override
    public Supplier<StringBuilder> supplier() {
        return StringBuilder::new;
    }

    @Override
    public BiConsumer<StringBuilder, CharSequence> accumulator() {
        return StringBuilder::append;
    }

    @Override
    public BinaryOperator<StringBuilder> combiner() {
        return (sb1, sb2) -> {sb1.append(sb2); return sb1;};
    }

    @Override
    public Function<StringBuilder, String> finisher() {
        return StringBuilder::toString;
    }

    @Override
    public Set<Characteristics> characteristics() {
        return Collections.emptySet();
    }
}

댓글