Friday, 22 June 2012

(June 18' 2012 - June 22' 2012)

First 3 days of this week we learned about Generics & Collections and on 21st-22nd, we had tests and brush-up sessions.TheA collection is a group of data manipulate as a single object. term generics means parametrized types. Using generics, it is possible to create
a single class that automatically works with different types of data.

Example:

import java.util.ArrayList;
import java.util.List;
public class GenericList {
   
public static void main(String[] args) {
     
List list=new ArrayList();
         list
.add("aaa");
         list
.add("bbb");
         list
.add("fff");
         list
.add("ccc");
         list
.add("ddd");
         
System.out.println(list);
   
}
}
 
Output:

[aaa, bbb, fff, ccc, ddd]


Collections A collection is a group of data manipulate as a single object. These are similar to C++'s Standard Template Library (STL) and contain only Objects (reference types).

Example:-

import java.util.HashSet;
import java.util.Collections;

public class Collection{

  public static void main(String[] args) {
 
    HashSet hashSet = new HashSet();
  
    hashSet.add(new Long("92"));
    hashSet.add(new Long("42"));
    hashSet.add(new Long("23"));
    hashSet.add(new Long("32"));
  
    Object obj = Collections.min(hashSet);
  
    System.out.println("Minimum Element of Java HashSet is : " + obj);
  }
}


OUTPUT:

Minimum Element of Java HashSet is : 23