Check if all items in a List are identical in Java
This post will discuss how to check if all items in a List are identical in Java.
1. Using Stream.distinct()
method
The idea is to get a sequential stream over the elements in the list and select the distinct elements from the stream using the distinct()
method. If all elements in a List are identical, then the count of elements in this stream should be exactly 1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static void main(String[] args) { List<Integer> nums = IntStream.range(1, 10).map(i -> 0).boxed() .collect(Collectors.toList()); boolean allEqual = nums.stream().distinct().count() <= 1; System.out.println(allEqual); // true } } |
Note that the distinct()
method compare elements according to Object.equals(Object)
method. Therefore, your class should override equals() and hashCode() methods to test equality.
2. Using Stream.allMatch()
method
The allMatch()
method returns true if all elements of the stream matches with the given predicate. It can be used as follows to check if all elements in a list are the same. Note the additional check in the code to handle an empty list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.List; import java.util.Objects; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static void main(String[] args) { List<Integer> nums = IntStream.range(1, 10).map(i -> 0).boxed() .collect(Collectors.toList()); Predicate<Integer> predicate = obj -> Objects.equals(nums.get(0), obj); boolean allEqual = nums.isEmpty() || nums.stream().allMatch(predicate); System.out.println(allEqual); // true } } |
3. Using Set
The most widely used solution to check for identical elements in a list involves converting the list into a set and then checking if the size of the set is 1 or not. This works since the Set data structure silently discards duplicates.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Collections; import java.util.HashSet; import java.util.List; class Main { public static void main(String[] args) { List<Integer> nums = Collections.nCopies(10, 0); boolean allEqual = new HashSet<>(nums).size() <= 1; System.out.println(allEqual); // true } } |
4. Using Collections.frequency()
method
Finally, you can get the count of any element in the list using the Collections.frequency()
method. If its frequency is equal to the number of elements in the list, then we can say that the list has all identical elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Collections; import java.util.List; class Main { public static void main(String[] args) { List<String> s = Collections.nCopies(5, ""); boolean allEqual = s.isEmpty() || Collections.frequency(s, s.get(0)) == s.size(); System.out.println(allEqual); // true } } |
That’s all about checking if all items in a List are identical in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)