A problem - developing a general-purpose list of elements
Suppose we're writing an application where we need a general-purpose 'container' that would contain many 'elements'. To be more technical, this 'container' can be called a 'list', and its 'elements' can be virtually anything - integer, float, string or any type of object.So how should we write a class for it? (Ignore for the moment that we already have a built-in List interface and its various implementations.) Let's try writing such a class:
public class List {
//here in an instance variable (perhaps an array)
//we will store the elements
public void add(??? element) { //add the element to the store } public ??? getElementAt(int index) { //return the element at index } //some more methods as needed... }Concentrate on the highlighted portions in the code above. Yes, the challenge is to provide a single data-type which can be used for anything - integer, strings, objects and so on.
Initial solution - use the Object type
Object class is the parent of all classes. So, using it will solve the problem of a generic data-type. Of course you won't be able to use the primitive data types (int, float, double etc.), rather you'll use their corresponding wrapper classes (Integer, Float, Double etc.).Problem with using Object type
However, using the Object type would cause your list to include elements of various types. For example, you could have a list with 2 integers, a float and 3 strings. This may cause problems. As an illustration of such a problem, consider the fact that you might like your list to be sorted in some order (actually it's a common requirement for a list of elements). If all the elements in the list were of a single type only, then there won't have been any problems. But as the list contains both integer and string values, how would you sort them??Meet Generics
Generics came to solve this problem. To make sure that you use a list for elements with the same data-type, a special syntax is used:public class List<E> {
//here in an instance variable (perhaps an array)
//we will store the elements
public void add(E element) { //add the element to the store } public E getElementAt(int index) { //return the element at index } //some more methods as needed... }The E in the above refers to a generic element type. Whenever you instantiate the class, you will replace the E with whatever type you want all your list elements to be of. For example, below we instantiate the list class to include only integer elements:
List<Integer> integerList = new List<Integer>(); integerList.add(5); int element = integerList.getElementAt(0);Similarly, we can use the exact same class for storing strings:
List<String> stringList = new List<String>();
stringList.add("Hello World");
String element = stringList.getElementAt(0);
So this is all about generics. Simple, isn't it?In the next Java blog post, we'll see some examples of generics through using the Java Collections Framework.
Nice example of Generics
ReplyDeleteThanks....
ReplyDelete