10/15/10

ArrayList inside a HashMap



package testproject;


import java.util.*;


/**
 *
 * @author Raghuram
 */
public class Collectionwithin {


    public static void main(String[] args) {




// simple arraylist and generic hashmap manipulation.
        ArrayList simplelist = new ArrayList();


        simplelist.add(1);


        HashMap<String, ArrayList> simplemap = new HashMap<String, ArrayList>();


        simplemap.put("simple", simplelist);


        ArrayList simConvert = simplemap.get("simple");


        System.out.println("value of simply converted arraylist --- >" + simConvert.get(0));


// simple generic arraylist and generic hashmap manipulation


        ArrayList<Integer> complexlist = new ArrayList<Integer>();


        complexlist.add(1);


        HashMap<String, ArrayList> complexmap = new HashMap<String, ArrayList>();


        complexmap.put("simple", complexlist);


        ArrayList complexConvert = complexmap.get("simple");


        System.out.println("value of complex converted arraylist --- >" + complexConvert.get(0));




// custom generic arraylist and generic hashmap
        Mytype mt = new Mytype();


        ArrayList<Mytype> customlist = new ArrayList<Mytype>();


        customlist.add(mt);


        HashMap<String, ArrayList> custommap = new HashMap<String, ArrayList>();


        custommap.put("simple", customlist);


        ArrayList<Mytype> customconvert = custommap.get("simple");


        Mytype m = (Mytype) customconvert.get(0);


        m.display();


        for(Mytype my:customconvert){
            my.forEachTest();
        }


    }
}


class Mytype {


    void display() {
        System.out.println("mytype display method.");
    }
     void forEachTest() {
        System.out.println("mytype forEachTest method.");
    }
}

No comments:

Post a Comment

Popular Posts