是不是有更好的办法,从ArrayList中搜索的HashMap [英] Is it there a better way to Search HashMap from ArrayList
问题描述
我试图实施的ListView
搜索。我的的ListView
在的ArrayList
由的HashMap
和我管理实现逻辑,但我想每一次的文本变化有很多对象和内存分配在我的做法。所以我找了分配的逻辑内存少在我的的ListView
与的HashMap
在搜索的ArrayList
@覆盖
公共无效之前onTextChanged(CharSequence中,诠释开始,诠释,
诠释计数){ // =长度限制:Textlength searchBar.getText()长();
//text_sort.clear();
sortedArrayList.clear();
的for(int i = 0; I< myHistList.size();我++){ HashMap的<字符串,字符串>哈希=新的HashMap<字符串,字符串>();
散列= myHistList.get(ⅰ); 如果(hash.get(MYNAME)。与toLowerCase()的indexOf(searchBar.getText()。toString()方法。与toLowerCase())!= -1){ 串callerNum1 = hash.get(mynumber的);
字符串myName1 = hash.get(MYNAME);
HashMap的<字符串,字符串> searchedHash =新的HashMap<字符串,字符串>(); //将每个子节点的HashMap键=>值
searchedHash.put(mynumber的callerNum1);
searchedHash.put(MYNAME,myName1);
recordingFile1); //添加HashList到ArrayList的
sortedArrayList.add(searchedHash); }
} ListView控件actualLv = mPullRefreshListView.getRefreshableView();
actualLv.setAdapter(新myHistoryListAdapter(myHistory.this,sortedArrayList)); }
起初,你可以替换
的HashMap<字符串,字符串>哈希=新的HashMap<字符串,字符串>();
散列= myHistList.get(ⅰ);
只有
的HashMap<字符串,字符串>散列= myHistList.get(ⅰ);
这将略有减少多余的对象的数量。
在第二个步骤,如果你需要比较字符串是相同的,但忽略字母'情况下,你可以尽量简化你的如果
状态
如果(hash.get(MYNAME)。与toLowerCase()的indexOf(searchBar.getText()。toString()方法。与toLowerCase())!= -1)
与
如果(hash.get(MYNAME)。与compareToIgnoreCase(searchBar.getText()。的toString())== 0)
此外,如果你把字符串callerNum1 = hash.get(mynumber的);
的如果
上述声明那么你就可以节省一些时间,因为你不需要通过你的HashSet两次看向搜索相同的元素。它看起来像下面这样:
字符串callerNum1 = hash.get(mynumber的);如果(callerNum1.compareToIgnoreCase(searchBar.getText()。的toString())== 0){
...
}
I am trying to implement Searching in ListView
. My ListView
is comprised of HashMap
in ArrayList
and I managed to implement the logic but I guess there is much object and memory allocation in my approach each time the text change. Therefore I am looking for less memory allocated logic to search in my ListView
with HashMap
in ArrayList
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
//textlength = searchBar.getText().length();
//text_sort.clear();
sortedArrayList.clear();
for (int i = 0; i < myHistList.size(); i++) {
HashMap<String, String> hash = new HashMap<String, String>();
hash = myHistList.get(i);
if (hash.get("myName").toLowerCase().indexOf(searchBar.getText().toString().toLowerCase()) != -1) {
String callerNum1 = hash.get("myNumber");
String myName1 = hash.get("myName");
HashMap<String, String> searchedHash = new HashMap<String, String>();
// adding each child node to HashMap key => value
searchedHash.put("myNumber", callerNum1);
searchedHash.put("myName", myName1);
recordingFile1);
// adding HashList to ArrayList
sortedArrayList.add(searchedHash);
}
}
ListView actualLv = mPullRefreshListView.getRefreshableView();
actualLv.setAdapter(new myHistoryListAdapter(myHistory.this, sortedArrayList));
}
At first you can replace
HashMap<String, String> hash = new HashMap<String, String>();
hash = myHistList.get(i);
with just
HashMap<String, String> hash = myHistList.get(i);
It will slightly reduce the number of redundant objects.
At second step if you need to compare strings are the same but ignore letters' case you can try to simplify your if
condition
if (hash.get("myName").toLowerCase().indexOf(searchBar.getText().toString().toLowerCase()) != -1)
with
if(hash.get("myName").compareToIgnoreCase(searchBar.getText().toString()) == 0)
Also if you put the String callerNum1 = hash.get("myNumber");
above the if
statement then you can save some time because you don't need to look through your HashSet two times to search the same element. It will look like following:
String callerNum1 = hash.get("myNumber");
if(callerNum1.compareToIgnoreCase(searchBar.getText().toString()) == 0){
...
}
这篇关于是不是有更好的办法,从ArrayList中搜索的HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!