How to determine if all elements are the same in a Java List?



In this article, we will explore how to determine if all elements in a Java List are the same. The following are the ways to achieve this:

Using a For Loop

We will iterate through the list and compare each element with the first element. If we find any element that is not equal to the first element, we will return false. If we reach the end of the list without finding any different element, we will return true.

Example

In the below example, we will create a list of integers and then use a for loop to check if all elements are the same.

import java.util.ArrayList;
import java.util.List;
public class SameElements{
   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>();
      
      list.add(1);
      list.add(1);
      list.add(1);
      list.add(1);
      
      boolean allSame = true;
      Integer firstElement = list.get(0);
      
      for (Integer element : list) {
         if (!element.equals(firstElement)) {
            allSame = false;
            System.out.println("Found different element: " + element);
            break;
         }
      }
      
      System.out.println("All elements are the same: " + allSame);
   }
}

Output

Following is the output of the above code:

All elements are the same: true

Using Stream API

Now, let's use the Stream API to check if all elements in a list are the same. we will be using the distinct() method to filter out unique elements and then check if the size of the resulting stream is 1.

The distinct() Method filters out unique elements from the stream. If all elements are the same, the size of the distinct stream will be 1.

Example 1

In the following example, let's take a list and use the Stream API to determine if all elements are the same.

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class SameElements {
   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>();
      
      list.add(2);
      list.add(2); 
      list.add(2);
      list.add(2);
      
      boolean allSame = list.stream().distinct().count() == 1;
      
      System.out.println("All elements are the same: " + allSame);
   }
}

Output

Following is the output of the above code:

All elements are the same: true

Example 2

In the following example, we will create a list of strings and put some different elements in it. Then we will use the Stream API to check if all elements are the same.

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class SameElements {
   public static void main(String[] args) {
      List<String> list = new ArrayList<>();
      
      list.add("apple");
      list.add("apple");
      list.add("banana");
      list.add("apple");
      
      boolean allSame = list.stream().distinct().count() == 1;
      
      System.out.println("All elements are the same: " + allSame);
   }
}

Output

Following is the output of the above code:

All elements are the same: false
Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-06-10T15:38:07+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements