Showing posts with label Collection-ArrayList. Show all posts
Showing posts with label Collection-ArrayList. Show all posts

Tuesday, August 9, 2011

Sort Java ArrayList in descending order using comparator

package org.best.example;
   
/*
      Sort Java ArrayList in descending order using comparator example
      This java example shows how to sort elements of Java ArrayList in descending order
      using comparator and reverseOrder method of Collections class.
    */
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    
    public class SortArrayListInDescendingOrderExample {
    
    public static void main(String[] args) {
    
    //create an ArrayList object
    ArrayList arrayList = new ArrayList();
    
    //Add elements to Arraylist
    arrayList.add("A");
    arrayList.add("B");
    arrayList.add("C");
    arrayList.add("D");
    arrayList.add("E");
    
    /*
      To get comparator that imposes reverse order on a Collection use
      static Comparator reverseOrder() method of Collections class
      */
    
    Comparator comparator = Collections.reverseOrder();
    
    System.out.println("Before sorting ArrayList in descending order : "
    + arrayList);
    
    /*
      To sort an ArrayList using comparator use,
      static void sort(List list, Comparator c) method of Collections class.
      */
    
    Collections.sort(arrayList,comparator);
    System.out.println("After sorting ArrayList in descending order : "
    + arrayList);
    
    }
    }
    
    /*
    Output would be
    Before sorting ArrayList in descending order : [A, B, C, D, E]
    After sorting ArrayList in descending order : [E, D, C, B, A]
    */

Monday, August 8, 2011

Sort elements of Java ArrayList

package org.best.example;
   
    /*
      Sort elements of Java ArrayList Example
      This Java Example shows how to sort the elements of java ArrayList object using
      Collections.sort method.
    */
    
    import java.util.ArrayList;
    import java.util.Collections;
    
    public class SortJavaArrayListExample {
    
    public static void main(String[] args) {
    
    //create an ArrayList object
    ArrayList arrayList = new ArrayList();
    
    //Add elements to Arraylist
    arrayList.add("1");
    arrayList.add("3");
    arrayList.add("5");
    arrayList.add("2");
    arrayList.add("4");
    
    /*
      To sort an ArrayList object, use Collection.sort method. This is a
      static method. It sorts an ArrayList object's elements into ascending order.
      */
    Collections.sort(arrayList);
    
    //display elements of ArrayList
    System.out.println("ArrayList elements after sorting in ascending order : ");
    for(int i=0; i<arrayList.size(); i++)
    System.out.println(arrayList.get(i));
    
    }
    }
    
    /*
    Output would be
    ArrayList elements after sorting in ascending order :
    1
    2
    3
    4
    5
    */

Sunday, August 7, 2011

Simple Java ArrayList

package org.best.example;
   
    /*
      Simple Java ArrayList Example
      This Java Example shows how to create an object of Java ArrayList. It also
      shows how to add elements to ArrayList and how get the same from ArrayList.
    */
    
    import java.util.ArrayList;
    
    public class SimpleArrayListExample {
    
    public static void main(String[] args) {
    
    //create an ArrayList object
    ArrayList arrayList = new ArrayList();
    
    /*
      Add elements to Arraylist using
      boolean add(Object o) method. It returns true as a general behavior
      of Collection.add method. The specified object is appended at the end
      of the ArrayList.
      */
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    
    /*
      Use get method of Java ArrayList class to display elements of ArrayList.
      Object get(int index) returns and element at the specified index in
      the ArrayList
      */
    System.out.println("Getting elements of ArrayList");
    System.out.println(arrayList.get(0));
    System.out.println(arrayList.get(1));
    System.out.println(arrayList.get(2));
    }
    }
    
    /*
    Output would be
    Getting elements of ArrayList
    1
    2
    3
    */

Saturday, August 6, 2011

Search an element of Java ArrayList

