Collection Revisited

This chapter is specially designed to know what all there is in Collection at very advanced level and how it is used in the industry and in business projects. In this chapter you we will explore the following:

  • What is introduced in jdk 1.5, 1.6 and 1.7?
  • How does the compiler treat Collection framework in advanced versions of Java?

  • Size means the number of elements present in ArrayList
  • Capacity means the capability to store elements but in Java it is not restricted,
    When we write
    ArrayList al=new ArrayList();
    by default, the capacity is 10
    This you can see in source code of ArrayList.class.

    See the code below from sun microsystem:

    • So, we can specify our own capacity as well
    • ArrayList al=new ArrayList(6);
    • But remember, this does not mean it only takes six elements

  • To add primitives in ArrayList of jdk 1.4 we have add method which asks for the Object

Example

  • Here, in this case, we must change primitive to Wrapper class as given below. For more details read the previous collection chapter.
    //Below is not an error as we put Object type
    arrayList.add (String.valueOf (34));
    OR
    arrayList.add (new Integer (34));

JDK 1.5

Following are concepts introduced in new version jdk 1.5:

  1. Generics
  2. Auto Boxing
  3. Enhance for loop or for each loop
  • We use generics for type safety, which means only one type of element will get added

  • It does not means Sun Microsystem has changed the add method of class ArrayList

  • It is same as before and only changes done are in the compiler. In this case, the compiler will do more work on your behalf

  • One of compiler tasks, besides compilation, is addition of extra code


Declaring ArrayList in 1.5

  • ArrayList <Integer> al = new ArrayList <Integer>();
  • This means that the compiler will start giving errors if we try to put any elements other than an integer.

  • Point to be remembered: If we look at the byte code, these angular brackets are not considered. This is just for the compiler to give errors.

  • In these <>, classes under Object hierarchy are allowed not primitives

  • In both sides, the class type must be same, No superclass or subclass combination is allowed.
    ArrayList <Object> al = new ArrayList <Integer>();  // not allowedly
    ArrayList <int> al = new ArrayList <int>();  // not allowed – primitives       
    
    In jdk 1.7 right side <Integer> or <int> is not needed
    Like below
    ArrayList <Integer> al = new ArrayList<>();

Adding elements in jdk 1.5

  1. al.add (33) - after the above declaration, we are able to directly add 33 primitive and there is no need of typecasting. This is one of the advantages of generics

  2. Compiler will convert this line on the fly to al.add(new Integer(33))


Retrieving elements in jdk 1.5

  1. al.get(index) in this case, in the earlier versions we must follow these three :

    Object obj=al.get (5) 
    Integer i = (Integer) obj; 
    int val=i.intValue ();
    
  2. int val = al.get (index)- This is also one of the extra features of compiler. Object gets converted into the Integer. If we use the decompiler then byte code we see this in the byte code.

  3. Another way is by using iterator:

    Iterator <Integer> itr=al.iterator (); 
      while (itr.hasNext()) {
        int element =itr.next(); [ Due to generics we return int directly]
      }
    

The foreach loop in java is used for traversing arrays or collection elements. It is sometimes referred to as the For In loops

  • In this case we need to remember these points below:
    • No increment decrement
    • No size calculation
    • Must use generics for iterating
    • ArrayList<String> al=new ArrayList<String>(); 
        for (String element : al){
          System.out.println (element)
      }
      
    • This for loop is for compiler. It always gets converted into a simple ordinary for loop
    • ArrayList<Student> al=new ArrayList<Student>(); 
        for (Student stu: al) {
          System.out.println (stu.age); 
          System.out.println (stu.loc);
        }
      
    • This way of iterating element is most commonly used in industrial projects.
  • Autoboxing means automatic conversion of objects
  • This happens in generics; there we don't need type casting
  • Do not think it is related to collection, it's just type
    • Object o =3; // OK in jdk 1.5 because compiler will convert it to Integer
    • Integer i=78; // OK
    • int ii=90; // OK

Business Use

Consider the following requirement:
Question: One student has age and location which needs to be added in the Arraylist, and give to ten students to other department.

Answer: First we design student as below:


Example 1: Write class which prepares Students and returns from one of method.


Now, write the class which gets students from above class's buildStudents () method and iterate over them to get students age and location.
Example 2:


Write client class to test our logic.

Question: One student, who has age, location and many phone numbers, needs to be added in ArrayList and given to other department which has ten students.


For Fetching Students use the method given below:
Example 4:


Consider the following requirement:

Example 5:
Question: One student has age, location and a phone having two types, one landline number and many mobile numbers. It needs to be added in ArrayList and given to other department which has ten students.
First we design Phone as below:


First we design Student as below having phone attribute: This also shows one to one relationship.

Try adding students and fetching.