Zliczanie wystąpień cyfr z tablicy w Java 8

0

Czy mógłby mi ktoś pomóc zapisać tę prostą funkcyjkę za pomocą streamów? Trochę uczę się API javy 8 i widze jej ograniczenia albo to tylko moja tępota.

Funkcja zlicza ilość cyfr mniejszych od "day", pomiędzy "day+1" a "day+7" oraz pozostałe. Np. dla:

        int[] deadline = {1, 2, 3, 4, 5};
        int day = 2;

        tasksTypes(deadline, day);

Funkcja powinna zwrócić:

[2, 3, 0]
int[] tasksTypes(int[] deadline, int day) {
        int[] tab = new int[3];

        for (int i : deadline) {
            if (i <= day) {
                tab[0] += 1;
            } else if (i <= day + 7 & i >= day + 1) {
                tab[1] += 1;
            } else
                tab[2] += 1;
        }
        return tab;
    }
1

To raczej nie ograniczenia Javy 8 lub czyjaś tępota, ale stare przyzwyczajnia ;)

Sposobów jest dużo, wskazuję dwa pierwsze z góry:

        int[] oldArray = {0, 1, 5, 5, 10, 11, 12};
        int day = 0;

        IntPredicate lessThanDay = i -> i <= day;
        IntPredicate betweenWeek = i -> i <= day + 7 && i >= day + 1;
        IntPredicate moreThanWeek = i -> i > day + 7;

        int[] array = new int[3];
        array[0] = (int) IntStream.of(oldArray).filter(lessThanDay).count();
        array[1] = (int) IntStream.of(oldArray).filter(betweenWeek).count();
        array[2] = (int) IntStream.of(oldArray).filter(moreThanWeek).count();

        IntStream.of(0, 1, 2)
                .mapToObj(i -> i+"=" + array[i])
                .forEach(System.out::println);

        IntFunction<Integer> classifier = i -> lessThanDay.test(i)? 0 :
                                        (moreThanWeek.test(i)? 2 : 1);
        Map<Integer, Long> map = IntStream.of(oldArray)
                .mapToObj(classifier)
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
        map.entrySet().stream()
                .map(e -> e.getKey() + "=" + e.getValue())
                .forEach(System.out::println);
2
public class Main
{
    public static void main(String[] args) {
        int[] deadline = {1, 2, 3, 4, 5};
        int day = 2;

        Task task = new Task(day,deadline);
        int [] result = task.execute();

        System.out.println(Arrays.toString(result));
    }

}
public class Task
{
    private int day;
    private int [] deadline;

    public Task(int day, int[] deadline)
    {
        this.day = day;
        this.deadline = deadline;
    }

    public int [] execute()
    {
        Counter counter =  Arrays.stream(deadline).collect(Counter::new,Counter::apply,Counter::combine);

        return counter.getResult();
    }


    private class Counter
    {
        private int [] result = new int[3];

        public Counter()
        {

        }

        public void apply(int n)
        {
            if(n <= day) result[0] ++;
            else if(n >= (day + 1) && n <= (day + 7)) result[1] ++;
            else result[2] ++;
        }

        public void combine(Counter c)
        {
            result = Arrays.copyOf(c.result,c.result.length);
        }

        public int [] getResult()
        {
            return result;
        }
    }
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1