Thursday, February 23, 2012

Java: Collections


So, this is first Java article, I will start explaining two very important classes related of Java Collections Framework
1)Class java.util.ArrayList and java.util.LinkedList from Java Collections Framework.
These collection Java classes are very important from data perspective.
Important points related of ArrayList|Java Collections:
0-an expandable array. This is the most used and easiest member of the collections framework.
1-If you want to use a thread-safe Collection don’t use ArrayList. Use Vector class as thread-safe.
2-When removing elements, using ArrayList is slow. This is because all remaining elements in the underlying array of Object instances must be shifted
down for each remove operation.So when you need to insert huge amount of objects & || randomly delete them one by one, better to use LinkedList.
3-ArrayList is pretty fast doing random lookup using ‘get’ and LinkedList is slow. So ArrayList works better when you are doing random access on the list and LinkedList works better if you are doing a lot of editing in the middle of the list.
ArrayList Source code example:
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package arraylistexample;
import java.util.ArrayList;
import java.util.List;
  
/*
 * Class created by Thiago Leoncio -->16 Feb 2012
 */
public class ArrayListExample {
   /**
     * Main method.
     * @param args
     * @goal Shows how it works Java Collections Framework.
     */
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<String>();
  
        // Adding values to the ArrayList called names.
        names.add("Thiago");
        names.add("Leoncio");
        names.add("Melaine");
  
        // Retrieving value at position 0.
        System.out.println(names.get(0));
  
        System.out.println("\nIteration ==>1: ");
        // Indexed for loop iteration
        for (int i = 0; i < names.size(); i++) {
            System.out.println(names.get(i));
        }
  
        // Removing items (careful!)
        names.remove(names.size() - 1);
  
        // This will remove the first item and copy the next item and include
        // into the subsequent position. ==> VERY SLOW.
        names.remove(0);
  
        System.out.println("\nIteration ==>2: ");
        for (String value : names) {
            System.out.println(value);
        }
  
        // List interface ...
        List<String> values = new ArrayList<String>();
    }
}
LinkedList source code example:
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
28
29
import java.util.LinkedList;
  
public class LinkedListExample {
/*
 * Class created by Thiago Leoncio -->16 Feb 2012
 */
  
  public static void main(String[] args) {
    
    //create LinkedList object
    LinkedList ThiagoLinkedList = new LinkedList();
    
    //add elements to LinkedList
    ThiagoLinkedList.add("T");
    ThiagoLinkedList.add("h");
    ThiagoLinkedList.add("i");
    ThiagoLinkedList.add("a");
    ThiagoLinkedList.add("g");
    ThiagoLinkedList.add("o");
  
    /*
     * Also primitive values can not be added into LinkedList.
     * They must be converted to their corresponding wrapper class.
     */ 
    
    System.out.println("This collection contains : " + ThiagoLinkedList);
    
  }
}
2) Class java.util.HashMap:
Maps are data collections that function like lookup tables. A Hashmap is a Map data structure.
So with these 2 phrases you can merge and have these features:
1-Store objects via “keys”. Keys are particular index. This index is called a hash and it is generated using a hash function.
2-You can quickly delete those easily.
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
28
29
30
31
32
33
34
35
36
37
package hashmapexample;
import java.util.HashMap;
import java.util.Map;
/*
 * Class created by Thiago Leoncio -->16 Feb 2012
 */
public class HasMapExample {
    /**
      * Main method.
      * @param args
      * @goal Shows how it works HashMap(Java Collections).
      */
    public static void main(String[] args) {
  
        HashMap<Integer, String> map = new HashMap<Integer, String>();
          
        map.put(12, "Thiago Leoncio");
        map.put(15, "Manager");
        map.put(13, "Director");
        map.put(11, "VP");
        map.put(9, "CIO");
           
        String textvalue = map.get(12);  
        System.out.println(textvalue); 
        // this second put will overwrite the first one.
        map.put(9, "CEO");
         
        //Using Map Interface
        for(Map.Entry<Integer, String> entry: map.entrySet()) {
            int key = entry.getKey();
            String value = entry.getValue();
            //Print HashMap Key +: + HashMap value
            System.out.println(key + ": " + value);
        }     
    }
}
I hope this helps,
Thiago Leoncio.