Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Wednesday, 3 April 2013

ArrayList Implementation in Java - Java Collection Framework

In today's discussion we will talk about ArrayList in details, we will come to know properties of ArrayList  and Examples of how to use ArrayList in java to get more from it. ArrayList is a useful class implementation of Java Collection framework. ArrayList implements Serializable, Cloneable, Iterable<E>, Collection<E> ,RandomAccess and List<E> interfaces and extends AttributeList, RoleList and RoleUnresolvedList class directly.


ArrayList in resizable array of List interface, ArrayList can contains duplicate values, but does not allows null values and guarantee that the order of elements will remain same over the time.
ArrayList provides a huge set of implementations, some of the basic and commonly used operations are listed below with proper example.

How to create a ArrayList

A ArrayList object can be obtained as shown below, gnerics specification is optional but it is always a good practice to tell JVM about the content that is going to be stored in ArrayList . Use of generics eleminates the need of explicit type casting.
//creating an ArrayList
        ArrayList<String> arrayList = new ArrayList<String>();         //adding elements to ArrayList         arrayList.add("apple");         System.out.println(arrayList);        

How to add elements to ArrayList - add() & addAll()

ArrayList provides two methods to add elements to it, add() adds a single object at a time while addAll() can be used to add all elements from a specified collection to the ArrayList.
 //adding element to ArrayList with an index value
        arrayList.add(1,"orange");
        System.out.println(arrayList);
       
        ArrayList<String> arrayList1 = new ArrayList<String>();
        //adding a collection to ArraList
        arrayList.addAll(arrayList1);
        System.out.println(arrayList1);
       
        //adding a collection with index value         ArrayList<String> arrayList2 = new ArrayList<String>();         arrayList.addAll(1, arrayList2);         System.out.println(arrayList);        

How to remove Elements from ArrayList - remove() & removeAll()

ArrayList provides two methods to remove elements from ArrayList , remove() remmoves one element while removeAll() removes all elements from ArrayList that are present in specified collection.
//remove element with specified index value
        arrayList.remove(1);
        //remove specified collection from ArrayList
        arrayList.removeAll(arrayList);
        //remove all elements from the list that are not present in specified collection
        arrayList.retainAll(arrayList1);
       

How to make ArrayList Empty - clear()

ArrayList provides clear() method to remove all elements from a ArrayList , it makes ArrayList an empty collection.
 //deleting content of ArrayList -- making empty
        arrayList.clear();
        System.out.println(arrayList);
       

How to clone an ArrayList - clone()

Creates a shallow copy of ArrayList instance but elements does not coloned themeselves. While comparing a ArrayList and its clone using .equals(), it always returns a 'true'.
//cloning an ArrayList
        ArrayList arraListClone = (ArrayList) arrayList.clone();         arrayList.equals(arraListClone);         System.out.println(arrayList.equals(arraListClone)); // returns a true        

How to check if ArrayList contains specified element/elements - contains() & containsAll()

ArrayList provides two very important methods, contains() returns true if the specified element is present in the ArrayList and containsAll() returns true if the elements in specified collection are present inArrayList .
        //check if ArrayList contains specified element
        arrayList.contains("apple");
        //check if ArrayList contains specified collection
        arrayList.containsAll(arrayList1);
       

How to convert ArrayList to an Array or a String - toArray() & toString()

toArray() converts all ArrayList elements to an array and toString() represents a String form of ArrayList .
// returns an array implementation of ArrayList
        arrayList.toArray();
        // returns an string implementation of ArrayList
        arrayList.toString();
       

How to use retainAll() with ArrayList - retainAll()

retainAll() method returns only those elements from ArrayList that are present in the specified collection.
        //remove all elements from the list that are not present in specified collection
        arrayList.retainAll(arrayList1);
       

How to check if ArrayList contains specified element or not - contains() & containsAll()

Returns true if the specified element/collection is present in ArrayList, false in other case.
//check if ArrayList contains specified element
        arrayList.contains("apple");
        //check if ArrayList contains specified collection
        arrayList.containsAll(arrayList1);
       

How to get a sublist from ArrayList - subList()

ArrayList provides subList() method to get a sublist on the basis of provided first and last index values.
        // returns a sub-list on the basis of start and end index
        arrayList.subList(0,4);
       

How to replace an element in ArrayList -set()

ArrayList provides set() method that replaces the element at specified index with the one provided in method itself.
// replace element on specified index with specified value 
        arrayList.set(1,"banana");
       

How to get size of an ArrayList - size()

The size of a ArrayList can be obtained using .size() method, it returns an integer value equals to the number of elements in the ArrayList .
//returns size of ArrayList
        arrayList.size();
       

How to get index of an element in ArrayList

ArrayList provides two very useful methods indexOf() and lastIndexOf() to get first and last occurences of an element in the list.
//returns first occurence of specified element
        arrayList.indexOf("apple");
        //returns last occurence of specified element
        arrayList.lastIndexOf("apple");
       

How to iterate ArrayList elements - iterator()

An iterator can be applied on a ArrayList instance using .iterator() method, hasNext() checks if the ArrayList contains more elements and .next() returns the next element in ArrayList .
// iterating an ArrayList 
        Iterator iterator = (Iterator) arrayList.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
       

In this blog we saw how to use ArrayList in java, we went through examples of ArrayList in java and come across the various implementations and operations over ArrayList . In upcoming blogs we will cover more topics and tricks on Java Collection Framework and its usage in Java Language.









Thanks for reading !
Being Java Guys Team



3 comments:

  1. very nice.. for more java examples, visit http://java2novice.com site.

    ReplyDelete
  2. very nice.. for more java examples, visit http://java2novice.com site.

    ReplyDelete

Search Tutorials
Like Us on Facebook

Like Us On Google+