I am trying to optimize the filter method for RecyclerView Adapter
in Android. The list is used as an ArrayList
. I have seen this post but they are filtering from the original list every time. They are not using old results if a character is added.
Example: if there are 10 results for String 'a' then user type 'm' , 'am' results are subset of 'a' results.
Please suggest code optimizations for this method and any test-case where it fails.
public void filter(String s) {
if (TextUtils.isEmpty(s)) {
mList.clear();
mList.addAll(copyList);
notifyItemRangeRemoved(0, getItemCount());
notifyItemRangeInserted(0, mList.size());
} else {
boolean isBackDirection = false;
List<WeekWorkBean> tempList = null;
if (s.length() < oldFilter.length()) {
tempList = new ArrayList<>();
tempList.addAll(mList);
mList.clear();
mList.addAll(copyList);
isBackDirection = true;
}
for (int i = 0; i < mList.size(); i++) {
WeekWorkBean bean = mList.get(i);
if (!bean.getEmpName().toLowerCase().startsWith(s.toLowerCase())) {
mList.remove(i);
if (!isBackDirection) {
notifyItemRemoved(i);
}
i--;
} else if (isBackDirection) {
if (tempList.size() != 0) {
for (WeekWorkBean b : tempList) {
if (!bean.getEmpId().equals(b.getEmpId())) {
notifyItemInserted(i);
}
}
} else {
notifyItemInserted(i);
}
}
}
oldFilter = s;
}
}
1 Answer 1
Consider using iterator instead of for-looping. That will get rid of your index manipulation (decrement if element is removed).
Iterator iterator = mList.iterator();
while(iterator.hasNext()){
Element e = iterator.next();
if(shouldRemove(e)){
iterator.remove();
}
}
Is there a performance requirement that necessitate implementation of such algorithm? In most cases, implementing conventional interfaces make code readable and modular and you'll probably benefit from performance increase should the way Android perform filtering is optimised further down the road, without doing anything.
In any case, what makes you think that this implementation is faster? Your reliance on copying ArrayLists may prove wasteful if the unfiltered list is exceptionally large.
-
\$\begingroup\$ I have some questions regarding new approach. Cannot edit this question. I would be glad if you answer those questions in my post. codereview.stackexchange.com/questions/148333/… \$\endgroup\$Harish Gyanani– Harish Gyanani2016年11月28日 13:19:02 +00:00Commented Nov 28, 2016 at 13:19