Collection are not type safe. We can add any type of data to collection. Because of this , while retrieving the objects from collection , we will get proble at runtime. See the below example.

import java.util.List;
import java.util.ArrayList;

public class ListSample {
     public static void main(String[] args) {
            List  countries = new ArrayList();
            countries.add("Japan");
            countries.add(1);
            countries.add("Russia");
            countries.add("Britain");
            for(int i=0 ; i< countries.size(); i++)
                {
                     Object object =countries.get(i);
                     String country = (String)object;    // on 2nd  iteration we will
                     System.out.println(country);        //get class  cast exception
                }
        }
}

/* ======== Output ==========

Japan
Exception in thread "main" java.lang.ClassCastException: 
java.lang.Integer cannot be cast to java.lang.String
        at ListSample.main(ListSample.java:14)


See above example, we added different types of data to collection (String type as well as int type) . So, while retrieving the data from collection we get class cast exception on 2nd iteration at run time.

From the above example , we have seen the problem. To overcome the above problem , we have to restrict the collection to allow particular data types only. Now the question is , how to force the collection to allow particular type of objects only. The answer is GENERICS concept. For this, we have to define the collection as follows.

List <String> countries= new ArrayList<String>();

Here, we are restricting the collection to allow String types only. Now we can add string type data only to countries list . If we are trying to add any other type, we will get compiler error.

List <String> countries= new ArrayList<String>();
countries.add("Japan"); // fine compilation 
countries.add(5) ; // compiler error here. 
no other type is allowed except String type.

One more benefit with generics concept is we need not to type cast while retrieving the data from the collection.