Unable to infer type of functional interface in groupBy Java 8

I am not sure whether this was answered earlier or not. Can someone please tell me what is the problem with the third groupByone I wrote? Why can't he conclude?

class TestGroupBy
{
    enum LifeCycle {
        ANNUAL, PERENNIAL
    }

    private String name;
    private LifeCycle lifeCycle;

    TestGroupBy(String name, LifeCycle lifeCycle) {
        this.name = name;
        this.lifeCycle = lifeCycle;
    }

    LifeCycle getLifeCycle() {
        return this.lifeCycle;
    }

    static EnumMap mySupplier() {
        return new EnumMap(TestGroupBy.class);
    }

    public static void main(String[] args) {
        List<TestGroupBy> garden = new ArrayList<>();
        garden.add(new TestGroupBy("Test1", TestGroupBy.LifeCycle.ANNUAL));
        garden.add(new TestGroupBy("Test2", TestGroupBy.LifeCycle.PERENNIAL));
        garden.add(new TestGroupBy("Test4", TestGroupBy.LifeCycle.ANNUAL));
        garden.add(new TestGroupBy("Test5", TestGroupBy.LifeCycle.PERENNIAL));

        // This works
        garden.stream()
            .collect(Collectors.groupingBy(e -> e.getLifeCycle()));

        // This works
        garden.stream()
            .collect(Collectors.groupingBy(
                e -> e.getLifeCycle(),
                TestGroupBy::mySupplier,
                Collectors.toSet()
            ));
        // This does not work
        garden.stream()
            .collect(Collectors.groupingBy(
                e -> e.getLifeCycle(),   // Can not resolve method getLifeCycle()
                new EnumMap(TestGroupBy.class),
                Collectors.toSet()));
    }
}
+4
source share
3 answers

Stop using raw types!

These are mySupplierwithout raw types:

static EnumMap<LifeCycle, Set<TestGroupBy>> mySupplier() {
    return new EnumMap<>(LifeCycle.class);
}

The key type EnumMapshould be an enum type, so you should use LifeCycleas the first argument. The second argument is what the collector you use at the end returns. You used toSethere, so I assume you need a kit TestGroupBy.

, , LifeCycle.class EnumMap!

:

    garden.stream()
            .collect(Collectors.groupingBy(
                    e -> e.getLifeCycle(),
                    () -> new EnumMap<>(LifeCycle.class),
                    Collectors.toSet()));

, () ->, .

+4

, , . -, Collectors.groupingBy. EnumMap, Supplier<EnumMap>.

-, EnumMap TestGroupBy.class, TestGrouBy . new EnumMap<>(LifeCycle.class):

garden.stream()
        .collect(Collectors.groupingBy(
              e -> e.getLifeCycle(),   // Can not resolve method getLifeCycle()
              () -> new EnumMap<>(LifeCycle.class),
              Collectors.toSet()));

mySupplier(), . :

static EnumMap mySupplier() {
    return new EnumMap<>(LifeCycle.class);
}
+2
static EnumMap mySupplier() {
    return new EnumMap(TestGroupBy.class);
}

. :

static EnumMap<LifeCycle, Set<TestGroupBy>> mySupplier() {
    return new EnumMap<>(TestGroupBy.LifeCycle.class); // Key class is changed.
}

FInally , :

garden.stream()
        .collect(Collectors.groupingBy(
            e -> e.getLifeCycle(),   
            new EnumMap(TestGroupBy.class),
            Collectors.toSet()));

:

new EnumMap(TestGroupBy.class),

groupingBy , , .

, new EnumMap(TestGroupBy.LifeCycle.class) .

-1

Source: https://habr.com/ru/post/1691940/


All Articles