Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return string from lambda expression stream().filter()

Tags:

java

lambda

I have something like this and I want to get a String as a result

    List<Profile> profile;
    String result = profile
                       .stream()
                       .filter(pro -> pro.getLastName().equals("test"))
                       .flatMap(pro -> pro.getCategory())

getCategory() should return an string but not sure what I have to use to return a string, I tried several things but any worked

Any idea?

Thanks

like image 374
William Avatar asked Oct 26 '25 01:10

William


1 Answers

List<Profile> profile;
String result = profile.stream()
                       .filter(pro -> pro.getLastName().equals("test"))
                       .map(pro -> pro.getCategory())
                       .findFirst()
                       .orElse(null);
like image 50
Downhillski Avatar answered Oct 28 '25 15:10

Downhillski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!