A small step for a man – overcoming Java Generics

Source Code of ArrayList.java

I did it finally. There was one part of Java in which I had never get over and that was Generics, the major feature of Java 5. The syntax was complicated and hard to understand the bunch of mysterious keyword E K N T V S ?. There is total 23 pages in the original Java tutorial of Generics and it proves that it is complicated.

I hate the long and mind boggling tutorial with a lot of jargons ( type parameter, parameterized type, upper bound lower bound, wildcard, type erasure, etc ), but I love ArrayList, which is a generic class. I used it almost everyday in place of the simple Array. I like the many convenient methods provided by ArrayList, such as add(int index,
E element)
,addAll(Collection<? extends E> c)contains(Object o)get(int index),indexOf(Object o),isEmpty(), remove(int index),removeAll(Collection<?> c)sort(Comparator<? super E> c)

I ran across a blog article titled ” Destroying the most common fear – “Generics?! It’s too hard for me!”” on Linkedin. The author tried to simplified the tutorial although it didn’t help much because of the nature of Generics. I read on anyway and write his example in Eclipse and see which works.

Actually I always know the rough idea of Generics, because I know I could put anything in an ArrayList, when I declare them with the correct type like ArrayList<String>, ArrayList<Integer>. It’s the same class and it achieves different purpose because of polymorphism.

Made up class called Bobby

Off my head there are few things I could do with Generics maybe container. There is a box and I don’t know what would be put inside, but I only want to know how many are in there, how do I take one item out, actually I don’t even need to know what would be put inside. That’s basically Java Collection! But Java has already done that.

Java 8 API documentation

Let’s call it a day.

Leave a Reply

Your email address will not be published. Required fields are marked *