16

I'm trying, and failing, to retrieve all Enum values and place them in a list using Java 8 and streams. So far I've tried the two approaches below, but neither one returns the value.

What am I doing wrong?

Code:

public class Main {
public static void main(String[] args) {
    List<String> fruits1 = Stream.of(FruitsEnum.values())
                                 .map(FruitsEnum::name)
                                 .collect(Collectors.toList());

    List<String> fruits2 = Stream.of(FruitsEnum.values().toString())
                                 .collect(Collectors.toList());

    // attempt 1
    System.out.println(fruits1);
    // attempt 2
    System.out.println(fruits2);
}

enum FruitsEnum {
    APPLE("APPL"),
    BANANA("BNN");

    private String fruit;

    FruitsEnum(String fruit) {this.fruit = fruit;}

    String getValue() { return fruit; }

   }
}

Output:

[APPLE, BANANA]
[[LMain$FruitsEnum;@41629346]

Desired:

["AAPL", "BNN"]
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
Simply_me
  • 2,840
  • 4
  • 19
  • 27
  • @Aomine, this is not a duplicate because the marked solution in that question does not work here. In fact, I had incorporated it in my snippet (`fruits1`). – Simply_me Jan 05 '19 at 16:57
  • I will consider reopening if you can be more specific and show the result you're currently getting and what you expect instead. – Ousmane D. Jan 05 '19 at 16:57
  • @Aomine, the result is in the output section. – Simply_me Jan 05 '19 at 16:58
  • what do you expect instead? – Ousmane D. Jan 05 '19 at 16:58
  • 1
    What do you expect `FruitsEnum.values().toString()` to do? Actually it'll convert the array of values to a string not to array of strings as you may expect. – ETO Jan 05 '19 at 17:00
  • 2
    I added the desired output; yes @ETO good point. – Simply_me Jan 05 '19 at 17:01
  • 4
    use Stream.of(FruitsEnum.values()).map(FruitsEnum::getValue).collect(Collectors.toList()); – Amit Jan 05 '19 at 17:07
  • Why can't you simply write the enum like this, `public enum FruitsEnum { APPLE, BANANA; }` – Ravindra Ranwala Jan 05 '19 at 17:20
  • @Amit wins. He answered the correct answer while I was trying out the same thing. `public static void main(String[] args) { List fruits1 = Stream.of(FruitsEnum.values()).map(FruitsEnum::getValue).collect(Collectors.toList()); System.out.println(fruits1); } ` – Capricorn1 Jan 05 '19 at 17:25
  • @RavindraRanwala see [this](https://books.google.co.uk/books?id=ka2VUBqHiWkC&pg=PA161&lpg=PA161&dq=Use+EnumMap+instead+of+ordinal+indexing&source=bl&ots=yYIfLfr5P-&sig=uDv2JHThXFPNI7LdoYZED3O39JE&hl=en&sa=X&ei=0cEUUcmiNom-0QWRvYDAAw#v=onepage&q=Use%20EnumMap%20instead%20of%20ordinal%20indexing&f=false) – Hadi J Jan 05 '19 at 17:26
  • @HadiJ You are referring to 2nd edition. How about the same in 3rd edition? However this page is not readable for me. – Ravindra Ranwala Jan 05 '19 at 17:28
  • @RavindraRanwala how about [this](http://thefinestartist.com/effective-java/31) ? – Hadi J Jan 05 '19 at 17:30
  • @HadiJ We are NOT using ordinals here? – Ravindra Ranwala Jan 05 '19 at 17:40

3 Answers3

13

You need to map with getValue

List<String> fruits = Stream.of(FruitsEnum.values())
                            .map(FruitsEnum::getValue) // map using 'getValue'
                            .collect(Collectors.toList());
System.out.println(fruits);

this will give you the output

[APPL, BNN]
Naman
  • 27,789
  • 26
  • 218
  • 353
6

This should do the trick:

Arrays.stream(FruitsEnum.values())
      .map(FruitsEnum::getValue)
      .collect(Collectors.toList());
ETO
  • 6,970
  • 1
  • 20
  • 37
2

Using of EnumSet is other way:

 List<String> fruits = EnumSet.allOf(FruitsEnum.class)
     .stream()
     .map(FruitsEnum::getValue)
     .collect(Collectors.toList());
Hadi J
  • 16,989
  • 4
  • 36
  • 62