Sweetener - Collection Restrictions - Part I

Search through the collection has always been a problem for developers and simple search enforced on the programmer writing many lines of code (Countless foreach, if-else constructions). Fortunately, class Collections solves this problem. The integral part of this class is Criteria and Restriction mechanism, which allows for complex query conditions. The restrictions can be joined in chain by add method. Example criteria is shown below:

Criteria.newCriteria().add(Restrictions.isNotNull("company.name"))
        .add(Restrictions.contains("categoriesOfDrivingLicense", "B","D"))
        .add(Restrictions.greaterOrEquals("age", 24))
        .add(Restrictions.equals("name", "marry", true));


Criteria object created in this way can be used in filter method on Collections

read more


Łukasz Stypka

Java experienced enthusiast, opened for new technologies. Clean code and simple solution fan.

Tomasz Kuryłek

Self-styled and passionate java hipster. Focused on solution that makes user and himself ecstatic..