package org.best.example;
   
    /*
      Search an element of Java ArrayList Example
      This Java Example shows how to search an element of java
      ArrayList object using contains, indexOf and lastIndexOf methods.
    */
    
    import java.util.ArrayList;
    
    public class SearchAnElementInArrayListExample {
    
    public static void main(String[] args) {
    
    //create an ArrayList object
    ArrayList arrayList = new ArrayList();
    
    //Add elements to Arraylist
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    arrayList.add("4");
    arrayList.add("5");
    arrayList.add("1");
    arrayList.add("2");
    
    /*
      To check whether the specified element exists in Java ArrayList use
      boolean contains(Object element) method.
      It returns true if the ArrayList contains the specified objct, false
      otherwise.
      */
    
    boolean blnFound = arrayList.contains("2");
    System.out.println("Does arrayList contain 2 ? " + blnFound);
    
    /*
      To get an index of specified element in ArrayList use
      int indexOf(Object element) method.
      This method returns the index of the specified element in ArrayList.
      It returns -1 if not found.
      */
    
    int index = arrayList.indexOf("4");
    if(index == -1)
    System.out.println("ArrayList does not contain 4");
    else
    System.out.println("ArrayList contains 4 at index :" + index);
    
    /*
      To get last index of specified element in ArrayList use
      int lastIndexOf(Object element) method.
      This method returns index of the last occurrence of the
      specified element in ArrayList. It returns -1 if not found.
      */
    
    int lastIndex = arrayList.lastIndexOf("1");
    if(lastIndex == -1)
    System.out.println("ArrayList does not contain 1");
    else
    System.out.println("Last occurrence of 1 in ArrayList is at index :"
    + lastIndex);
    
    }
    }
    /*
    Output would be
    Does arrayList contain 2 ? true
    ArrayList contains 4 at index :3
    Last occurrence of 1 in ArrayList is at index :5
    */

Friday, August 5, 2011

Replace an element at specified index of Java ArrayList

package org.best.example;
   
        /*
      Replace an element at specified index of Java ArrayList Example
      This Java Example shows how to replace an element at specified index of java
      ArrayList object using set method.
    */
    
    import java.util.ArrayList;
    
    public class ReplaceElementAtSpecifiedIndexArrayListExample {
    
    public static void main(String[] args) {
    //create an ArrayList object
    ArrayList arrayList = new ArrayList();
    
    //Add elements to Arraylist
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    
    /*
      To replace an element at the specified index of ArrayList use
      Object set(int index, Object obj) method.
      This method replaces the specified element at the specified index in the
      ArrayList and returns the element previously at the specified position.
      */
    arrayList.set(1,"REPLACED ELEMENT");
    
    System.out.println("ArrayList contains...");
    //display elements of ArrayList
    for(int index=0; index < arrayList.size(); index++)
    System.out.println(arrayList.get(index));
    
    }
    }
    
    /*
    Output would be
    ArrayList contains...
    1
    REPLACED ELEMENT
    3
    */

Thursday, August 4, 2011

Remove an element from specified index of Java ArrayList

package org.best.example;
   
    /*
      Remove an element from specified index of Java ArrayList Example
      This Java Example shows how to remove an element at specified index of java
      ArrayList object using remove method.
    */
    
    import java.util.ArrayList;
    
    public class RemoveElementFromArrayListExample {
    
    public static void main(String[] args) {
    //create an ArrayList object
    ArrayList arrayList = new ArrayList();
    
    //Add elements to Arraylist
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    
    /*
      To remove an element from the specified index of ArrayList use
      Object remove(int index) method.
      It returns the element that was removed from the ArrayList.
      */
    Object obj = arrayList.remove(1);
    System.out.println(obj + " is removed from ArrayList");
    
    System.out.println("ArrayList contains...");
    //display elements of ArrayList
    for(int index=0; index < arrayList.size(); index++)
    System.out.println(arrayList.get(index));
    
    }
    }
    
    /*
    Output would be
    2 is removed from ArrayList
    ArrayList contains...
    1
    3
    */

Wednesday, August 3, 2011

Remove all elements from Java ArrayList

package org.best.example;
   
/*
      Remove all elements from Java ArrayList Example
      This Java Example shows how to remove all elements from java ArrayList object
      using clear method.
    */
    
    import java.util.ArrayList;
    
    public class RemoveAllElementsOfArrayListExample {
    
    public static void main(String[] args) {
    //create an ArrayList object
    ArrayList arrayList = new ArrayList();
    
    //Add elements to Arraylist
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    
    System.out.println("Size of ArrayList before removing elements : "
    + arrayList.size());
    /*
      To remove all elements from the ArrayList use
      void clear() method.
      */
    arrayList.clear();
    System.out.println("Size of ArrayList after removing elements : "
    + arrayList.size());
    
    }
    }
    /*
    Output would be
    Size of ArrayList before removing elements : 3
    Size of ArrayList after removing elements : 0
    */

Tuesday, August 2, 2011

