Java Collections Framework:ArrayList-6

 

How to compare two ArrayList in Java


In this tutorial we will learn how to compare two ArrayList. We would be using contains() method for comparing two elements of different ArrayList.

public boolean contains(Object o)
It returns true if the list contains the Object o else it returns false.

Example:

In this example we have two ArrayList al1 and al2 of String type. We have compared these ArrayLists using contains() method and stored the comparison result in third ArrayList (al3 and al4).

package beginnersbook.com;
import java.util.ArrayList;
public class Details
{
     public static void main(String [] args)
     {
          ArrayList<String> al1= new ArrayList<String>();
          al1.add("hi");
          al1.add("How are you");
          al1.add("Good Morning");
          al1.add("bye");
          al1.add("Good night");

          ArrayList<String> al2= new ArrayList<String>();
          al2.add("Howdy");
          al2.add("Good Evening");
          al2.add("bye");
          al2.add("Good night");

          //Storing the comparison output in ArrayList<String>
          ArrayList<String> al3= new ArrayList<String>();
          for (String temp : al1)
              al3.add(al2.contains(temp) ? "Yes" : "No");
          System.out.println(al3);

          //Storing the comparison output in ArrayList<Integer>
          ArrayList<Integer> al4= new ArrayList<Integer>();
          for (String temp2 : al1)
               al4.add(al2.contains(temp2) ? 1 : 0);
          System.out.println(al4);
     }
}

Output:



[No, No, No, Yes, Yes]
[0, 0, 0, 1, 1]

What is the logic in above code?
If the first element of ArrayList al1 is present in al2 then ArrayList al3 would be having “Yes” and al4 would be having “1” However if the element is not present “No” would be stored in al3 and 0 would be in al4.

How to synchronize ArrayList in java with example



There are two ways to synchronize explicitly:

  1. Using Collections.synchronizedList() method
  2. Using thread-safe variant of ArrayList: CopyOnWriteArrayList

Example 1: Collections.synchronizedList() method for Synchronizing ArrayList

In this example we are using Collections.synchronizedList() method. The important point to note here is that iterator should be in synchronized block in this type of synchronization as shown in the below example.

package beginnersbook.com;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Collections;

public class Details {

   public static void main(String a[]){
       List<String> syncal = 
         Collections.synchronizedList(new ArrayList<String>());

       //Adding elements to synchronized ArrayList
       syncal.add("Pen");
       syncal.add("NoteBook");
       syncal.add("Ink");

       System.out.println("Iterating synchronized ArrayList:");
       synchronized(syncal) {
       Iterator<String> iterator = syncal.iterator(); 
       while (iterator.hasNext())
          System.out.println(iterator.next());
       }
   }
}

Output:



Iterating synchronized ArrayList:
Pen
NoteBook
Ink

Method 2: Using CopyOnWriteArrayList

CopyOnWriteArrayList is a thread-safe variant of ArrayList.

package beginnersbook.com;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.Iterator;

public class Details {

 public static void main(String a[]){
    CopyOnWriteArrayList<String> al = new CopyOnWriteArrayList<String>();

    //Adding elements to synchronized ArrayList
    al.add("Pen");
    al.add("NoteBook");
    al.add("Ink");

    System.out.println("Displaying synchronized ArrayList Elements:");
    //Synchronized block is not required in this method
    Iterator<String> iterator = al.iterator(); 
    while (iterator.hasNext())
       System.out.println(iterator.next());
  }
}

Output:

Displaying synchronized ArrayList Elements:
Pen
NoteBook
Ink

Comments

Popular posts from this blog

Java OOPS:OOPS Concepts Overview

Selenium-Java Contents

Java OOPS:Constructors in Java – A complete study!!