Compare each element in List Java

Comparing two ArrayLists could be a tricky task for beginner programmers. On this page, we are going to share some of the best ways to compare two ArrayLists.

Compare ArrayLists using Equal[]:

You can easily compare the two ArrayLists by simply using the equals[] method. This method returns a boolean value[True or False]. So, if the result is true, the ArrayLists are equal; otherwise, they are not equal.

Source Code:

import java.util.ArrayList; public class ComparingArrayLists { //using equals[] public static void main[String[] args] { //initializing two array lists ArrayList < String > list1 = new ArrayList < > []; ArrayList < String > list2 = new ArrayList < > []; //storing data in list 1 list1.add["Java"]; list1.add["Python"]; list1.add["C++"]; //storing data in list 2 list2.add["Java"]; list2.add["Python"]; list2.add["C++"]; //printing both lists System.out.println["List 1:" + list1]; System.out.println["List 2:" + list2]; //comparing both lists if [list1.equals[list2]] { System.out.println["Lists are equal"]; } else { System.out.println["Lists are not equal"]; } //adding another element to list 1 list1.add["HTML"]; //comparing both lists again if [list1.equals[list2]] { System.out.println["Lists are equal"]; } else { System.out.println["Lists are not equal"]; } } }

Output:

run: List 1:[Java, Python, C++] List 2:[Java, Python, C++] Lists are equal Lists are not equal BUILD SUCCESSFUL [total time: 5 seconds]

Compare Arraylists using retainAll[]:

In case you want to compare two ArrayLists with respect to common elements, then you should be trying retainAll[] method. It will return all common elements between two ArrayLists.

Source code:

import java.util.ArrayList; public class ComparingArrayLists {//using retainAll[] public static void main[String[] args] { //initializing two array lists ArrayList list1 = new ArrayList[]; ArrayList list2 = new ArrayList[]; //storing data in list 1 list1.add["Mike"]; list1.add["Sara"]; list1.add["John"]; //storing data in list 2 list2.add["Mike"]; list2.add["Sara"]; list2.add["Diaz"]; list2.add["Sam"]; //printing both lists System.out.println["List 1:" + list1]; System.out.println["List 2:" + list2]; //it will return common elements list2.retainAll[list1]; //printing common elements System.out.println["Common elements are: " + list2]; } }

Output:

run: List 1:[Mike, Sara, John] List 2:[Mike, Sara, Diaz, Sam] Common elements are: [Mike, Sara] BUILD SUCCESSFUL [total time: 14 seconds]

Compare ArrayLists using contains[]:

An alternate way of retainAll[] is to compare the elements of the ArrayLists using contains[] method. The contains[] method checks whether List 1 has the same element as List 2 at the same index.

Source code:

import java.util.ArrayList; import java.util.Arrays; public class ComparingArrayLists {//using contains[] public static void main[String[] args] { //initializing two array lists ArrayList list1 = new ArrayList[Arrays.asList["Apple", "Banana", "Peach", "Apricot"]]; ArrayList list2 = new ArrayList[Arrays.asList["Cabbage", "Carrots", "Cucumber", "Banana"]]; //printing both lists System.out.println["List 1:" + list1]; System.out.println["List 2:" + list2]; //Finding similar items in both lists int i=0; while[i2.

I'm an OO purist. I'd put the Card every time. I'd also write an equals[Card otherCard] method in the Card class and not rely on the other code knowing about how the Strings work.

apines has a good point - you can incorporate that by aborting if the hashtable size goes >2.

Was in the middle of writing the same thing :] Override the equals method is the best practice here in my opinion.

ok Thanks for your help, so how do i update the integer number in the hashtable?

Also should i use containsKey instead of contains? as I'm checking the key?

ok Thanks for your help, so how do i update the integer number in the hashtable?

The Put method with the same key suppose to do this for you, and return the previous value. So get the current value, increment it, and put it back to the table.

Also should i use containsKey instead of contains? as I'm checking the key?

Seems like the correct choice.

Ok, ive implemented abit of this, tell me what you think so far, am i doing it correctly?

public void checkMatch[]{ for[CardHolder h: holders]{ if[topCards.containsKey[h.topCard[]]]{ Integer i = [Integer] topCards.get[h.topCard[]]; topCards.put[h.topCard[],i++]; } else{ topCards.put[h.topCard[], 1]; } } if[topCards.size[] 2]{ } else if[topCards.containsKey[h.topCard[]]]{ //else no match add card[if contained update] Integer i = [Integer] topCards.get[h.topCard[]]; topCards.put[h.topCard[],i++]; System.out.println["update existing card"]; } else{ //first time card added, add and put value as 1 topCards.put[h.topCard[], 1]; System.out.println["adding new card"]; } }

But how am i supposed to check whether a match has been made?

*EDIT*
would a possible way to return the Integer value from the hashtable, then just check if that value equals N-1? that would find the match correctly wouldn't it?

would a possible way to return the Integer value from the hashtable, then just check if that value equals N-1? that would find the match correctly wouldn't it?

Yes [that's what I had in mind when I first suggested HashTable]. You can loop thru the keyset checking the corresponding values, something like:

for [Card c : topcards.keySet] if [topcards.get[c] == [n-1]] // card c appears [n-1] times

Video liên quan

Bài Viết Liên Quan

Toplist mới

Bài mới nhất

Chủ Đề