gpt4 book ai didi

java - 快速重复搜索快速增长的数组中是否存在值的最佳方法?

转载 作者:行者123 更新时间:2023-12-01 16:56:30 24 4
gpt4 key购买 nike

我有一个程序,可以从网站上查找数百个类别,并且从每个类别中许多产品的产品详细信息页面获取每个类别的数据

每个类别都是从包含大约 800 个类别的表中进行 SELECT 获取的,然后我从每个类别中获取大约 100 个产品。

问题是,其中许多产品属于多个类别,因此我尝试在代码中添加一种方法,如果我之前已经获取过该产品(在不同的类别中),则不会进入产品详细信息页面

所以代码从概念上讲是这样的:

thesql = "SELECT catid from categories";

ResultSet rs = statement.executeQuery(thesql);

while (rs.next())
{
> go check the website for this particular catid

> loop to check all products in this page
> for each productid listed in this category's page:
> check array to see if we have encountered this productid before(in
this session)
> if we have, skip this product and continue with next one
> if we haven't, go to this product's detail web page, grab its
data, insert it in database, and add this productid in our array.
}

我有 2 个问题:

1) 每次都必须将一个产品 id 添加到如此大的数组中,并且每次都必须搜索以查看当前产品 id 是否预先存在于数组中,是否会适得其反或过于占用资源?(数组最终可能会产生 2000-6000 件元素)

2)如果有不止一种方法可以实现,您推荐的方法是什么?

(请注意,尽管我理解这个概念,但我对 java 数组的经验很少)

我知道在插入数据库表时可以使用“INSERT IGNORE INTO...”以确保不会插入重复项,但我想节省检查我已有的产品网站所需的时间和资源已检查。

非常感谢!

编辑/更新:我忘了提及,productid 不是数字,而是 10 个字符的字符串,混合字母和数字。不确定这是否会产生很大的影响。

最佳答案

只需使用 HashSet<Id> 而不是数组:

This class offers constant time performance for the basic operations (add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets.

如果您的 Id 的话,10k 件根本不是问题实现不太密集hashcode/equals ,例如如果您的 ID 是 Long .

还请记住,如果您访问网页并执行数据库调用,则在 Java 代码中花费的时间很可能可以忽略不计:大部分时间将花在等待这些外部调用返回上。

关于java - 快速重复搜索快速增长的数组中是否存在值的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32099043/

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