+ Reply to Thread
Results 1 to 1 of 1

Thread: Tutorial: Java Collections

  1. #1
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Tutorial: Java Collections

    Collections
    Collections are an interface in the java.util package, and as its name suggests, it is used to define a collection of objects. There are many different classes which implement the collections interface. Each one has its unique features, some add elements in a sorted manner, others in a binary fashion. But there are two basic structures, a map and a list and most others extended these interfaces.





    Here is how you do it
    1) Declare any object you wish to insert into a Map. Now note that a map stores value in a <KEY><VALUE> pair. Say we want to store an object of DummyClass
    Code:
    DummyClass1 dm2 = new  DummyClass1();
    2) Now you declare a HashMap which will store the key value pair. Now since Java 1.5 it is mandatory for you to specify the type of objects that the map will store. Previously it was not mandatory, so a map could have any type of objects. But now you have to specify the type of objects that the map will store.


    Code:
    Map m = new HashMap(); //Old method Map<DummyClass> 
    
    m = new HashMap<DummyClass>();  //New method as per Java 1.5 and above
    3) To add the object to a map you need to use the put function. The KEY1 specifies the key by which the element will be identified and dm1 is the object.

    Code:
    m.put("KEY1", dm1);
    4) To retrieve this object use the get method and use KEY1 to identify the object to retrieve it. Note that the get method returns an Object . So you need to type cast it into a the class you want.


    Code:
    DummyClass1 dm3;
    dm3 = (DummyClass1)m.get("KEY1");
    List
    1) Declare the objects you wish to add in the list. The process is same as map, except that you only have a value to store, no need for a key.

    Code:
    DummyClass dummyClass1 = new  DummyClass();
    DummyClass dummyClass2 = new DummyClass();
    2) Now declare a List which will store the list of objects. Just like Maps there are two different methods to do this.

    Code:
    List ar = new ArrayList();//Old method List<DummyClass> 
    
    ar = new ArrayList<DummyClass>();  //As per the new method
    3) Now to add the objects to the list. Just as map has put, list has add.

    Code:
    ar.add(dummyClass1);
    ar.add(dummyClass2);
    4)To retrieve an object added to the list use the index of the object added to the list to identify it and retrieve the list. The get method returns an object so it has to be type casted to the class you want it to be.

    Code:
    DummyClass dummyClass3 = (DummyClass)ar.get(1);
    Code
    Code:
    import java.util.HashMap;
    import java.util.Map;
    
    
    public class Maps {
        public static void main(String[] args) {
            DummyClass1 dm1 = new DummyClass1(300);
            DummyClass1 dm2 = new DummyClass1(300);
    
            Map m = new HashMap();
            m.put("KEY1", dm1);
            m.put("KEY2", dm2);
            m.put("KEY3","This is a string object" );
    
            DummyClass1 dm3;
            dm3 = (DummyClass1)m.get("KEY1");
            dm3.fn();
            System.out.println((String)m.get("KEY3"));
        }
    }
    
    class DummyClass1{
        int number;
        public DummyClass1(int number) {
            this.number = number;
        }
        public void fn()
        {
            System.out.println("Number value is "+number);
        }
    }
    Output
    Code:
    Number value is 300
    This is a string object
    List
    Code:
    import java.util.ArrayList;
    import java.util.List;
    
    
    public class ArrayLst
    {
        public static void main(String[] args)
        {
            DummyClass dummyClass1 = new DummyClass(100);
            DummyClass dummyClass2 = new DummyClass(200);
            List<DummyClass> ar = new ArrayList();
            ar.add(dummyClass1);
            ar.add(dummyClass2);
            System.out.println("The size of the list is "+ar.size());
            DummyClass dummyClass3 = (DummyClass)ar.get(1);
            dummyClass3.fn();
        }
    }
    
    class DummyClass
    {
        int number;
        DummyClass(int number)
        {
            this.number = number;
        }
        public void fn()
        {
            System.out.println("\nAm in function fn.\nNumber value is "+number);
        }
    }
    Output
    Code:
    The size of the list is 2
    
    Am in function fn.
    Number value is 200


    Last edited by Jordan; 05-06-2008 at 11:44 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Tutorial: Starting Java Using Netbeans
    By Jordan in forum Java Tutorials
    Replies: 4
    Last Post: 02-27-2010, 05:20 PM
  2. Tutorial: Java Threads
    By Jordan in forum Java Tutorials
    Replies: 1
    Last Post: 05-17-2008, 10:37 AM
  3. Java tutorial : my sql with java
    By Arkie in forum Java Tutorials
    Replies: 2
    Last Post: 04-05-2008, 12:51 PM
  4. John's Java Tutorial Index
    By John in forum Java Tutorials
    Replies: 0
    Last Post: 01-11-2007, 03:05 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts