【问题标题】:android what is binary search or btree collection [closed]android什么是二分搜索或btree集合[关闭]
【发布时间】:2014-01-06 12:00:18
【问题描述】:

什么是实现二分查找或B树的集合类。

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

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

【问题讨论】:

  • 参见 Arrays.binarySearch
  • 我需要这个问题的答案

标签: android search


【解决方案1】:
Collections.binarySearch(myList, searchingItem, comparator)

但在调用它之前,您必须使用以下命令对列表进行排序:

Collections.sort(myList, comparator);

【讨论】:

  • 排序又是昂贵的...... B-tree会更好或者维护列表排序的东西
  • @Vijay 这是有问题的陈述。添加到列表更便宜。因此,如果您添加很多然后只查询,那么它可能对您更好。
【解决方案2】:

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

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

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

但它没有实现List接口

【讨论】:

    猜你喜欢
    • 2010-09-19
    • 2013-09-28
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 2015-09-04
    • 1970-01-01
    相关资源
    最近更新 更多