How to improve the performance of processing multiple Arraylist request in
a synchronized way to create them into a final list with no...
I have a scenario in which I have a method who gets results as an
Arraylist in a form like as shown in the below picture.
So, as a brief explanation to the picture, I will get Result 1 as the
first chunk of objects, then I will be getting Result 2 which actually
contains Result 1 and a new set of objects, and this goes on.
Note: All these chunks will contain duplicates. So I will have to filter
this out.
My aim is to create one single list out of these chunks without having any
duplicates.
Please find the current code snippet, used in the synchronized method I
call whenever I get a chunk of result, which I am using to implement this:
On every result update, this method will be called with the result arrayList.
private synchronized void processRequestResult(QueryResult result)
{
ArrayList currArrayList = result.getResultsList();
ArrayList tempArrayList = result.getResultsList();
/**
* Remove all elements in prevArrayList from currArrayList
*
* As per the javadocs, this would take each record of
currArrayList and compare with each record of prevArrayList,
* and if it finds both equal, it will remove the record from
currArrayList
*
* The problem is that its easily of n square complexity.
*/
currArrayList.removeAll(prevArrayList);
// Clone and keep the currList for dealing with next List
prevArrayList = (ArrayList) tempArrayList.clone();
for (int i = 0; i < currArrayList.size(); i++)
{
Object resultObject = currArrayList.get(i);
// Check for if it reached the max of items to be displayed in
the list.
if (hashMap.size() >= MAX_RESULT_LIMIT)
{
//Stop my requests
//Launch Message
break;
}
if (resultObject instanceof X)
{
final Integer key =
Integer.valueOf(resultObject.someUniqueID);
hashMap.put(key, (X)myObject);
}
else if (resultObject instanceof Y)
{
final Integer key =
Integer.valueOf(resultObject.someUniqueID);
hashMap.put(key, (Y)myObject);
}
}
// Convert the HashSet to arrayList
allResultsList = new ArrayList(hashMap.values());
//Update the change to screen
}
Please see my inline comments in the code, because of that, I would like
to get some pointers to improve my performance to this process.
No comments:
Post a Comment