Lost Password?

  #1 (permalink)  
Old 05-05-2008, 06:38 PM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Age: 25
Posts: 4,536
Last Blog:
PHP: list()
Rep Power: 50
Jordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud of
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default 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


__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall

Last edited by Jordan; 05-06-2008 at 11:44 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply

Tags
collections, java, tutorial



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Tutorial: Java Threads Jordan Java Tutorials 1 05-17-2008 10:37 AM
Tutorial: Starting Java Using Netbeans Jordan Java Tutorials 0 04-05-2008 02:45 PM
Java tutorial : my sql with java Arkie Java Tutorials 2 04-05-2008 12:51 PM
John's Java Tutorial Index John Java Tutorials 0 01-11-2007 03:05 PM


All times are GMT -5. The time now is 10:27 AM.

Contest Stats

dargueta ........ 93.00000
John ........ 87.50000
Xav ........ 50.00000
MeTh0Dz ........ 20.00000
gaylo565 ........ 18.00000
Johnnyboy ........ 3.00000

Contest Rules

Ads