Today I discovered a feature of java that I have been using for a while but I didn’t know it existed. Generics allow developers to create generic algorithms that work on collections of different data types but at the same time provide type safety.
Generics are commonly used for data structures. List is an example of a data structure that uses generics:
1
List<String> list = new ArrayList<String>();
You could have declared your list like this:
1
List list = new ArrayList();
The difference is that by using generics you make sure that you don’t accidentally try to insert something that is not a string into your list and cause a run time error. By specifying in the list declaration that this is a List of Strings you make sure that you get a compile time error if you try to add something that is not a string to the list.
This is all cool but the coolest part is that you can create your own classes that use generics:
1
2
3
4
5
6
7
8
9
10
11
public class OneElement<T> {
private T element;
public void set(T el) {
this.element = el;
}
public T get() {
return element;
}
}
And you can can use them like this:
1
2
3
4
5
6
7
OneElement<String> o = new OneElement<String>();
o.set("hello world");
System.out.println(o.get()); // Prints hello world
OneElement<Integer> e = new OneElement<Integer>();
e.set(4);
System.out.println("" + e.get()); // Prints 4
mobile
java
programming
]