Java streams on List<Object> get a property of any object matching filter()

I want to get the property of any object matching the filter() of the stream, but I am unable to.

transactionsList.stream()
    .filter(transaction -> transaction.getPayerIban() != null)
    .findFirst()
    //Here I get an Optional<Object>
    .ifPresent()
    .map(Transaction::getName)

I don’t know what to do inside the .ifPresent() stream operation, but I want to get any transaction name of the matching transactions

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

Remove the ifPresent():

Optional<String> optionalName = transactionsList.stream()
    .filter(transaction -> transaction.getPayerIban() != null)
    .findFirst()
    .map(Transaction::getName);