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

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

Collect values from an enum with stream doesn't work

问题

以下是翻译好的部分:

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

例如,我试图这样做:

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

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

排序类型

  1. public enum SortingType {
  2. DISTANCE("DISTANCE", 101, "按距离排序POI"),
  3. PRICE("PRICE", 104, "按价格排序POI"),
  4. UNKNOWN("UNKNOWN", 000, "未知排序选项");
  5. private String name;
  6. private Integer identifier;
  7. private String description;
  8. SortingType(final String name, final Integer identifier, final String description) {
  9. this.name = name;
  10. this.identifier = identifier;
  11. this.description = description;
  12. }
  13. public static SortingType getByName(final String name) {
  14. return Stream.of(values()).filter(u -> u.name.equalsIgnoreCase(name)).findFirst().orElse(SortingType.UNKNOWN);
  15. }
  16. public static SortingType getByIdentifier(final Integer identifier) {
  17. return Stream.of(values()).filter(u -> u.identifier.equals(identifier)).findFirst().orElse(SortingType.UNKNOWN);
  18. }
  19. public String getDescription() {
  20. return description;
  21. }
  22. public Integer getIdentifier() {
  23. return identifier;
  24. }
  25. public String getName() {
  26. return name;
  27. }
英文:

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:

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

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

Sorting Type

  1. public enum SortingType {
  2. DISTANCE("DISTANCE", 101, "Sorting POIs by distance"),
  3. PRICE("PRICE", 104, "Sorting POIS by price"),
  4. UNKNOWN("UNKNOWN", 000, "Unknown sorting option");
  5. private String name;
  6. private Integer identifier;
  7. private String description;
  8. SortingType(final String name, final Integer identifier, final String description) {
  9. this.name = name;
  10. this.identifier = identifier;
  11. this.description = description;
  12. }
  13. public static SortingType getByName(final String name) {
  14. return Stream.of(values()).filter(u -> u.name.equalsIgnoreCase(name)).findFirst().orElse(SortingType.UNKNOWN);
  15. }
  16. public static SortingType getByIdentifier(final Integer identifier) {
  17. return Stream.of(values()).filter(u -> u.identifier.equals(identifier)).findFirst().orElse(SortingType.UNKNOWN);
  18. }
  19. public String getDescription() {
  20. return description;
  21. }
  22. public Integer getIdentifier() {
  23. return identifier;
  24. }
  25. public String getName() {
  26. return name;
  27. }

答案1

得分: 1

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

  1. public enum SortingType {
  2. // .....
  3. public static Stream<SortingType> stream() {
  4. return Stream.of(values());
  5. }
  6. }
英文:

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

  1. public enum SortingType {
  2. // .....
  3. public static Stream&lt;SortingType&gt; stream() {
  4. return Stream.of(values());
  5. }
  6. }

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:

确定