سؤال

I'm working on speed issues with a currently working method that finds a specific attribute collection within an ArrayList. Depending on the size, it can take longer than 7 seconds to find the value in the list.

I need to speed up this process, so I can deal with larger volumes of data. Any assistance would be greatly appreciated. Here is my example;

Method:

public ArrayList getIntegrationTag(String attribute) {
  return crmMapping?.findAll { it.get("ATTRIBUTE") == attribute }?.collect{
    it.INTEGRATION_TAG 
  }?.unique()
}//end getIntegrationTag(String attribute)

crmMapping content

"[{ATTRIBUTE=AcademicIndex, INTEGRATION_TAG=Contact~nAcademic_Index}, {ATTRIBUTE=AcademicInterest, INTEGRATION_TAG=Contact~msplAcademic_Interest},........]"

هل كانت مفيدة؟

المحلول

the findAll loops over each record, then the collect loops over each record, then unique loops over each record again.

Try...

Set result = [] as Set

for(element in crmMapping) {
  if(element.get("ATTRIBUTE") == attribute) {
    result << element.INTEGRATION_TAG
  }
}

return (result as ArrayList)

This will only loop once it it will be unique as it was added to a Set

نصائح أخرى

Do the following once

def crmMappingMap = crmMapping.groupBy({ it.ATTRIBUTE })

Then you have a map of all same attribute instances and can access it using crmMappingMap[attribute].INTEGRATION_TAG, which will return the desired array, like this:

public ArrayList getIntegrationTag(String attribute) {
    crmMappingMap[attribute].INTEGRATION_TAG.unique()
}

Always keep a map, then speed of access will be fast enough.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top