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:
1 |
crunchifyList1 = new ArrayList<Integer>(Arrays.asList(crunchifyArray)); |
Here crunchifyList1
is of type ArrayList
.. Very simple.
1 |
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.
Take a look at below Java program which clearly explains you the difference between the same.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
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:
1 2 3 4 |
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:
1 2 3 4 5 6 7 8 9 10 |
crunchifyList1: 5 7 9 11 crunchifyList2: 5 7 9 |
Hope you get clear picture about the difference between list vs arraylist
in Java.