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
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:DummyClass1 dm2 = new DummyClass1();
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:Map m = new HashMap(); //Old method Map<DummyClass> m = new HashMap<DummyClass>(); //New method as per Java 1.5 and above
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:m.put("KEY1", dm1);
ListCode:DummyClass1 dm3; dm3 = (DummyClass1)m.get("KEY1");
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.
2) Now declare a List which will store the list of objects. Just like Maps there are two different methods to do this.Code:DummyClass dummyClass1 = new DummyClass(); DummyClass dummyClass2 = new DummyClass();
3) Now to add the objects to the list. Just as map has put, list has add.Code:List ar = new ArrayList();//Old method List<DummyClass> ar = new ArrayList<DummyClass>(); //As per the new method
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:ar.add(dummyClass1); ar.add(dummyClass2);
CodeCode:DummyClass dummyClass3 = (DummyClass)ar.get(1);
OutputCode: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); } }
ListCode:Number value is 300 This is a string object
OutputCode: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); } }
Code:The size of the list is 2 Am in function fn. Number value is 200
![]()


LinkBack URL
About LinkBacks












Reply With Quote

Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum