android什么是二进制搜索或btree集合

时间:2014-01-06 12:00:19

标签: android search

实现二进制搜索或B树的集合类是什么。

我目前正在使用List<Item> allItemList发现任何元素都非常慢,因为我必须从头到尾开始并比较每个项目。

我想知道是否有任何课程可以做到这一点?

2 个答案:

答案 0 :(得分:1)

如果你的allItemList是ArrayList,那么你可以对它进行排序,然后用户Collections.binarySearch使用二进制搜索来查找元素。

您也可以使用TreeSet,从您可以阅读的文档:

This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains).

但它没有实现List接口

答案 1 :(得分:1)

Collections.binarySearch(myList, searchingItem, comparator)

但在你打电话之前,你必须用以下方式对列表进行排序:

Collections.sort(myList, comparator);