gpt4 book ai didi

java - 自定义列表上的二进制搜索 - android

转载 作者:太空宇宙 更新时间:2023-11-04 11:17:24 25 4
gpt4 key购买 nike

在我的adapterrecyclerView我比较两个列表,如下所示:

for (int i = 0; i < existImages.size(); i++) {
if (jsonInstagramList.get(position).getId().equalsIgnoreCase(existImages.get(i).getImageID())) {
holder.insertedProduct.setVisibility(View.VISIBLE);
break;
} else {
holder.insertedProduct.setVisibility(View.GONE);
}
}

existImagesprivate List<ExistImage> existImages;jsonInstagramListprivate List<Node> jsonInstagramList; .

如何使用binary search为了加快搜索速度或其他方式?

最佳答案

根据您的上下文,这可能非常简单。

Collections 类中已经实现了二分搜索(链接到 javadoc )。所以你只需调用该方法即可。

当然,请记住:

Searches the specified list for the specified object using the binary search algorithm. The list must be sorted into ascending order according to the natural ordering of its elements...

这意味着您的列表应该已经排序。因此,真正的答案是首先查看当前的循环:

jsonInstagramList.get(position).getId().equalsIgnoreCase(existImages.get(i).getImageID()

发现你根本无法将其转变为二分搜索。

换句话说:如果此查找元素的代码被频繁调用,以至于需要使用二分搜索 - 那么您必须提前准备数据。

含义:二分查找在相同类型元素的排序列表中查找。因此,您首先必须创建一个包含您要搜索的内容的列表!

关于java - 自定义列表上的二进制搜索 - android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45304203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com