如何提高 arraylist 的获取速度?
How do I increase the fetching speed of an arraylist?
我正在使用 Arraylist
来获取我的应用程序中的所有可用联系人。这效率不高,因为 Arraylist
需要很长时间来获取和填充 Listview
,因为几乎有 600+ contacts
.
我正在寻找一种性能更好的替代方法。
虽然我搜索了其他相关问题,但找不到方便的问题。
这是我的 java 代码:
private List<String> getContactList() {
List<String> stringList=new ArrayList<>();
ContentResolver cr = context.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME)
);
if (cur.getInt(cur.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null
);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.v("Data : ",""+id+" "+name+" "+phoneNo);
stringList.add(id);
stringList.add(name);
stringList.add(phoneNo);
}
pCur.close();
}
}
}
if(cur!=null){
cur.close();
}
return stringList;
}
您可以考虑使用 Paging
库:https://developer.android.com/topic/libraries/architecture/paging/
它的设计理念是列表仅显示一定数量的项目,因此实际上没有必要加载比它可能显示的更多的方式。例如,一个 ListView 可能只显示 10 个联系人,因此无需获取 600 个联系人。
相反,Paging 库将在用户滚动时获取更小的数量,从而消除 600 个联系人的加载时间、600 个联系人的内存等...从而提高效率。
如果您担心速度,我会尝试使用 Set,尽管 ArrayList 中有 600 多个联系人应该不是问题。当数据集达到数百万甚至更多时,这就成了一个问题。我会尝试查看您的代码中的任何其他低效之处。
就Set而言,最常见的两种Java数据结构是HashSet和TreeSet。 TreeSet 如果你想对集合进行排序。 HashSet 稍微快一点,但是你失去了排序。两者都有 O(1) 访问时间。
您的查询效率低下,您目前正在对每个联系人进行查询,这非常慢,您可以通过一个大查询(非常快)获得所有信息:
String[] projection = new String[] { Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER };
Cursor c = cr.query(Phone.CONTENT_URI, projection, null, null, null);
while (c.moveToNext()) {
long contactId = c.getLong(0);
String name = c.getString(1);
String phone = c.getString(2);
Log.i("Phones", "got contact phone: " + contactId + " - " + name + " - " + phone);
}
c.close();
我正在使用 Arraylist
来获取我的应用程序中的所有可用联系人。这效率不高,因为 Arraylist
需要很长时间来获取和填充 Listview
,因为几乎有 600+ contacts
.
我正在寻找一种性能更好的替代方法。
虽然我搜索了其他相关问题,但找不到方便的问题。
这是我的 java 代码:
private List<String> getContactList() {
List<String> stringList=new ArrayList<>();
ContentResolver cr = context.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME)
);
if (cur.getInt(cur.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null
);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.v("Data : ",""+id+" "+name+" "+phoneNo);
stringList.add(id);
stringList.add(name);
stringList.add(phoneNo);
}
pCur.close();
}
}
}
if(cur!=null){
cur.close();
}
return stringList;
}
您可以考虑使用 Paging
库:https://developer.android.com/topic/libraries/architecture/paging/
它的设计理念是列表仅显示一定数量的项目,因此实际上没有必要加载比它可能显示的更多的方式。例如,一个 ListView 可能只显示 10 个联系人,因此无需获取 600 个联系人。
相反,Paging 库将在用户滚动时获取更小的数量,从而消除 600 个联系人的加载时间、600 个联系人的内存等...从而提高效率。
如果您担心速度,我会尝试使用 Set,尽管 ArrayList 中有 600 多个联系人应该不是问题。当数据集达到数百万甚至更多时,这就成了一个问题。我会尝试查看您的代码中的任何其他低效之处。
就Set而言,最常见的两种Java数据结构是HashSet和TreeSet。 TreeSet 如果你想对集合进行排序。 HashSet 稍微快一点,但是你失去了排序。两者都有 O(1) 访问时间。
您的查询效率低下,您目前正在对每个联系人进行查询,这非常慢,您可以通过一个大查询(非常快)获得所有信息:
String[] projection = new String[] { Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER };
Cursor c = cr.query(Phone.CONTENT_URI, projection, null, null, null);
while (c.moveToNext()) {
long contactId = c.getLong(0);
String name = c.getString(1);
String phone = c.getString(2);
Log.i("Phones", "got contact phone: " + contactId + " - " + name + " - " + phone);
}
c.close();