View RSS Feed

Recent Blogs Posts

  1. Use an Interface to Compare Objects in Generics

    by , 11-29-2011 at 11:25 PM
    A key aspect of using generics is being able to compare different objects. Consider an example where you are comparing different species of felines.
    Java Code:
    public class ComparableFeline implements Comparable<ComparableFeline> {
    
    	@Override
    	public int compareTo(ComparableFeline arg0) {
    		return 0;
    	}
    }
    So if you wanted to narrow ComparableFeline for use only among different types of domesticated cats instead of other felines such as ...

    Updated 11-30-2011 at 08:37 AM by Advanced Java

    Categories
    Generics
  2. Use Lists instead Arrays in Generics

    by , 11-29-2011 at 11:20 PM
    Generics were added to java in release 1.5. It allows you to avoid needing to cast objects as they are read from a collection. Instead you tell the compiler what types of objects that you want to hold in the collection. Often it is difficult to know which generic to use when you have two or more who seem to be able to complete the same function. Consider the choice between whether to use a list or an array. Now a key thing to remember is that arrays are covariant and know and enforce their element ...
  3. Wildcards with Generics

    by , 11-29-2011 at 11:16 PM
    Unlike with arrays, it is important to remember with generics that is not possible to upcast an object from its superclass to its subclass. So for example, this will create a compile time error.

    Java Code:
    import java.util.*;
    
    public class NonCovariantGenerics { 
    // Compile Error: incompatible types: 
    List<Shape> slist = new ArrayList<Rectangle>();
    In this listing List of Rectangle is not type-equivalent to a List of Shape, even if an Rectangle ...
  4. Supertype Wildcards with Generics

    by , 11-29-2011 at 11:12 PM
    In my last tip, I showed how you can use a wildcard with generics. It is also possible to use a supertype wildcards. In this case the wildcard is bounded by any base class of a particular class, by specifying <? super MyClass> or even using a type parameter: <? super T> (although you cannot give a generic parameter a supertype bound; that is, you cannot say <T super MyClass>). This allows you to safely pass a typed object into a generic type. Thus, with supertype wildcards you ...
  5. Unbounded Wildcards with Generics

    by , 11-29-2011 at 11:09 PM
    When you as a programmer decide to use an unbounded wildcard <?>, it appears to mean anything. and so using an unbounded wildcard seems equivalent to using a raw type. There are time that the compiler is indifferent as to using raw type or <?>. If you use <?> it can be considered as a decoration. When you are using the raw type, the generic parameter can hold any type. In this example I show an important use of unbounded wildcards. If you are dealing with multiple generic parameters, ...
  6. Dynamic Type Safety in Pre Java 5 Code Collections

    by , 11-29-2011 at 10:40 PM
    Sometimes is might be necessary to pass generic containers to pre-Java5 code. In those cases, it is possible that the old-style code can corrupt your containers. In order to address this issue, there are a set of utilities in the java.util.Collections package to address the issues around type-checking. There are a number of static methods designed for this. They all take the container that you are checking as the first argument and the type you want to enforce as the second argument. They are shown ...
  7. Comparing Objects and HashCode

    by , 11-29-2011 at 10:30 PM
    Another thing that many programmers forget when they make objects that they need to compare in their applications is that it is not enough to assume that if your object is a subclass of Object, that the hashCode method that you can use it in a HashMap, Hashtable or HashSet. In fact, the Java specification states that you must always override the hashCode when ever you override the equals method. Otherwise, you have violated one of the key tenants of Java in not ensuring that equal objects have equal ...
  8. Sorting Collections

    by , 11-29-2011 at 05:23 PM
    If you are interested in sorting your collections, the good news is that since the inception of Java 5 it is now possible for collections and arrays to be sorted. This is very useful because it is a hell of a lot easier to find something in a sorted set than in an unsorted set. There are a number of sorting algorithms that can be used for your collections. There are listed below.
    • Bubble Sort algorithm
    • Bi-direction Bubble Sort algorithm
    • Quick Sort algorithm
    • Generic Sorting algorithm
    ...
    Categories
    Tutorial , Sorting
  9. How to do Overloading with Generics

    by , 11-29-2011 at 04:12 PM
    Overloading is a technique often used in object orientated programming languages such as java. In the context of generics unfortunately, classic type of overloading will produce a compile time error because of erasure.

    Java Code:
    import java.util.List;
    
    public class OverloadingExample<W, T> {
    	void f(List<T> v) {
    	}
    
    	void f(List<W> v) {
    	}
    }

    What you must do to overcome this problem is to provide ...
  10. Generics - Exceptions

    by , 11-29-2011 at 03:35 AM
    Due to erasure, exception use is limited with generics. As exceptions are know at both compile and at run time, it renders redundant the use of catch clauses. This also means that a generic class cannot inherit directly or indirectly from Throwable.

    One can though use type parameters in the throws clause of a method declaration. This will allow you to write generic code that varies with the type of a checked exception:

    Java Code:
    import java.util.List;
    
    public interface
    ...
  11. Using a Collection vs. Using an Iterator

    by , 11-28-2011 at 06:36 PM
    In this tip, we will look at determining the appropriate use of a collection versus using an iterator. One of the main reasons for using an interface is that you can create more generic and flexible code. By writing to an interface rather than an implementation, you can reuse your code for more types of objects.

    The root interface for all sequence containers is Collection. The java.utiLAbstractCollection class provides a default implementation for a Collection, so that you can create ...
  12. How to Sort Objects Using Collections.sort method

    by , 11-08-2011 at 06:19 PM (My Java Tips)
    A programmer usually needs to sort a given set of inputs. Java has inbuilt set of API's for sorting. There is a Comparable interface in java that needs to be implemented by class whose object needs to be compared. Java classes like "Integer", "Date" implements this interface, so object of these classes can be sorted easily by Collection's sort method. We can make a class that implements Comparable interface so that it's objects can be sorted by "Collections.sort" method. ...