英文:
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<SortingType> stream() {
return Stream.of(values());
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论