0

Convenient Way to override equals(), hashCode(), and toString() methods

Two years ago, I used to override the equals() and hashCode() methods via Eclipse’s “Generate hashCode() and equals()…” feature. It proved to be a maintenance nightmare. Every time a new field is added to a class, these methods are updated to include the new instance variable. There is a convenient way to override these methods, toString() included. One that’ll not require subsequent changes when fields are added or remove from a class. Continue Reading

0

How to find objects with specific properties in a List using Apache Commons Collections

You overridden your JavaBean class’ hashCode and equals methods so you can easily find (and manipulate) objects in a list. Now most properties are used in those methods. But what if you want to search for a list of objects by one or two properties only? Surely, you do not need to create a “search” object and set all its properties with values. Will you resort to loop and conditional statements? Will you modify the hashCode and equals methods? To introduce the least possible codes and/or not modify existing JavaBeans, we could use Predicate interface in conjunction with ListUtils from Apache Commons Collection. Continue Reading