Functional Java: How to use a List in a functional way vs the imperative way

Reading Time: 3 minutes

In my previous blog, we discussed how we can traverse a list in a functional way. There, first, we saw how we can do it in an imperative way and then moved ahead by modifying the code to reach the functional way. In this blog, we will explore some more uses cases of a list in a functional way and will compare it with the imperative way.

So let’s start.

We will be using the below list here:

final List<String> players = Arrays.asList("Virat", "Rohit",
 "Shikhar", "Rahul", "Rishabh", "Hardik", "MSD");

Transforming a list:

Convert a list of names to all capital letters.


 final List<String> uppercaseNames = new ArrayList();
        for(String player : players) {


List<String> uppercaseNames =

In an imperative way, we are creating a new list and then mutating it in the loop while in the functional approach, there is no mutation and the code is very clean.

Filter elements from a list:

From a list of players, let’s pick the ones that start with the letter R.


final List<String> startsWithR = new ArrayList<String>();

for (String player : players) {
    if (player.startsWith("R")) {


List<String> startsWithR =
        filter(player -> player.startsWith("R"))

Here, in an imperative way, we are again mutating a newly created list while there is no mutation in a functional way.

Picking an element from the list:

Let’s create a method that will look for an element that starts with a given letter, and prints it


public static void pickName(
        final List<String> players, final String startingLetter) {
    String foundName = null;
    for (String player : players) {
        if (player.startsWith(startingLetter)) {
            foundName = player;

    System.out.print(String.format("A name starting with %s: ", startingLetter));
    if (foundName != null) {
    } else {
        System.out.println("No name found");


public static void pickName(
        final List<String> names, final String startingLetter) {
    final Optional<String> foundName =
                    .filter(name ->name.startsWith(startingLetter))
    System.out.println(String.format("A name starting with %s: %s",
            startingLetter, foundName.orElse("No name found")));

You can see, the functional way is very clear and concise. We have used break in an imperative way and then later added the If condition to check the value of foundName variable while in a functional way, it is a very clean and simple approach where we have used Optional.

Joining Elements:

Let’s make a comma-separated string by joining the elements of List and print it.


String names = "";
for (int i = 0; i < players.size() - 1; i++) {
    names += players.get(i) + ", ";

if(players.size() > 0)
    names += players.get(players.size() - 1);



Approach 1:

String names =", "));

Approach 2:

String names = String.join(", ", players);

In an imperative approach, we had to use the normal for loop just to iterate before the last element and then later added the last element explicitly just to avoid the extra comma at the end while the functional approach is very neat and clean.


As a conclusion, we get the following benefits by using functional approach:

  • Need to write less code
  • Code is maintainable
  • No mutability
  • More focus on business logic instead of writing the basic coding
  • and many more.

That’s it. Hope this blog will be helpful to give you a better understanding of using List in a functinal way. Stay tuned !!!


Written by 

Rishi is a Lead Consultant, with experience of more than 7 years. Rishi is product focused developer who loves developing both front-end user interfaces and scalable back-end infrastructure. He is a good team player, quick learner and a humble person. He has good time management skills, aimed to give best results and fully dedicated towards his work & responsibilities. He is able to work as individual and as well as in team. He loves to share his knowledge, therefore he often writes technical blogs.