Collections Continuation

ArrayList

The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.
Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold.
Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.

uses a dynamic array for storing the elements.

can contain duplicate elements.

maintains insertion order.

not synchronized.

random access because array works at the index basis.


manipulation slow because a lot of shifting needs to be occurred.


Example:

The following program illustrates several of the methods supported by ArrayList:
Code:
?
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
package com.javaatlas.arraylist;
import java.util.ArrayList;
public class MyBasicArrayList {
    public static void main(String[] a){
         
        ArrayList<String> al = new ArrayList<String>();
        //add elements to the ArrayList
        al.add("JAVA");
        al.add("C++");
        al.add("PERL");
        al.add("PHP");
        System.out.println(al);
        //get elements by index
        System.out.println("Element at index 1: "+al.get(1));
        System.out.println("Does list contains JAVA? "+al.contains("JAVA"));
        //add elements at a specific index
        al.add(2,"PLAY");
        System.out.println(al);
        System.out.println("Is arraylist empty? "+al.isEmpty());
        System.out.println("Index of PERL is "+al.indexOf("PERL"));
        System.out.println("Size of the arraylist is: "+al.size());
    }
}


Output:
[JAVA, C++, PERL, PHP]
Element at index 1: C++
Does list contains JAVA? true
[JAVA, C++, PLAY, PERL, PHP]
Is arraylist empty? false
Index of PERL is 3
Size of the arraylist is: 5

LinkedList


It uses doubly linked list to store the elements.



It extends the AbstractList class and implements List and Deque interfaces.



can contain duplicate elements.



maintains insertion order.



not synchronized.



No random access.



manipulation fast because no shifting needs to be occurred.



can be used as list, stack or queue.

LinkedList class in collection framework

Example:

The following program illustrates several of the methods supported by LinkedList:


Code:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.javaatlas.linkedlist;
import java.util.LinkedList;
public class MyBasicOperations {
    public static void main(String a[]){
         
        LinkedList<String> ll = new LinkedList<String>();
        ll.add("Orange");
        ll.add("Apple");
        ll.add("Grape");
        ll.add("Banana");
        System.out.println(ll);
        System.out.println("Size of the linked list: "+ll.size());
        System.out.println("Is LinkedList empty? "+ll.isEmpty());
        System.out.println("Does LinkedList contains 'Grape'? "+ll.contains("Grape"));
    }
}

Output:
[Orange, Apple, Grape, Banana]
Size of the linked list: 4
Is LinkedList empty? false
Does LinkedList contains 'Grape'? true

Vector

Vector implements a dynamic array. It is similar to ArrayList, but with two differences:
  • Vector is synchronized.
  • Vector contains many legacy methods that are not part of the collections framework.
Vector proves to be very useful if you don't know the size of the array in advance.

Example:

The following program illustrates several of the methods supported by this collection:
Code:
?
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
package com.javaatlas.vector;
import java.util.Vector;
public class BasicVectorOperations {
    public static void main(String a[]){
        Vector<String> vct = new Vector<String>();
        //adding elements to the end
        vct.add("First");
        vct.add("Second");
        vct.add("Third");
        System.out.println(vct);
        //adding element at specified index
        vct.add(2,"Random");
        System.out.println(vct);
        //getting elements by index
        System.out.println("Element at index 3 is: "+vct.get(3));
        //getting first element
        System.out.println("The first element of this vector is: "+vct.firstElement());
        //getting last element
        System.out.println("The last element of this vector is: "+vct.lastElement());
        //how to check vector is empty or not
        System.out.println("Is this vector empty? "+vct.isEmpty());
    }
}


Output:
[First, Second, Third]
[First, Second, Random, Third]
Element at index 3 is: Third
The first element of this vector is: First
The last element of this vector is: Third
Is this vector empty? false


HashSet

HashSet extends AbstractSet and implements the Set interface. It creates a collection that uses a hash table for storage.
A hash table stores information by using a mechanism called hashing. In hashing, the informational content of a key is used to determine a unique value, called its hash code.
The hash code is then used as the index at which the data associated with the key is stored. The transformation of the key into its hash code is performed automatically.
contains unique elements only.

Example:

The following program illustrates several of the methods supported by HashSet:
Code:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.javaatlas.hashset;
import java.util.HashSet;
public class MyBasicHashSet {
    public static void main(String a[]){
        HashSet<String> hs = new HashSet<String>();
        //add elements to HashSet
        hs.add("first");
        hs.add("second");
        hs.add("third");
        System.out.println(hs);
        System.out.println("Is HashSet empty? "+hs.isEmpty());
        hs.remove("third");
        System.out.println(hs);
        System.out.println("Size of the HashSet: "+hs.size());
        System.out.println("Does HashSet contains first element? "+hs.contains("first"));
    }
}


Output:
[second, third, first]
Is HashSet empty? false
[second, first]
Size of the HashSet: 2
Does HashSet contains first element? true


LinkedHashSet

This class extends HashSet, but adds no members of its own.
LinkedHashSet maintains a linked list of the entries in the set, in the order in which they were inserted. This allows insertion-order iteration over the set.
That is, when cycling through a LinkedHashSet using an iterator, the elements will be returned in the order in which they were inserted.
The hash code is then used as the index at which the data associated with the key is stored. The transformation of the key into its hash code is performed automatically.
contains unique elements only like HashSet. 
maintains insertion order.

Example:

The following program illustrates several of the methods supported by LinkedHashSet:
Code:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.javaatlas.linkedhashset;
import java.util.LinkedHashSet;
public class MyLkdHashSetOperations {
    public static void main(String a[]){
         
        LinkedHashSet<String> lhs = new LinkedHashSet<String>();
        //add elements to HashSet
        lhs.add("first");
        lhs.add("second");
        lhs.add("third");
        System.out.println(lhs);
        System.out.println("LinkedHashSet size: "+lhs.size());
        System.out.println("Is LinkedHashSet emplty? : "+lhs.isEmpty());
    }
}


Output:
[first, second, third]
LinkedHashSet size: 3
Is LinkedHashSet emplty? : false


TreeSet

TreeSet provides an implementation of the Set interface that uses a tree for storage. Objects are stored in sorted, ascending order.
Access and retrieval times are quite fast, which makes TreeSet an excellent choice when storing large amounts of sorted information that must be found quickly.
contains unique elements only like HashSet. 
maintains ascending order.

Example:

The following program illustrates several of the methods supported by this collection:
Code:
?
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
package com.java2novice.treeset;
import java.util.TreeSet;
public class MyBasicTreeset {
    public static void main(String a[]){
         
        TreeSet<String> ts = new TreeSet<String>();
        ts.add("one");
        ts.add("two");
        ts.add("three");
        System.out.println("Elements: "+ts);
        //check is set empty?
        System.out.println("Is set empty: "+ts.isEmpty());
        //delete all elements from set
        ts.clear();
        System.out.println("Is set empty: "+ts.isEmpty());
        ts.add("one");
        ts.add("two");
        ts.add("three");
        System.out.println("Size of the set: "+ts.size());
        //remove one string
        ts.remove("two");
        System.out.println("Elements: "+ts);
    }
}


Output:
Elements: [one, three, two]
Is set empty: false
Is set empty: true
Size of the set: 3
Elements: [one, three]

No comments:

Post a Comment