Iterate through elements Java ArrayList using ListIterator

    package org.best.examples;

    /*
      Iterate through elements Java ArrayList using ListIterator Example
      This Java Example shows how to iterate through the elements of java
      ArrayList object in forward and backward direction using ListIterator.
    */
    
    import java.util.ArrayList;
    import java.util.ListIterator;
    
    public class IterateThroughArrayListUsingListIteratorExample {
    
    public static void main(String[] args) {
    
    //create an ArrayList object
    ArrayList arrayList = new ArrayList();
    
    //Add elements to Arraylist
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    arrayList.add("4");
    arrayList.add("5");
    
    /*
      Get a ListIterator object for ArrayList using
      listIterator() method.
      */
    ListIterator itr = arrayList.listIterator();
    
    /*
      Use hasNext() and next() methods of ListIterator to iterate through
      the elements in forward direction.
      */
    System.out.println("Iterating through ArrayList elements in forward
    direction...");
    while(itr.hasNext())
    System.out.println(itr.next());
    
    /*
      Use hasPrevious() and previous() methods of ListIterator to iterate through
      the elements in backward direction.
      */
    System.out.println("Iterating through ArrayList elements in backward
    direction...");
    while(itr.hasPrevious())
    System.out.println(itr.previous());
    
    }
    }
    
    /*
    Output would be
    Iterating through ArrayList elements...
    Iterating through ArrayList elements in forward direction...
    1
    2
    3
    4
    5
    Iterating through ArrayList elements in backward direction...
    5
    4
    3
    2
    1
    */

Monday, August 1, 2011

Iterate through elements Java ArrayList using Iterator

    package org.best.examples;

    /*
      Iterate through elements Java ArrayList using Iterator Example
      This Java Example shows how to iterate through the elements of java
      ArrayList object using Iterator.
    */
    
    import java.util.ArrayList;
    import java.util.Iterator;
    
    public class IterateThroughArrayListUsingIteratorExample {
    
    public static void main(String[] args) {
    
    //create an ArrayList object
    ArrayList arrayList = new ArrayList();
    
    //Add elements to Arraylist
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    arrayList.add("4");
    arrayList.add("5");
    
    //get an Iterator object for ArrayList using iterator() method.
    Iterator itr = arrayList.iterator();
    
    //use hasNext() and next() methods of Iterator to iterate through the elements
    System.out.println("Iterating through ArrayList elements...");
    while(itr.hasNext())
    System.out.println(itr.next());
    
    }
    }
    
    /*
    Output would be
    Iterating through ArrayList elements...
    1
    2
    3
    4
    5
    */

Sunday, July 31, 2011

Insert all elements of other Collection to Specified Index of Java ArrayList

    package org.best.examples;

    /*
      Insert all elements of other Collection to Specified Index of Java ArrayList Example
      This Java Example shows how to insert all elements of other Collection object
      at specified index of Java ArrayList object using addAll method.
    */
    
    import java.util.ArrayList;
    import java.util.Vector;
    
    public class InsertAllElementsOfOtherCollectionToArrayListExample {
    
    public static void main(String[] args) {
    
    //create an ArrayList object
    ArrayList arrayList = new ArrayList();
    
    //Add elements to Arraylist
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    
    //create a new Vector object
    Vector v = new Vector();
    v.add("4");
    v.add("5");
    
    /*
      To insert all elements of another Collection to sepcified index of ArrayList
      use
      boolean addAll(int index, Collection c) method.
      It returns true if the ArrayList was changed by the method call.
      */
    
    //insert all elements of Vector to ArrayList at index 1
    arrayList.addAll(1,v);
    
    //display elements of ArrayList
    System.out.println("After inserting all elements of Vector at index 1,
    ArrayList contains..");
    for(int i=0; i<arrayList.size(); i++)
    System.out.println(arrayList.get(i));
    
    }
    }
    
    /*
    Output would be
    After inserting all elements of Vector at index 1, ArrayList contains..
    1
    4
    5
    2
    3
    */

Saturday, July 30, 2011

Get Sub List of Java ArrayList

    package org.best.examples;

      /*
      Get Sub List of Java ArrayList Example
      This Java Example shows how to get sub list of java ArrayList using subList
      method by providing start and end index.
    */
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class GetSubListOfJavaArrayListExample {
    
    public static void main(String[] args) {
    
    //create an ArrayList object
    ArrayList arrayList = new ArrayList();
    
    //Add elements to Arraylist
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    arrayList.add("4");
    arrayList.add("5");
    
    /*
      To get a sub list of Java ArrayList use
      List subList(int startIndex, int endIndex) method.
      This method returns an object of type List containing elements from
      startIndex to endIndex - 1.
      */
    
    List lst = arrayList.subList(1,3);
    
    //display elements of sub list.
    System.out.println("Sub list contains : ");
    for(int i=0; i< lst.size() ; i++)
    System.out.println(lst.get(i));
    
    /*
      Sub List returned by subList method is backed by original Arraylist. So any
      changes made to sub list will also be REFLECTED in the original Arraylist.
      */
    //remove one element from sub list
    Object obj = lst.remove(0);
    System.out.println(obj + " is removed from sub list");
    
    //print original ArrayList
    System.out.println("After removing " + obj + " from sub list, original ArrayList contains : ");
    for(int i=0; i< arrayList.size() ; i++)
    System.out.println(arrayList.get(i));
    
    }
    
    }
    /*
    Output would be
    Sub list contains :
    2
    3
    2 is removed from sub list
    After removing 2 from sub list original ArrayList contains :
    1
    3
    4
    5
    */

Friday, July 29, 2011

Get Size of Java ArrayList and loop through elements

    package org.best.examples;

        /*
      Get Size of Java ArrayList and loop through elements Example
      This Java Example shows how to get size or number of elements currently
      stored in ArrayList. It also shows how to loop through element of it.
    */
    
    import java.util.ArrayList;
    
    public class GetSizeOfArrayListExample {
    
    public static void main(String[] args) {
    //create an ArrayList object
    ArrayList arrayList = new ArrayList();
    
    //Add elements to Arraylist using
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    
    //To get size of Java ArrayList use int size() method
    int totalElements = arrayList.size();
    
    System.out.println("ArrayList contains...");
    //loop through it
    for(int index=0; index < totalElements; index++)
    System.out.println(arrayList.get(index));
    
    }
    }
    
    /*
    Output would be
    ArrayList contains...
    1
    2
    3
    */

Thursday, July 28, 2011

Copy all elements of Java ArrayList to an Object Array

    package org.best.examples;

    /*
      Copy all elements of Java ArrayList to an Object Array Example
      This Java Example shows how to copy all elements of Java ArrayList object to an
      array of Objects using toArray method.
    */
    
    import java.util.ArrayList;
    
    public class CopyElementsOfArrayListToArrayExample {
    
    public static void main(String[] args) {
    //create an ArrayList object
    ArrayList arrayList = new ArrayList();
    
    //Add elements to ArrayList
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    arrayList.add("4");
    arrayList.add("5");
    
    /*
      To copy all elements of java ArrayList object into array use
      Object[] toArray() method.
      */
    
    Object[] objArray = arrayList.toArray();
    
    //display contents of Object array
    System.out.println("ArrayList elements are copied into an Array.
    Now Array Contains..");
    for(int index=0; index < objArray.length ; index++)
    System.out.println(objArray[index]);
    }
    }
    
    /*
    Output would be
    ArrayList elements are copied into an Array. Now Array Contains..
    1
    2
    3
    4
    5
    */

Wednesday, July 27, 2011

Append all elements of other Collection to Java ArrayList

    package org.best.examples;

        /*
      Append all elements of other Collection to Java ArrayList Example
      This Java Example shows how to append all elements of other Collection object
      at the end of Java ArrayList object using addAll method. This program shows
      how to append all elements of Java Vector to Java ArrayList object.
    */
    
    import java.util.ArrayList;
    import java.util.Vector;
    
    public class AppendAllElementsOfOtherCollectionToArrayListExample {
    
    public static void main(String[] args) {
    
    //create an ArrayList object
    ArrayList arrayList = new ArrayList();
    
    //Add elements to Arraylist
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    
    //create a new Vector object
    Vector v = new Vector();
    v.add("4");
    v.add("5");
    
    /*
      To append all elements of another Collection to ArrayList use
      boolean addAll(Collection c) method.
      It returns true if the ArrayList was changed by the method call.
      */
    
    //append all elements of Vector to ArrayList
    arrayList.addAll(v);
    
    //display elements of ArrayList
    System.out.println("After appending all elements of Vector,
    ArrayList contains..");
    for(int i=0; i<arrayList.size(); i++)
    System.out.println(arrayList.get(i));
    
    }
    }
    
    /*
    Output would be
    After appending all elements of Vector, ArrayList contains..
    1
    2
    3
    4
    5
    */

Tuesday, July 26, 2011

Add an element to specified index of Java ArrayList

    package org.best.examples;

    /*
      Add an element to specified index of Java ArrayList Example
      This Java Example shows how to add an element at specified index of java
      ArrayList object using add method.
    */
    
    import java.util.ArrayList;
    
    public class AddElementToSpecifiedIndexArrayListExample {
    
    public static void main(String[] args) {
    //create an ArrayList object
    ArrayList arrayList = new ArrayList();
    
    //Add elements to Arraylist
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    
    /*
      To add an element at the specified index of ArrayList use
      void add(int index, Object obj) method.
      This method inserts the specified element at the specified index in the
      ArrayList.
      */
    arrayList.add(1,"INSERTED ELEMENT");
    
    /*
      Please note that add method DOES NOT overwrites the element previously
      at the specified index in the list. It shifts the elements to right side
      and increasing the list size by 1.
      */
    
    System.out.println("ArrayList contains...");
    //display elements of ArrayList
    for(int index=0; index < arrayList.size(); index++)
    System.out.println(arrayList.get(index));
    
    }
    }
    
    /*
    Output would be
    ArrayList contains...
    1
    INSERTED ELEMENT
    2
    3
    */