Java pojo - 迭代新数据集合后回收缓存的哈希表值

发布于 2024-10-16 05:27:48 字数 1188 浏览 2 评论 0原文

所以是的,我只是想看看是否有一种更优雅的方法来完成我下面要做的事情(请记住,我想要 POJO(普通旧 Java 对象)相关的答案,因为这个问题是J2ME 相关,因此没有泛型和现代数据结构,仅在 Java 1.5 及更高版本中找到):

假设我有一个对象 MyImage,它只是一个简单的 bean 对象,它填充了来自对我的服务器的网络调用的数据。它包含的只是与我的应用程序相关的所述图像的元数据,更重要的是,它包含一个唯一标识符,用于构造 URL,以便从我的服务器获取该对象的图像。当我向这些对象发出请求时,我经常会收到一组新的对象,其中一些与以前的请求相同。

现在,即使我能够下载图像,问题也出现在如何缓存图像数据,当我收到一组新的 MyImage 对象时,我将它们与我的缓存交叉引用,并且只保留图像该 MyImage 对象(如果已下载)。换句话说,当我将下载的图像保存到哈希表缓存时,我使用构造的 URL (MY_IMAGE_SERVER+myImageUniqueId) 来键入图像数据。当我获得一组新的 MyImage 对象时,当前我会执行以下操作:

 Hashtable imgs = getImages();

 //If we have cached images, we should see which ones to carry over.
 if(imgs.size() > 0){       
    Hashtable newImgs = new Hashtable();
    for(int i = 0; i < myImages.length; i++){
        MyImage mi = myImages[i];
        if(mi != null && mi.hasImage()){
            //Check if we have the MD5 URL
            if(imgs.containsKey(IMG_URL_PATH + mi.getUniqueId())){
                //Place in new hashtable
                newImgs.put(IMG_URL_PATH + mi.getUniqueId(), imgs.get(IMG_URL_PATH + mi.getUniqueId()));
            }
        }
    }
   _bannerImgs = newImgs;
 }

我想知道这听起来是否合法,或者是否可以以更好更有效的方式完成?

So yeah, I'm just trying to see if there is a more elegant way of doing what I'm about to do below (please keep in mind that I want POJO (Plain-Old Java Objects) related answers, since this issue is J2ME related, so no Generics and modern data structures only found in Java 1.5 and above):

Suppose I have an object, MyImage, which just a simple bean object that gets populated with data from a network call to my server. All it contains is metadata about said image related to my app, more importantly, it contains a unique identifier that is used to construct a URL in order to fetch the image from my server for that object. I receive a new set of those objects every so often when I make a request for them, some of which are the same as previous requests.

Now, even though I am able to download the image, the problem arises in how to cache the image data in a way that when I receive a new set of MyImage objects, I cross-reference them against my cache and only retain the image for that MyImage object if it was already downloaded. In other words, when I save my downloaded image to my Hashtable cache, I key the image data with the constructed URL (MY_IMAGE_SERVER+myImageUniqueId). When I get a new set of MyImage objects, currently I do the following:

 Hashtable imgs = getImages();

 //If we have cached images, we should see which ones to carry over.
 if(imgs.size() > 0){       
    Hashtable newImgs = new Hashtable();
    for(int i = 0; i < myImages.length; i++){
        MyImage mi = myImages[i];
        if(mi != null && mi.hasImage()){
            //Check if we have the MD5 URL
            if(imgs.containsKey(IMG_URL_PATH + mi.getUniqueId())){
                //Place in new hashtable
                newImgs.put(IMG_URL_PATH + mi.getUniqueId(), imgs.get(IMG_URL_PATH + mi.getUniqueId()));
            }
        }
    }
   _bannerImgs = newImgs;
 }

I am wondering if this sounds legit, or can it be done in a better more efficient way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独自唱情﹋歌 2024-10-23 05:27:48

后续

根据注释中代码的假定用途(如下),您的做法似乎是合理的,但您可以进行一些小的优化。将代码的相关部分更改为:

    // Check if we have the image in our cache
    String key = IMG_URL_PATH + mi.getUniqueId();
    Object image = imgs.get(key);
    if (image != null) {
        // Carry over to new cache
        newImgs.put(key, image);
    }

注意:

  1. 创建/使用局部变量可以避免创建密钥字符串 3 次。
  2. 使用 get 而不是 contains 消除了一次哈希表查找。

然而,这是否会对系统性能产生显着影响是值得怀疑的……除非 getUniqueId() 方法做了一些愚蠢的事情,比如每次调用它时计算 MD5 和。 (显然事实并非如此。)

尽管性能有所提高,但我还是会进行此更改,因为它使代码更易于阅读……IMO。

FOLLOWUP

Based on the assumed purpose of the code in the comment (below), the way you are doing it seems reasonable, but there are a couple of minor optimization you could make. Change the relevant part of the code to this:

    // Check if we have the image in our cache
    String key = IMG_URL_PATH + mi.getUniqueId();
    Object image = imgs.get(key);
    if (image != null) {
        // Carry over to new cache
        newImgs.put(key, image);
    }

Note:

  1. Creating / using a local variable avoids creating the key string 3 times.
  2. Using get instead of contains eliminates one hashtable lookup.

However, it is doubtful that this will make a significant difference to your system's performance ... unless the getUniqueId() method does something daft, like calculate an MD5 sum each time you call it. (And apparently it doesn't.)

Notwithstanding the performance, I'd make this change because it makes the code easier to read ... IMO.

澜川若宁 2024-10-23 05:27:48

如果您已经有一组包含要保留的图像的键,那么您可以简单地执行以下操作:

imgs.keySet().retainAll(stillValidKeys);

但是,如果您拥有的只是一个列表,那么您当前的代码可能就足够了。

if you already had a set containing the keys of images to retain, then you could simply do something like:

imgs.keySet().retainAll(stillValidKeys);

however, if all you have is a list, then your current code is probably sufficient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文