How to Compare Two ArrayList in JavaThere are following ways to compare two ArrayList in Java:
Java equals() methodJava equals() method of List interface compares the specified object with the list for equality. It overrides the equals() method of Object class. Syntax This method accepts an object to be compared for equality with the list. It returns true if the specified object is equal to the list, else returns false. Example In the following example, we have create two ArrayList firstList and secondList. Comparing both list by using equals() method, it returns true. We have added another element in the secondList to create a difference between firstList and secondList. Now, if we perform comparison, it returns false. Output [Apple, Pears, Guava, Mango] [Apple, Pears, Guava, Mango] true false Java removeAll() method (finding the missing element)Java removeAll() method of ArrayList class is used to remove all elements from the list that are contained in the specified collection. It overrides the removeAll() method of AbstractCollection<E> class. Syntax This method accepts Collection as a parameter containing elements to be removed from this list. It returns true if this list changed as a result of the call. Example In the following example, we have created two ArrayList firstList and secondList. The removeAll() method removes all the elements of the firstList because the same elements are also present in the secondList, except Papaya. So, Papaya is the missing element in the firstList. Hence, it returns Papaya. The method returns an empty list [] if both the list have same elements. Output [Papaya] Let's see another example of removeAll() method that returns the elements from firstList which are not present is the secondList. Example Output First array list: [12, 4, 67, 90, 34] Second array list: [12, 4, 67, 0, 34] Un-common element of the first list: [90] Java retainAll() method (returns common elements in both lists)Java retainAll() method of ArrayList class retains only the elements of the list that are contained in other list also. It overrides the retainAll() method of AbstarctCollection<E> class. Syntax This method accepts a Collection as a parameter that contains elements to be retained in the list. It returns true if this list changed as a result of the call. Example In this example, we have created two ArrayList firstList and secondList by using the asList() method of the Arrays class. The asList() method returns a list view of the specified array. Output First arraylist: [M, W, J, K, T] Second arraylist: [M, W, E, K, T] Common elements in both list: [M, W, K, T] Java ArrayList.contains() methodJava ArrayList.contains() method is used for comparing two elements of different ArrayList. Java ArrayList.contains() method overrides the contains() method of AbstrarctCollection<E> class. Syntax This method parses an element whose presence in the list is to be checked. It returns true if the element is matched, else returns false. Example In this example, we have created two ArrayList firstList and secondList of String type. We have compared these ArrayList using contains() method. If the elements of firstList match with the elements of the secondList, it return Yes and stores this value into thirdList. Similarly, if the element does not match, it return No. Output [Yes, No, No] Java contentEquals() methodJava contentEquals() method compares the String with the StringBuffer and returns a boolean value. It belongs to String class. Syntax This method accepts StringBuffer as a parameter to compare against the String. It returns true if the String represents the same sequence of characters as the specified StringBuffer, else returns false. Example In this example, we have created two ArrayList firstList and secondList of String type. We have created a static method compareList() which parses two ArrayList ls1 and ls2 as an argument and returns a boolean value. The method converts a list into String. The contentEquals() method compares the String to the specified StringBuffer. We have added another element in the secondList to create the difference between both lists and again call the compareList() method, which returns false. Output When Lists are same: true When Lists are not same: false Java Stream InterfaceJava Stream is an interface. It belong to java.util package. It provides a filter() method to filter stream elements on the basis of given elements. It is an intermediate process. Syntax This method accepts Predicate (functional interface) as an argument and returns a new stream of resultant elements. collect() methodAnother method is used in the following example is the collect() method of Stream interface. It performs a mutable reduction operation on the elements of this stream using a Collector. The Collector encapsulates the functions used as arguments to collect(Supplier, BiConsumer, BiConsumer). It allows to reusing of collection strategies and composition of collect operations. It is a terminal operation. Syntax Collectors.toList() methodThe toList() is a static method of Collectors class. It belong to java.util.Stream.Collectors class. Syntax Where T is the type of elements. It returns a Collector which collects all the input elements into a List, in an encounter (unordered) order. Example In the following example, we have created two ArrayList firstList and secondList of String type. The filter() method returns a List of common elements which are present in both Lists. Output First List: [Java, Python, Ruby, Go] Second List: [Java, Python, Ruby, Go, Perl] Common elements: [Java, Python, Ruby, Go] Next TopicJava Tutorial |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India