Java 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.
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
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.Map m = new HashMap(); //Old method Map<DummyClass>3) To add the object to a map you need to use the put function. The KEY1 specifies
m = new HashMap<DummyClass>(); //New method as per Java 1.5 and above
the key by which the element will be identified and dm1 is the object.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.DummyClass1 dm3;List
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.DummyClass dummyClass1 = new DummyClass();2) Now declare a List which will store the list of objects. Just like Maps there
DummyClass dummyClass2 = new DummyClass();
are two different methods to do thisList ar = new ArrayList();//Old method List<DummyClass>3) Now to add the objects to the list. Just as map has put, list has add.
ar = new ArrayList<DummyClass>(); //As per the new methodar.add(dummyClass1);4)To retrieve an object added to the list use the index of the object added
ar.add(dummyClass2);
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.DummyClass dummyClass3 = (DummyClass)ar.get(1);Listimport 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);
}
}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("Am in function fn.Number value is "+number);
}
}