Showing posts with label Articles - Collections. Show all posts
Showing posts with label Articles - Collections. Show all posts

Thursday, September 29, 2011

What is the different between Set and List


Set and List explanation
  • Set – Stored elements in unordered or shuffles way, and does not allow duplicate values.
  • List – Stored elements in ordered way, and allow duplicate values.
Set and List Example
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SetAndListExample
{
    public static void main( String[] args )
    {
        System.out.println("List example .....");
        List<String> list = new ArrayList<String>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("1");

        for (String temp : list){
               System.out.println(temp);
        }

        System.out.println("Set example .....");
        Set<String> set = new HashSet<String>();
        set.add("1");
        set.add("2");
        set.add("3");
        set.add("4");
        set.add("1");
        set.add("2");
        set.add("5");

        for (String temp : set){
               System.out.println(temp);
        }       
    }
}
Output
List example .....
1
2
3
4
1
Set example .....
3
2
10
5
4
In Set, the stored values are in unordered way, and the duplicated value will just ignored.

Tuesday, July 26, 2011

ArrayList

Java ArrayList is a resizable array which implements List interface. ArrayList provides all operation defined by List interface. Internally ArrayList uses an array to store its elements. ArrayList provides additional methods to manipulate the array that actually stores the elements. ArrayList is equivalent to Vector, but ArrayList is not synchronized.

Java ArrayList Capacity

Capacity of an ArrayList is the size of the array used to store the list elements. It grows automatically as we add elements to it. Every time this happens, the internal array has to be reallocated. This increases the load.
We can set the initial capacity of the ArrayList using following method.
ArrayList arrayList = new ArrayList();
arrayList.ensureCapacity(100);

Java ArrayList Iterators

Java ArrayList provides two types of Iterators.
1) Iterator
2) ListIterator

                  Iterator iterator = arrayList.iterator();
Returns object of Iterator.
                  ListIterator listIterator = arrayList.listIterator();
Returns object of ListIterator.
                  ListIterator listIterator = arrayList.listIterator(int startIndex);
Returns object of ListIterator. The first next() method call on this ListIterator object will return the element at the specified index passed to get the ListIterator object.

Iterators returned by these methods are fail-fast. That means if the list is modified after getting the Iterator by using some other means rather than Iterators own add or remove method, Iterator will throw ConcurrentModificationException.

ArrayList Constructors

1) ArrayList()
Creates an empty ArrayList.
For example,
ArrayList arrayList = new ArrayList();

2) ArrayList(int capacity)
Creates an ArrayList with specified initial capacity.
For example,
ArrayList arrayList = new ArrayList(10);

3) ArrayList(Collection c)
Creates an ArrayList containing elements of the collection specified.

For example,
ArrayList arrayList = new ArrayList(myCollection);

Where myCollection is an object of the type Collection. This creates an ArrayList of elements contained in the myCollection, in the order returned by the myCollection’s Iterator.