Today, I would like to introduce you new methods of pl.jsolve.sweetener.collection.Collections class. Classes included in the package ‘collection’ provides a set of methods to facilitate the work of well-known classes from the core Java: List, Set, Map, array as well.
This method allows user to filter collection passed as first argument by specified criteria.
Assume that we have prepared list of objects:
For this list we can prepare some filtering conditions:
WHERE (company IS NOT NULL) AND (categoriesOfDrivingLicense CONTAINS ANY [‘A’, ‘D’])
WHERE age >= 31
WHERE company.address.street like ‘street’
truncate allows developer to getting piece of the collection. This method is available in two versions:
from - the beginning index, inclusive.
to - the ending index, exclusive.
Every developer knows that the pagination should be done on the database side. Unfortunately, there are situations when we get the whole list, and manually have to split it into pages.
To speed up work on pagination, the paginate method has been created.
Lets say we have a list of the letters of the alphabet:
If you want to display only the four elements, you should use:
Pagination object has the following construction:
One important thing. paginate method never thrown IndexOutOfBoundsException, instead of exception, method returns object with empty collection : elementsOfPage.
Easy and enjoyable, right?
Method chop is very similar to paginate, but you don’t have to invoke it for each page. The signature of this method is the following:
ChoppedElement object has the following construction:
The usage of this method is as simple as using the paginate method. All what you have to do is specify the collection and the number of items per page:
Imagine that you have a huge list of students. In your application you don’t want to display all at once, but instead, you want to group them according to the department, field of study, and the semester. Assuming that the list contains the following objects:
you can do it in an extremely simple way!
In the above example the following groups were created:
Semester: 3, FieldOfStudy: Maths, Department: AEI = > John Deep
Semester: 5, FieldOfStudy: Maths, Department: MT => Peter Hunt
Semester: 7, FieldOfStudy: Computer science, Department: AEI => Lucas Sky
Not always searching for duplicates means search for items with the same references. From time to time, for example, we want to find elements with the same identifiers. How do it do fast? Probably two nested loops with ‘if construction’ inside. What if we distinguish duplicate items by more than one argument? For this reason we have created the duplicates method. The signature is the following:
Suppose that we have a product object:
All products must have a unique pair: producer and product name. So these two fields will help us to find duplicate products:
By invoking the uniques will be returned only unique values. So, it is the opposite method to the duplicates.