Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java: Getting all names and Labels in an Enum as a List Object

How do I stream a Enum Value-Label into a List object? Two

public enum ProductActions {
    BUY("Buy"),
    SELL("Sell"),
    Transfer("Transfer"),

    public final String label;

    ProductActions(String label) {
        this.label = label;
    }
}

Want to transfer into this List<ProductActionItem>

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ProductActionItem {
    private String productAction;
    private String productLabel;

}

Working on Code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

return Arrays.stream(productActions.values())
             .map(e -> e.label).collect(Collectors.toList());

Trying to use this Resource:

https://stackoverflow.com/a/28828117/15435022

>Solution :

You are on the right track you just need to convert the enums to your new class. Assuming appropriate getter in the enum and constructor:

Arrays.stream(ProductActions.values())
    .map(pav -> new ProductActionItem(pav.name(), pav.label())
    .toList();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading