Java 使用集合缓存数据

Java 使用集合缓存数据,java,algorithm,Java,Algorithm,因此,我在java集合中缓存数据时遇到了一个问题。当我第一次初始化应用程序时,我使用如下函数: cacheImageAndSounds(0,6); 现在,一旦我从一开始就到达第四个位置,我想从集合中删除前3个元素,并缓存下3个元素,即 cacheImageAndSounds(4,10); 但由于缓存中已经有第4、第5和第6个图像,我不想将它们重新带到缓存中,因为它们已经存在,因此我只想下载或获取第7到第10个图像和声音文件 我如何才能做到这一点,或者如何调整在地图中缓存数据的算法 这是一个函

因此,我在java集合中缓存数据时遇到了一个问题。当我第一次初始化应用程序时,我使用如下函数:

cacheImageAndSounds(0,6);
现在,一旦我从一开始就到达第四个位置,我想从集合中删除前3个元素,并缓存下3个元素,即

cacheImageAndSounds(4,10);
但由于缓存中已经有第4、第5和第6个图像,我不想将它们重新带到缓存中,因为它们已经存在,因此我只想下载或获取第7到第10个图像和声音文件

我如何才能做到这一点,或者如何调整在地图中缓存数据的算法

这是一个函数,我使用它在集合中创建图像和声音文件的缓存,并根据各种索引值进一步使用它从中检索数据。我使用它的方式是,我可以设置两个索引,并在集合中填充所需的数据

public int cacheImageAndSounds(int startIndex,int lastIndex)
    {
        for(int i=startIndex;i<lastIndex;i++)
        {   
        aq.ajax(data1.get(i), Bitmap.class, new AjaxCallback<Bitmap>() {
            @Override
            public void callback(String url, Bitmap object, AjaxStatus status) {
                imageFilexxS.put(url, object);
                System.out.println("size of imagefile"+imageFilexxS.size());
            }
        }); 

        aq.ajax(data1.get(i).replace(".png", ".mp3"), File.class, new AjaxCallback<File>() {
            @Override
            public void callback(String url, File object, AjaxStatus status) {
                imageFilexxSm.put(url, object);

                System.out.println("size of songfile"+imageFilexxSm.size());

                if(imageFilexxSm.size()>=6)
                {
                      update(); //call to the UI
                }
            }
        });
       }
      return 1;
    }

看起来在进行ajax调用之前,您没有缓存索引并检查它。设置一个名为
的新
。而这个方法呢,

public int cacheImageAndSounds(int startIndex,int lastIndex)
    {
        for(final int i=startIndex;i<lastIndex;i++)
        {   
            //check if the index is already processed, if not then make the call
            if(!processed.contains(i)) {
                aq.ajax(data1.get(i), Bitmap.class, new AjaxCallback<Bitmap>() {
                    @Override
                    public void callback(String url, Bitmap object, AjaxStatus status) {
                        imageFilexxS.put(url, object);
                        System.out.println("size of imagefile"+imageFilexxS.size());
                        processed.add(i); //once the result comes, mark the index as processed
                    }
                }); 

                aq.ajax(data1.get(i).replace(".png", ".mp3"), File.class, new AjaxCallback<File>() {
                    @Override
                    public void callback(String url, File object, AjaxStatus status) {
                        imageFilexxSm.put(url, object);
                        processed.add(i); //once the result comes, mark the index as processed
                        System.out.println("size of songfile"+imageFilexxSm.size());

                        if(imageFilexxSm.size()>=6)
                        {
                            update(); //call to the UI
                        }
                    }
                });
            }
        }
      return 1;
    }
public int-cacheImageAndSounds(int-startIndex,int-lastIndex)
{
对于(最终整数i=startIndex;i=6)
{
update();//对UI的调用
}
}
});
}
}
返回1;
}

这样,当您调用
cacheImageAndSounds(4,10)时
,对于第4、第5和第6个索引,不会进行ajax调用,因为这些索引已经存在于
已处理的
集中

,但我必须在到达特定索引后从已处理的
集中删除元素,并且我希望缓存下一个元素。例如,正如我在问题->中提到的,我在第4张图片上,我想从
地图
中删除以前的图片,这样地图将保留其中的内存。因此,我需要再次清除
集合
,我指的是前3个,然后让我们假设我回到第一个,然后我将明显清除最后三个,然后
集合
将获得新的位置。这应该是清楚的。。
public int cacheImageAndSounds(int startIndex,int lastIndex)
    {
        for(final int i=startIndex;i<lastIndex;i++)
        {   
            //check if the index is already processed, if not then make the call
            if(!processed.contains(i)) {
                aq.ajax(data1.get(i), Bitmap.class, new AjaxCallback<Bitmap>() {
                    @Override
                    public void callback(String url, Bitmap object, AjaxStatus status) {
                        imageFilexxS.put(url, object);
                        System.out.println("size of imagefile"+imageFilexxS.size());
                        processed.add(i); //once the result comes, mark the index as processed
                    }
                }); 

                aq.ajax(data1.get(i).replace(".png", ".mp3"), File.class, new AjaxCallback<File>() {
                    @Override
                    public void callback(String url, File object, AjaxStatus status) {
                        imageFilexxSm.put(url, object);
                        processed.add(i); //once the result comes, mark the index as processed
                        System.out.println("size of songfile"+imageFilexxSm.size());

                        if(imageFilexxSm.size()>=6)
                        {
                            update(); //call to the UI
                        }
                    }
                });
            }
        }
      return 1;
    }