Software Development

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

Background

[wp_ad_camp_1]

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.

Software Environment

  • Windows 7 Professional SP1
  • Eclipse – Kepler Release
  • Java 1.7 (1.7.0_67 – Windows x86)
  • Apache Commons Collections 4

pom.xml

The Codes

All classes are put in a simple file – CollectionUtilsPredicateSample001.java. For brevity, the main method contains the following codes.

[wp_ad_camp_2]

Sample Output

Download the Project

[wp_ad_camp_3]

https://www.dropbox.com/s/w0p0kt6d0pqaf5f/TurretaApacheCommons.zip?dl=0

 

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like