I am learning Groovy and I am trying to write an alternative to the following bit of Java code.
Collection<Record> records = requestHelper.getUnmatchedRecords();
Collection<Integer> recordIdentifiers = new ArrayList<>();
for (Record record : records){
int rowId = record.getValue("RowID");
if (rowId >= min && rowId <= max) {
recordIdentifiers.add(rowId);
}
}
When that bit of code is run recordIdentifiers should contain 50 items. This is my Groovy equivalent so far.
def records = requestHelper.getUnmatchedRecords()
def recordIdentifiers = records.findAll{record ->
int rowId = record.getValue("RowId")
rowId >= min && rowId <= max
}
For some reason the array contains 100 items after Groovy code is executed. All the examples of findAll() I have come across do simple comparisons when the array is constructed natively in Groovy, but how do you filter a Collection that you receive from a Java class?
Seems strange. The following code works fine:
def records = [[r:3],[r:5],[r:6],[r:11],[r:10]]
def range = (1..10)
recordIdentifiers = records.findAll { range.contains(it.r) }
assert recordIdentifiers.size() == 4
Could You please provide a working example?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With