Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy filtering array with findAll method

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?

like image 922
user283188 Avatar asked Dec 09 '22 05:12

user283188


1 Answers

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?

like image 183
Opal Avatar answered Dec 11 '22 12:12

Opal