用流(stream)从枚举中收集值不起作用。

huangapple go评论63阅读模式
英文:

Collect values from an enum with stream doesn't work

问题

以下是翻译好的部分:

我想要一个包含枚举几乎所有值的列表。我尝试使用流来实现,但我不知道为什么它不起作用。

例如,我试图这样做:

SortingType.stream() //
                .filter(d -> !d.getName().equals(SortingType.UNKNOWN.getName()))
                .forEach(u -> {
                    sortingList.add(createSortingBE(locale, u, u.name().equalsIgnoreCase(sortingType.name())));
                });

.stream() 部分显示为红色,并显示以下消息:"无法解析方法 'stream' 在 'SortingType' 中"。

排序类型

public enum SortingType {
    DISTANCE("DISTANCE", 101, "按距离排序POI"),
    PRICE("PRICE", 104, "按价格排序POI"),
    UNKNOWN("UNKNOWN", 000, "未知排序选项");

    private String name;
    private Integer identifier;
    private String description;

    SortingType(final String name, final Integer identifier, final String description) {
        this.name = name;
        this.identifier = identifier;
        this.description = description;
    }

    public static SortingType getByName(final String name) {
        return Stream.of(values()).filter(u -> u.name.equalsIgnoreCase(name)).findFirst().orElse(SortingType.UNKNOWN);
    }

    public static SortingType getByIdentifier(final Integer identifier) {
        return Stream.of(values()).filter(u -> u.identifier.equals(identifier)).findFirst().orElse(SortingType.UNKNOWN);
    }

    public String getDescription() {
        return description;
    }

    public Integer getIdentifier() {
        return identifier;
    }

    public String getName() {
        return name;
}
英文:

I want to have a list with almost all the values from an enum. I tried to do that with the help of stream but I don't know why it is not working.

For example I am trying to do like this:

 SortingType.stream() //
            .filter(d -> !d.getName().equals(SortingType.UNKNOWN.getName()))
            .forEach(u -> {
                sortingList.add(createSortingBE(locale, u, u.name().equalsIgnoreCase(sortingType.name())));
            }); 

.stream() appears with red and I receive this message : " Cannot resolve method 'stream' in 'SortingType' "

Sorting Type

public enum SortingType {
    DISTANCE("DISTANCE", 101, "Sorting POIs by distance"),
    PRICE("PRICE", 104, "Sorting POIS by price"),
    UNKNOWN("UNKNOWN", 000, "Unknown sorting option");

    private String name;
    private Integer identifier;
    private String description;

    SortingType(final String name, final Integer identifier, final String description) {
        this.name = name;
        this.identifier = identifier;
        this.description = description;
    }

    public static SortingType getByName(final String name) {
        return Stream.of(values()).filter(u -> u.name.equalsIgnoreCase(name)).findFirst().orElse(SortingType.UNKNOWN);
    }

    public static SortingType getByIdentifier(final Integer identifier) {
        return Stream.of(values()).filter(u -> u.identifier.equals(identifier)).findFirst().orElse(SortingType.UNKNOWN);
    }

    public String getDescription() {
        return description;
    }

    public Integer getIdentifier() {
        return identifier;
    }

    public String getName() {
        return name;
    }

答案1

得分: 1

你应该简单地像这样添加 stream() 方法。

public enum SortingType {

    // .....

    public static Stream<SortingType> stream() {
        return Stream.of(values());
    }
}
英文:

You should simply add the stream() method like this.

public enum SortingType {

    // .....

    public static Stream&lt;SortingType&gt; stream() {
        return Stream.of(values());
    }
}

huangapple
  • 本文由 发表于 2020年7月28日 22:46:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63136851.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定