Java Program to Compare two Boolean Arrays Last Updated : 17 Dec, 2020 Comments Improve Suggest changes Like Article Like Report Two arrays are equal if they contain the same elements in the same order. In java, we can compare two Boolean Arrays in 2 ways: By using Java built-in method that is .equals() method.By using the Naive approach. Examples: Input : A = [true , true , false] A1 = [true, true, false] Output: Both the arrays are equal. Input : A = [true, true, false] A1 = [true, false, true] Output: Both the arrays are not equal. Method 1: Array class in Java provides the method Arrays.equals() to check whether two arrays are equal or not. Syntax: public static boolean equals(boolean[] a, boolean[] a1) Parameters: a - one array to be tested for equality a1 - another array to be tested for equality Returns: It returns true if both the arrays are equal else returns false. Code: Java // Java Program to Compare two Boolean // Arrays using built-in function import java.util.Arrays; class GFG { public static void main(String[] args) { // initializing both the boolean arrays boolean[] a = new boolean[] { true, true, false }; boolean[] a1 = new boolean[] { true, true, false }; // Displaying Array1 System.out.println("Array1..."); for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } // Displaying Array2 System.out.println("Array2..."); for (int j = 0; j < a1.length; j++) { System.out.println(a1[j]); } // comparing array1 and array2 boolean result = Arrays.equals(a, a1); if (result) { System.out.println("Both the arrays equal "); } else { System.out.println( "Both the arrays not equal "); } } } OutputArray1... true true false Array2... true true false Both the arrays equal Method 2: In this, we will use the Naive approach to compare two arrays.We can run a for loop and check every element of the array and compare each one of them. Java // Java Program to Compare two Boolean // Arrays using Naive approach import java.io.*; class GFG { public static void main(String[] args) { // initializing both the boolean arrays boolean[] a = new boolean[] { true, true, false }; boolean[] a1 = new boolean[] { true, true, false }; // Displaying Array1 System.out.println("Array1..."); for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } // Displaying Array2 System.out.println("Array2..."); for (int j = 0; j < a1.length; j++) { System.out.println(a1[j]); } // Comparing both the arrays for (int i = 0; i < a.length; i++) { // if any element is found different we will // print our ans and exit the program. if (a[i] != a1[i]) { System.out.println( "Both the arrays equal "); System.exit(0); } } System.out.println("Both the arrays equal "); } } OutputArray1... true true false Array2... true true false Both the arrays equal Comment More infoAdvertise with us Next Article Java Program to Compare two Boolean Arrays N nikhiltanna33 Follow Improve Article Tags : Java Technical Scripter Java Programs Java-Array-Programs Practice Tags : Java Similar Reads Java Program to convert integer to boolean Given a integer value, the task is to convert this integer value into a boolean value in Java. Examples: Input: int = 1 Output: true Input: int = 0 Output: false Approach: Get the boolean value to be converted. Check if boolean value is true or false If the integer value is greater than equal to 1, 2 min read Java Program to Convert String to boolean In Java, to convert a string to a Boolean, we can use Boolean.parseBoolean(string) to convert a string to a primitive Boolean, or Boolean.valueOf(string) to convert it to a Boolean object. The Boolean data type only holds two possible values which are true and false. If the string equals "true" (ign 3 min read Java Program to Find Common Elements Between Two Arrays Given two arrays and our task is to find their common elements. Examples:Input: Array1 = ["Article", "for", "Geeks", "for", "Geeks"], Array2 = ["Article", "Geeks", "Geeks"]Output: [Article, Geeks]Input: Array1 = ["a", "b", "c", "d", "e", "f"], Array2 = ["b", "d", "e", "h", "g", "c"]Output: [b, c, d, 4 min read Java Program to Check if Two of Three Boolean Variables are True Boolean values are true/false. In this program, we need to check if any two out of three boolean values are true or not. We will be discussing two major approaches to do it: using an if-else.using the ternary operator. Approach 1: Using if-else condition We initialize three boolean variable flags, w 3 min read Comparing two ArrayList In Java Java provides a method for comparing two Array List. The ArrayList.equals() is the method used for comparing two Array List. It compares the Array lists as, both Array lists should have the same size, and all corresponding pairs of elements in the two Array lists are equal. Example: Input : ArrayLis 2 min read How to Compare two Collections in Java? Java Collection provides an architecture to store and manipulate the group of objects. Here we will see how to compare Elements in a Collection in Java. Steps: Take both inputs with help of asList() function.Sort them using Collections.sort() method.Compare them using equals() function.Print output. 2 min read equals() and deepEquals() Method to Compare two Arrays in Java Arrays. equals() method does not compare recursively if an array contains another array on other hand Arrays. deepEquals() method compare recursively if an array contains another array. Arrays.equals(Object[], Object[]) Syntax : public static boolean equals(int[] a, int[] a2) Parameters : a - one ar 3 min read How to Compare Two TreeMap Objects in Java? TreeMap class in java provides a way of storing key-value pairs in sorted order. The below example shows how to compare two TreeMap objects using the equals() method. It compares two TreeMap objects and returns true if both of the maps have the same mappings else returns false. Syntax: boolean equal 1 min read Check if Two Integers are Equal or Not in Java Checking two integers equal or not in Java is done by various approaches. Arithmetic operatorComparison OperatorsString functionsXOR operatorComplement (~) and bit-wise (&) operator Example Input: FirstNumber = 15 SecondNumber= 15 Output: Numbers are same Input: FirstNumber = 15 SecondNumber= 25 2 min read EnumMap equals() Method in Java with Examples The Java.util.EnumMap.equals(obj) in Java is used to compare the passed object with this EnumMap for the equality. It must be kept in mind that the object passed must be a map of the same type as the EnumMap.Syntax: boolean equals(Object obj) Parameter: The method takes one parameter obj of Object t 2 min read Like