View RSS Feed

Advanced Java

Advanced Java programming.

  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. 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 ...