Java Collections

ArrayList Class in Java Collection Framework

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<p>The <i><b>ArrayList <&sol;b><&sol;i>class extends <i><b>AbstractList <&sol;b><&sol;i>and implements the List interface&period; <b><i>ArrayList <&sol;i><&sol;b>supports dynamic arrays that can grow as needed&period;<&sol;p>&NewLine;<p>Standard Java arrays are of a fixed length&period; After arrays are created&comma; they cannot grow or shrink&comma; which means that you must know in advance how many elements an array will hold&period;<&sol;p>&NewLine;<p>Array lists are created with an initial size&period; When this size is exceeded&comma; the collection is automatically enlarged&period; When objects are removed&comma; the array may be shrunk&period;<br &sol;>&NewLine;<b><br &sol;>&NewLine;<&sol;b> <b><i>ArrayList <&sol;i>class<&sol;b><&sol;p>&NewLine;<ul>&NewLine;<li><b><i>ArrayList <&sol;i><&sol;b>class extends <i><b>AbstractList <&sol;b><&sol;i>class and implements the <i><b>List <&sol;b><&sol;i>interface&period;<&sol;li>&NewLine;<li><i><b>ArrayList <&sol;b><&sol;i>supports dynamic array that can grow as needed&period; <i><b>ArrayList <&sol;b><&sol;i>has three constructors&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<pre class&equals;"highlight">&sol;&sol;constructor builds an empty array list &NewLine;ArrayList&lpar;&rpar; &NewLine;&sol;&sol;The following constructor builds an array list that is initialized with the elements of the collection c&period; &NewLine;ArrayList&lpar; Collection C &rpar; &NewLine;&sol;&ast;The following constructor builds an array list that has the specified initial capacity&period; The capacity is the size of the underlying array that is used to store the elements&period; &NewLine; &NewLine;The capacity grows automatically as elements are added to an array list&period;&ast;&sol; &NewLine;ArrayList&lpar; int capacity &rpar; &NewLine;<&sol;pre>&NewLine;<ul>&NewLine;<li><b>ArrayLists <&sol;b>are created with an initial size&comma; when this size is exceeded&comma; it gets enlarged automatically&period;<&sol;li>&NewLine;<li>It can coontain Duplicate elements and maintains the insertion order&period;<&sol;li>&NewLine;<li><i><b>ArrayLists <&sol;b><&sol;i>are not synchronized&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;arraylist&period;jpg" border&equals;"0" &sol;><&sol;div>&NewLine;<p><b>Methods in the <i>ArrayList <&sol;i>Class&colon;<&sol;b><&sol;p>&NewLine;<div style&equals;"-webkit-text-size-adjust&colon; auto&semi; -webkit-text-stroke-width&colon; 0px&semi; background-color&colon; white&semi; color&colon; black&semi; font-family&colon; Helvetica&comma; Arial&comma; sans-serif&semi; font-size&colon; 12px&semi; font-style&colon; normal&semi; font-variant&colon; normal&semi; font-weight&colon; normal&semi; letter-spacing&colon; normal&semi; line-height&colon; normal&semi; margin&colon; 0&period;8em 0px 1em&semi; orphans&colon; auto&semi; padding&colon; 0px&semi; text-align&colon; justify&semi; text-indent&colon; 0px&semi; text-transform&colon; none&semi; white-space&colon; normal&semi; widows&colon; auto&semi; word-spacing&colon; 0px&semi;">Apart from the methods inherited from its parent classes&comma; <i><b>ArrayList <&sol;b><&sol;i>defines following methods&colon;<&sol;div>&NewLine;<table class&equals;"src" style&equals;"-webkit-text-size-adjust&colon; auto&semi; -webkit-text-stroke-width&colon; 0px&semi; background-color&colon; &num;f7f7f7&semi; border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; color&colon; black&semi; font-family&colon; Helvetica&comma; Arial&comma; sans-serif&semi; font-size&colon; 12px&semi; font-style&colon; normal&semi; font-variant&colon; normal&semi; font-weight&colon; normal&semi; letter-spacing&colon; normal&semi; line-height&colon; normal&semi; margin&colon; 8px 0px&semi; orphans&colon; auto&semi; padding&colon; 0px&semi; text-align&colon; start&semi; text-indent&colon; 0px&semi; text-transform&colon; none&semi; vertical-align&colon; top&semi; white-space&colon; normal&semi; widows&colon; auto&semi; width&colon; 560px&semi; word-spacing&colon; 0px&semi;">&NewLine;<tbody style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<th style&equals;"background-color&colon; &num;eeeeee&semi; border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi; text-align&colon; left&semi;">SN<&sol;th>&NewLine;<th style&equals;"background-color&colon; &num;eeeeee&semi; border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi; text-align&colon; left&semi;">Methods with Description<&sol;th>&NewLine;<&sol;tr>&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;">1<&sol;td>&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;"><b style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">void add&lpar;int index&comma; Object element&rpar;<&sol;b><br &sol;>&NewLine;Inserts the specified element at the specified position index in this list&period; Throws IndexOutOfBoundsException if the specified index is is out of range &lpar;index &lt&semi; 0 &vert;&vert; index &gt&semi; size&lpar;&rpar;&rpar;&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;">2<&sol;td>&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;"><b style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">boolean add&lpar;Object o&rpar; <&sol;b><br &sol;>&NewLine;Appends the specified element to the end of this list&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;">3<&sol;td>&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;"><b style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">boolean addAll&lpar;Collection c&rpar;<&sol;b><br &sol;>&NewLine;Appends all of the elements in the specified collection to the end of this list&comma; in the order that they are returned by the specified collection&&num;8217&semi;s iterator&period; Throws NullPointerException if the specified collection is null&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;">4<&sol;td>&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;"><b style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">boolean addAll&lpar;int index&comma; Collection c&rpar; <&sol;b><br &sol;>&NewLine;Inserts all of the elements in the specified collection into this list&comma; starting at the specified position&period; Throws NullPointerException if the specified collection is null&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;">5<&sol;td>&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;"><b style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">void clear&lpar;&rpar; <&sol;b><br &sol;>&NewLine;Removes all of the elements from this list&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;">6<&sol;td>&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;"><b style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">Object clone&lpar;&rpar; <&sol;b><br &sol;>&NewLine;Returns a shallow copy of this ArrayList&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;">7<&sol;td>&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;"><b style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">boolean contains&lpar;Object o&rpar; <&sol;b><br &sol;>&NewLine;Returns true if this list contains the specified element&period; More formally&comma; returns true if and only if this list contains at least one element e such that &lpar;o&equals;&equals;null &quest; e&equals;&equals;null &colon; o&period;equals&lpar;e&rpar;&rpar;&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;">8<&sol;td>&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;"><b style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">void ensureCapacity&lpar;int minCapacity&rpar; <&sol;b><br &sol;>&NewLine;Increases the capacity of this ArrayList instance&comma; if necessary&comma; to ensure that it can hold at least the number of elements specified by the minimum capacity argument&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;">9<&sol;td>&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;"><b style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">Object get&lpar;int index&rpar; <&sol;b><br &sol;>&NewLine;Returns the element at the specified position in this list&period; Throws IndexOutOfBoundsException if the specified index is is out of range &lpar;index &lt&semi; 0 &vert;&vert; index &gt&semi;&equals; size&lpar;&rpar;&rpar;&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;">10<&sol;td>&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;"><b style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">int indexOf&lpar;Object o&rpar; <&sol;b><br &sol;>&NewLine;Returns the index in this list of the first occurrence of the specified element&comma; or -1 if the List does not contain this element&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;">11<&sol;td>&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;"><b style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">int lastIndexOf&lpar;Object o&rpar;<&sol;b><br &sol;>&NewLine;Returns the index in this list of the last occurrence of the specified element&comma; or -1 if the list does not contain this element&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;">12<&sol;td>&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;"><b style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">Object remove&lpar;int index&rpar; <&sol;b><br &sol;>&NewLine;Removes the element at the specified position in this list&period; Throws IndexOutOfBoundsException if index out of range &lpar;index &lt&semi; 0 &vert;&vert; index &gt&semi;&equals; size&lpar;&rpar;&rpar;&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;">13<&sol;td>&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;"><b style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">protected void removeRange&lpar;int fromIndex&comma; int toIndex&rpar; <&sol;b><br &sol;>&NewLine;Removes from this List all of the elements whose index is between fromIndex&comma; inclusive and toIndex&comma; exclusive&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;">14<&sol;td>&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;"><b style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">Object set&lpar;int index&comma; Object element&rpar; <&sol;b><br &sol;>&NewLine;Replaces the element at the specified position in this list with the specified element&period; Throws IndexOutOfBoundsException if the specified index is is out of range &lpar;index &lt&semi; 0 &vert;&vert; index &gt&semi;&equals; size&lpar;&rpar;&rpar;&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;">15<&sol;td>&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;"><b style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">int size&lpar;&rpar; <&sol;b><br &sol;>&NewLine;Returns the number of elements in this list&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;">16<&sol;td>&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;"><b style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">Object&lbrack;&rsqb; toArray&lpar;&rpar; <&sol;b><br &sol;>&NewLine;Returns an array containing all of the elements in this list in the correct order&period; Throws NullPointerException if the specified array is null&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;">17<&sol;td>&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;"><b style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">Object&lbrack;&rsqb; toArray&lpar;Object&lbrack;&rsqb; a&rpar; <&sol;b><br &sol;>&NewLine;Returns an array containing all of the elements in this list in the correct order&semi; the runtime type of the returned array is that of the specified array&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;">18<&sol;td>&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid &num;d6d6d6&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;"><b style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">void trimToSize&lpar;&rpar; <&sol;b><br &sol;>&NewLine;Trims the capacity of this ArrayList instance to be the list&&num;8217&semi;s current size&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<&sol;tbody>&NewLine;<&sol;table>&NewLine;<p><b>Example of <i>ArrayList<&sol;i>&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">import java&period;util&period;&ast;&semi; &NewLine; &NewLine;public class ArrayListDemo &lbrace; &NewLine; public static void main&lpar;String args&lbrack;&rsqb;&rpar; &lbrace; &NewLine; &sol;&sol; create an array list &NewLine; ArrayList list &equals; new ArrayList&lpar;&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"Initial size of list&colon; " &plus; list&period;size&lpar;&rpar;&rpar;&semi; &NewLine; &NewLine; &sol;&sol; add elements to the array list &NewLine; list&period;add&lpar;"Dinesh"&rpar;&semi; &NewLine; list&period;add&lpar;"Sweety"&rpar;&semi; &NewLine; list&period;add&lpar;"Adesh"&rpar;&semi; &NewLine; list&period;add&lpar;"Vinesh"&rpar;&semi; &NewLine; list&period;add&lpar;"Mom"&rpar;&semi; &NewLine; list&period;add&lpar;"DAD"&rpar;&semi; &NewLine; &NewLine; System&period;out&period;println&lpar;"Size of list after additions&colon; " &plus; list&period;size&lpar;&rpar;&rpar;&semi; &NewLine; &NewLine; &sol;&sol; display the array list &NewLine; System&period;out&period;println&lpar;"Contents of list&colon; " &plus; list&rpar;&semi; &NewLine; &NewLine; System&period;out&period;println&lpar;"Iterating list"&rpar;&semi; &NewLine; Iterator itr &equals; list&period;iterator&lpar;&rpar;&semi; &NewLine; while&lpar;itr&period;hasNext&lpar;&rpar;&rpar;&lbrace; &NewLine; System&period;out&period;println&lpar;itr&period;next&lpar;&rpar;&rpar;&semi; &NewLine; &rcub; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>output&colon;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;arraylist&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p><b>Two ways to iterate the elements of collection&colon;<&sol;b><&sol;p>&NewLine;<ol>&NewLine;<li>By Iterator interface&period;<&sol;li>&NewLine;<li>By for-each loop&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p><b>Iterating the elements of Collection by for-each loop&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">import java&period;util&period;&ast;&semi; &NewLine; &NewLine;public class ArrayListDemo &lbrace; &NewLine; public static void main&lpar;String args&lbrack;&rsqb;&rpar; &lbrace; &NewLine; &sol;&sol; create an array list &NewLine; ArrayList list &equals; new ArrayList&lpar;&rpar;&semi; &NewLine; &NewLine; &sol;&sol; add elements to the array list &NewLine; list&period;add&lpar;"Dinesh"&rpar;&semi; &NewLine; list&period;add&lpar;"Sweety"&rpar;&semi; &NewLine; list&period;add&lpar;"Adesh"&rpar;&semi; &NewLine; list&period;add&lpar;"Vinesh"&rpar;&semi; &NewLine; list&period;add&lpar;"Mom"&rpar;&semi; &NewLine; list&period;add&lpar;"DAD"&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"Iterating list using for each loop"&rpar;&semi; &NewLine; for&lpar;Object obj &colon; list&rpar;&lbrace; &NewLine; System&period;out&period;println&lpar;obj&rpar;&semi; &NewLine; &rcub; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>output&colon;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;arraylist2&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p><b>Storing User-Defined classes&colon;<&sol;b><br &sol;>&NewLine;In the above example we are storing only string object in <b><i>ArrayList <&sol;i><&sol;b>collection&period; But You can store any type of object&comma; including object of class that you create in Collection classes&period;<br &sol;>&NewLine;<b>Example of storing User-Defined object<&sol;b><&sol;p>&NewLine;<p><b>Employee class<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">class Employee &NewLine;&lbrace; &NewLine; String firstName&semi; &NewLine; String lastName&semi; &NewLine; String phone&semi; &NewLine; &NewLine; public Employee&lpar;String firstName&comma;String lastName&comma;String phone&rpar; &NewLine; &lbrace; &NewLine; this&period;firstName &equals; firstName &semi; &NewLine; this&period;lastName &equals; lastName&semi; &NewLine; this&period;phone &equals; phone&semi; &NewLine; &rcub; &NewLine; &NewLine; public String toString&lpar;&rpar; &NewLine; &lbrace; &NewLine; return firstName &plus;" "&plus;lastName &plus;" &lpar;"&plus;phone &plus;"&rpar; "&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>Storing Employee class<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">public class EmployeeListDemo &NewLine;&lbrace; &NewLine; &NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &NewLine; &lbrace; &NewLine; Employee e1 &equals; new Employee&lpar;"Dinesh"&comma; "Rajput"&comma;"999100091"&rpar;&semi; &NewLine; Employee e2 &equals; new Employee&lpar;"Sandeep"&comma; "Sharma"&comma;"998392819"&rpar;&semi; &NewLine; Employee e3 &equals; new Employee&lpar;"Vinesh"&comma; "Kohli"&comma;"998131319"&rpar;&semi; &NewLine; &NewLine; ArrayList al &equals; new ArrayList&lpar;&rpar;&semi; &NewLine; al&period;add&lpar;e1&rpar;&semi; &NewLine; al&period;add&lpar;e2&rpar;&semi; &NewLine; al&period;add&lpar;e3&rpar;&semi; &NewLine; System&period;out&period;println&lpar;al&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>output&colon;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;arraylist3&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p><b>Example of <i>addAll&lpar;Collection c&rpar;<&sol;i> method&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">import java&period;util&period;&ast;&semi; &NewLine;class SimpleListDemo&lbrace; &NewLine; public static void main&lpar;String args&lbrack;&rsqb;&rpar;&lbrace; &NewLine; &NewLine; ArrayList al &equals; new ArrayList&lpar;&rpar;&semi; &NewLine; al&period;add&lpar;"Dinesh"&rpar;&semi; &NewLine; al&period;add&lpar;"Anamika"&rpar;&semi; &NewLine; al&period;add&lpar;"Sweety"&rpar;&semi; &NewLine; &NewLine; ArrayList al2 &equals; new ArrayList&lpar;&rpar;&semi; &NewLine; al2&period;add&lpar;"Adesh"&rpar;&semi; &NewLine; al2&period;add&lpar;"Vinesh"&rpar;&semi; &NewLine; &NewLine; al&period;addAll&lpar;al2&rpar;&semi; &NewLine; &NewLine; Iterator itr&equals;al&period;iterator&lpar;&rpar;&semi; &NewLine; while&lpar;itr&period;hasNext&lpar;&rpar;&rpar;&lbrace; &NewLine; System&period;out&period;println&lpar;itr&period;next&lpar;&rpar;&rpar;&semi; &NewLine; &rcub; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>output&colon;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;arraylist4&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p><b>Example of <i>removeAll&lpar;&rpar;<&sol;i> method&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">import java&period;util&period;&ast;&semi; &NewLine;class SimpleListDemo&lbrace; &NewLine; public static void main&lpar;String args&lbrack;&rsqb;&rpar;&lbrace; &NewLine; &NewLine; ArrayList al &equals; new ArrayList&lpar;&rpar;&semi; &NewLine; al&period;add&lpar;"Dinesh"&rpar;&semi; &NewLine; al&period;add&lpar;"Anamika"&rpar;&semi; &NewLine; al&period;add&lpar;"Sweety"&rpar;&semi; &NewLine; &NewLine; ArrayList al2 &equals; new ArrayList&lpar;&rpar;&semi; &NewLine; al2&period;add&lpar;"Anamika"&rpar;&semi; &NewLine; al2&period;add&lpar;"Vinesh"&rpar;&semi; &NewLine; &NewLine; al&period;removeAll&lpar;al2&rpar;&semi; &NewLine; &NewLine; Iterator itr&equals;al&period;iterator&lpar;&rpar;&semi; &NewLine; while&lpar;itr&period;hasNext&lpar;&rpar;&rpar;&lbrace; &NewLine; System&period;out&period;println&lpar;itr&period;next&lpar;&rpar;&rpar;&semi; &NewLine; &rcub; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>output&colon;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;arraylist5&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p><b>Example of <i>retainAll&lpar;&rpar;<&sol;i> method&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">import java&period;util&period;&ast;&semi; &NewLine;class SimpleListDemo&lbrace; &NewLine; public static void main&lpar;String args&lbrack;&rsqb;&rpar;&lbrace; &NewLine; &NewLine; ArrayList al &equals; new ArrayList&lpar;&rpar;&semi; &NewLine; al&period;add&lpar;"Dinesh"&rpar;&semi; &NewLine; al&period;add&lpar;"Anamika"&rpar;&semi; &NewLine; al&period;add&lpar;"Sweety"&rpar;&semi; &NewLine; &NewLine; ArrayList al2 &equals; new ArrayList&lpar;&rpar;&semi; &NewLine; al2&period;add&lpar;"Anamika"&rpar;&semi; &NewLine; al2&period;add&lpar;"Vinesh"&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"iterating the elements after retaining the elements of al2&period;&period;&period;"&rpar;&semi; &NewLine; al&period;retainAll&lpar;al2&rpar;&semi; &NewLine; &NewLine; Iterator itr&equals;al&period;iterator&lpar;&rpar;&semi; &NewLine; while&lpar;itr&period;hasNext&lpar;&rpar;&rpar;&lbrace; &NewLine; System&period;out&period;println&lpar;itr&period;next&lpar;&rpar;&rpar;&semi; &NewLine; &rcub; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>output&colon;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;arraylist6&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border-radius&colon; 3px&semi; border&colon; 1px solid &num;c9e6f2&semi; line-height&colon; 1&period;45&semi; padding&colon; 16px&semi;">&NewLine;<b>Java Collections Tutorial<&sol;b><&sol;p>&NewLine;<ol style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;linkedlist-class-in-collection&sol;">LinkedList class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;listiterator-interface-in-collection&sol;">ListIterator interface<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;">HashSet class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;linkedhashset-class-in-collection&sol;">LinkedHashSet class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;">TreeSet class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-hashset-and-treeset&sol;">Difference between TreeSet &amp&semi; HashSet<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;map-interface-in-collection&sol;">Map interface<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashmap-class-in-collection-framework&sol;">HashMap class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;how-does-java-hashmap-work-internally&sol;">How does work HashMap&quest;<&sol;a> <&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-treemap-vs-hashmap&sol;">Difference between TreeMap vs HashMap<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;linkedhashmap-class-in-collection&sol;">LinkedHashMap class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treemap-class-in-collection-framework&sol;">TreeMap class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashtable-class-in-collection-framework&sol;">Hashtable class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-hashmap-and&sol;">Difference between HashMap and HashTable in Java<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;sorting-in-collection-framework&sol;">Sorting<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;java-comparable-and-comparator&sol;">Comparable interface<&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ol>&NewLine;<&sol;div>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;java-collection-framework">Previous<&sol;a> &lt&semi;&lt&semi;   &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;">Index <&sol;a>&vert;&vert;   &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;linkedlist-class-in-collection&sol;">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;java-collection-framework&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;linkedlist-class-in-collection&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '468'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago