Java Collections: The Basics

One long section of code outlining how to use the Java Collections. As per the Java Strings post, this consists of one long code snippet outlining commonly used Java collections such as Hash Maps, Linked Lists etc. As this is ongoing, expect to see newer stuff added as time progresses.

package javacollections;
import java.util.*;

public class JavaCollections 
{
   public static void main(String[] args) 
    {
        // 1. Hash maps
        HashMap hm = new HashMap();
        hm.put("Tinker",  new Double(3434.34));
        hm.put("Tailor",  new Double(123.22));
        hm.put("Soldier", new Double(123.22));
        hm.put("Spy",     new Double(123.22));        
        Double value = (Double) hm.get("Soldier");    // 123.22
        
        // 2. HashSet: holds unique values only
        Set set = new HashSet();
        set.add("XYZ");
        set.add("ABC");
        set.add("BCD");
        set.add("ABC");
        int size = set.size();                   // 3       
        set.remove("ABC");
        boolean contains = set.contains("ABC");  // false
        contains = set.contains("XYZ");          // true
        int number = set.size();                 // 2
        
        // Create array containing string elements in set
        String[] array = (String[]) set.toArray(new String[set.size()]);
        
        // Iterate over set elements
        Iterator it = set.iterator();
        while (it.hasNext()) 
        {
            Object element = it.next();
            String str = (String) element;
            System.out.println( str );
        }
                
        // 3. List - a doubly-linked list       
        List list = new LinkedList();    // Doubly-linked list               
        list.add("a");          // Append element to list      
        list.add(0, "b");       // Insert element at head of list
        list.add(0, "b");       // Ditto
        list.add(0, "c");       // Ditto       
        size = list.size();     // 3       
        Object element = list.get(list.size()-1);   // "a"       
        element = list.get(0);                      // "a"      
        boolean b = list.remove("b");      // Remove 1st occurence - true
        b = list.remove("b");              // true
        list.remove( 0 );                  // Remove element at index 
        
        // 4. Map - type specific
        Map<Integer, String> map = new HashMap<Integer, String>();
        map.put(1, "first");        
        map.put(21, "twenty first");
        map.put(22, "twenty second");
        String text = map.get(21);    // "twenty first"
        
        // 5. Sorted Set
        SortedSet sset = new TreeSet();       
        sset.add("XYZ");
        sset.add("BCD");
        sset.add("ABC");
        sset.add("CBA");
        System.out.println(sset);  
                 
        // 6. Arraylist
        ArrayList al = new ArrayList();
        al.add("C");
        al.add("A");
        al.add("E");
        System.out.println(al);
        al.add(1, "A2"); 
        System.out.println(al);
        System.out.println(al.toString());  
        
        // Dictionary / Vector considered obsolete
        // use HashMap / ArrayList respectively
    }
}

Leave a Reply