# Java Array Tutorials
# Java Array Programs
➤ Find Length of Array
➤ Different ways to Print Array
➤ Sum of Array Elements
➤ Average of Array Elements
➤ Sum of Two Arrays Elements
➤ Compare Two Arrays in Java
➤ 2nd Largest Number in Array
➤ How to Sort an Array in Java
➤ Reverse an Array in Java
➤ GCD of N Numbers in Java
➤ Linear Search in Java
➤ Binary Search in Java
➤ Copy Array in Java
➤ Merge 2 Arrays in Java
➤ Merge two sorted Arrays
➤ Largest Number in Array
➤ Smallest Number in Array
➤ Remove Duplicates
➤ Insert at Specific Position
➤ Add Element to Array
➤ Remove Element From Array
➤ Count Repeated Elements
➤ More Array Programs
Java Matrix Programs
➤ Matrix Tutorial in Java
➤ Print 2D Array in Java
➤ Print a 3×3 Matrix
➤ Sum of Matrix Elements
➤ Sum of Diagonal Elements
➤ Row Sum – Column Sum
➤ Matrix Addition in Java
➤ Matrix Subtraction in Java
➤ Transpose of a Matrix in Java
➤ Matrix Multiplication in Java
➤ Menu-driven Matrix Operations
Check if all elements in an Array are Equal Java | Here, the problem is to check whether all the elements in an array are equal or not. As we just need to check the elements in the given array, therefore there is no need to compare between two or more arrays. Hence to solve this problem we just need one array of any type.
Let us see some example to check if all elements in an array are equal Java:-
1) array1[ ] = {1,1,1,1}
Yes, all array elements are equal.
2) array2[ ] = {1,2,3,4}
No, the array elements are not equal.
3) array3[ ] = {“apple”, “apple”}
Yes, the elements of the string array are equal.
4) double array4[ ] = {1223345.5, 1289067.7, 1234678.6}
No, the given double array elements are not equal.
Check If all Elements in an Array are Equal Java using HashSet
Set only store unique elements. For example, if we try to store number 123 any number of times then it will store them only once. Hence the idea is to create a set and store all elements of the given array in the set.
After storing in the set, if the set contains only 1 element then all arrays was equal else if the set contains different elements.
Check If all Elements in an Array are Equal Java by Set
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Integer[] array = { 10, 10, 10, 10 };
Set<Integer> set = new HashSet<>(Arrays.asList(array));
if (set.size() == 1) {
System.out.println("All elements of array are same");
} else {
System.out.println("All elements of array are not same");
}
}
}
Output:-
All elements of array are same
Check If all Elements in an Array are Equal Java using Set & Stream
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
int[] array = { 10, 10, 10, 10, 10 };
Set<Integer> result =
Arrays.stream(array).boxed().collect(Collectors.toSet());
boolean checkequal = result.size() == 1;
System.out.println(checkequal);
}
}
Output:-
true
Check If all Elements in an Array are Equal Java by using Stream.distinct() method
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] array = { 10, 10, 10, 10, 10 };
boolean checkequal =
Arrays.stream(array).distinct().count() == 1;
System.out.println(checkequal);
}
}
Output:-
true
Check If all Elements in an Array are Equal Java by using Stream.allMatch() method
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] array = { 1, 1, 1, 1, 1 };
boolean checkequal =
array.length == 1 ||
Arrays.stream(array).allMatch(t -> t == array[0]);
System.out.println(checkequal);
}
}
Output:-
true
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!