Functional Programming - FindFirst

Imperative Vs Functional

About

This page is about the FindFirst function in the functional programming paradigm.

As its name indicates, it will find the first element in the collection.

It's generally followed by a delivery function such as:

  • ifPresent - if you want to do other operations
  • or orElseThrow - if you want to stop and throw an exception

Example

Java: FindFirst or throw

Example in Java:

list
  .stream()
  .filter(txt -> txt.startsWith(startsWith))
  .findFirst()
  .orElseThrow(() -> new NoSuchElementException("No element found"));





Discover More
Imperative Vs Functional
Code - Functional programming (FP) - Collection Operations

Functional programming (FP) defines standard operations on collections. It is a declarative paradigm that treats computation as the evaluation of mathematical functions. Most of the operations, you perform...



Share this page:
Follow us:
Task Runner