Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

Difference between Arrays.asList(array) Vs. ArrayList(Arrays.asList(array)) in Java

$
0
0
Difference between Arrays.asList(array) Vs. ArrayList(Arrays.asList(array)) in Java - Crunchify

In java what is a difference between List Vs. ArrayList? In other words, have you ever wondered what is the difference between Arrays.asList(array) and ArrayList<Integer>(Arrays.asList(array))? This one is a simple Java program which demonstrates the difference between both, i.e. List Vs. ArrayList.

Let’s take a look at both statements first:

crunchifyList1 = new ArrayList<Integer>(Arrays.asList(crunchifyArray));

Here crunchifyList1 is of type ArrayList.. Very simple.

crunchifyList2 = Arrays.asList(crunchifyArray);

Here crunchifyList2 is a fixed-size list backed by the specified array. In our case it’s of type Integer. Also it’s of type List and not ArrayList.

What is a difference between List and Arraylist?

Answer is very simple. List is an interface, ArrayList is a class that implements List. Below are the list of all available methods for ArrayList.

List of all available methods for ArrayList

Take a look at below Java program which clearly explains you the difference between the same.

package crunchify.com.tutorial;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * @author Crunchify.com
 * 
 */

public class CrunchifyDiffArraysAsList {
	public static void main(String[] args) {

		List<Integer> crunchifyList1, crunchifyList2 = null;

		Integer[] crunchifyArray = { 5, 7, 9 };

		crunchifyList1 = new ArrayList<Integer>(Arrays.asList(crunchifyArray));
		// crunchifyList1 is of type ArrayList
		crunchifyList1.add(11);

		// asList() returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)
		crunchifyList2 = Arrays.asList(crunchifyArray);

		crunchifyList2.add(12); // add() method is not allowed to crunchifyList2.
		// crunchifyList2 is returned as a List view, it has only the methods attached to that interface.

		// Exception in thread "main" java.lang.UnsupportedOperationException
		// at java.util.AbstractList.add(AbstractList.java:148)
		// at java.util.AbstractList.add(AbstractList.java:108)
		// at crunchify.com.tutorials.CrunchifyDiffArraysAsList.main(CrunchifyDiffArraysAsList.java:24)

		// crunchifyList2.add(11); // Because of above exception you can't add value to crunchifyList2

		log("crunchifyList1: ");

		// iterator() returns an iterator over the elements in this list in proper sequence.
		Iterator<Integer> crunchifyIterator1 = crunchifyList1.iterator();
		while (crunchifyIterator1.hasNext()) {
			log(crunchifyIterator1.next() + " ");
		}

		log("\ncrunchifyList2: ");
		Iterator<Integer> crunchifyIterator2 = crunchifyList2.iterator();
		while (crunchifyIterator2.hasNext()) {
			log(crunchifyIterator2.next() + " ");
		}
	}

	private static void log(String string) {
		System.out.println(string);

	}
}

After running above program you will see below exception:

Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.AbstractList.add(AbstractList.java:148)
	at java.util.AbstractList.add(AbstractList.java:108)
	at crunchify.com.tutorial.CrunchifyDiffArraysAsList.main(CrunchifyDiffArraysAsList.java:27)

Just commented out above highlighted line 27 and rerun the program to see below result.

Eclipse Console Result:

crunchifyList1: 
5 
7 
9 
11 

crunchifyList2: 
5 
7 
9

Hope you get clear picture about the difference between list vs arraylist in Java.

The post Difference between Arrays.asList(array) Vs. ArrayList<Integer>(Arrays.asList(array)) in Java appeared first on Crunchify.


Viewing all articles
Browse latest Browse all 1037

Trending Articles