0

I iterate map and want to exit when executed this line:

mostOccurRecognizedCheck = filterList.get(0)

here snippet:

val maxIndexesMap: Map<Int, Int> =
            howOftenCheckIndexMap.filter { it.value == findMaxCount }
    maxIndexesMap.forEach lit@{ (indexMaxCount, value) ->
                val filterList: List<Check> =
                    recognizedCheckList.filterIndexed { index, check -> index == indexMaxCount && check.amount != null }
                if (filterList.isNotEmpty()) {
                    Debug.d(
                        TAG,
                        "getMostOccurRecognizedCheck: get_first_item from RESULT_FILTER_LIST(${filterList.size}) = $filterList"
                    )
                    mostOccurRecognizedCheck = filterList.get(0)
                    return@lit 
                } else {
                    Debug.w(
                        TAG,
                        "getMostOccurRecognizedCheck: filterList_is_empty -> get_item_from_index = $indexMaxCount in recognizedCheckList"
                    )
                    mostOccurRecognizedCheck = recognizedCheckList.get(indexMaxCount)
                }
            }

and here log:

getMostOccurRecognizedCheck: findMaxCount = 1 -> maxIndexesMap = {1=1, 0=1, 3=1, 2=1}
getMostOccurRecognizedCheck: get_first_item from RESULT_FILTER_LIST(1) = [
Check(fuel = GPL, fuelPrice = 9.87, volume = 10.13, amount = 100.0, tva = 7.41, date = Mon Jan 06 15:07:17 GMT+02:00 2020, name=null, address=null, idno=null, codFiscal=null, id=0, transactionID=null
getMostOccurRecognizedCheck: get_first_item from RESULT_FILTER_LIST(1) = [
Check(fuel = null, fuelPrice = null, volume = null, amount = 100.0, tva = null, date = null, name=null, address=null, idno=null, codFiscal=null, id=0, transactionID=null, ]
getMostOccurRecognizedCheck: get_first_item from RESULT_FILTER_LIST(1) = [
Check(fuel = GPL, fuelPrice = 9.87, volume = 10.13, amount = 100.0, tva = null, date = Wed Jan 08 15:07:17 GMT+02:00 2020, name=null, address=null, idno=null, codFiscal=null, id=0, transactionID=null, ]
getMostOccurRecognizedCheck: get_first_item from RESULT_FILTER_LIST(1) = [
Check(fuel = GPL, fuelPrice = 9.87, volume = 10.13, amount = 100.0, tva = null, date = Mon Jan 06 15:07:17 GMT+02:00 2020, name=null, address=null, idno=null, codFiscal=null, id=0, transactionID=null, ]

As you can see the

get_first_item

print 4 times. Why?

I use this to exit

return@lit

but it not help

Alexei
  • 14,350
  • 37
  • 121
  • 240
  • Becase the return@* inside foreach works like a `continue` in java. See [this](https://stackoverflow.com/a/47639682/2235972). Solutions [here](https://stackoverflow.com/questions/32540947/break-and-continue-in-foreach-in-kotlin) – denvercoder9 Jan 15 '20 at 14:13

1 Answers1

2

forEach will run for each element of the map, there's no stopping that. Your return statement will simply return from that single execution of the lambda.

If you want to find some element (and return that) then .find is a better fit:

  maxIndexesMap.find { (indexMaxCount, value) ->
                val filterList: List<Check> =
                    recognizedCheckList.filterIndexed { index, check -> index == indexMaxCount && check.amount != null }
                !filterList.empty() 
  }?.let { (indexMaxCount, value) ->
    // Log
  }

(you need the ?.let in case there are no elements for which filterList is not empty).

Note that this won't log anything for the 'other' cases.

al3c
  • 1,392
  • 8
  • 15