分段刷新追加缓存实现

最近项目中有一个需求, 要对原有的缓存列表做分段刷新操作.

具体需求如下
oldCache : "... , 00, 10, 20, 30, 40" //原有数据缓存
newCache: "00, 10, 20, 30, 40, 50, 60" //新刷新的数据片段
要求新数据片段更新到原先的缓存中. 并且更新最后相等的元素.
结果:
"... , 00, 10, 20, 30, 40, 50, 60"
其中的40, 50, 60元素来自新刷新的数据.

实现如下:

    public static void main(String args[]){
        List<String> oldList = new ArrayList<>();
        //oldList.add("06_old_0");
        oldList.add("00_old_0");
        oldList.add("10_old_1");
        oldList.add("20_old_2");
        oldList.add("30_old_3");
        oldList.add("40_old_4");
        oldList.add("41_old_4");

        List<String> newList = new ArrayList<>();
        newList.add("05_new_0");
        newList.add("10_new_1");
        newList.add("20_new_2");
        newList.add("30_new_3");
        newList.add("40_new_4");
        newList.add("42_new_4");
        newList.add("50_new_5");
        //newList.add("60_new_5");
        //newList.add("70_new_5");

        int oldLastIndex = oldList.size() - 1;
        int startRefreshIndex = newList.size() - 1;
        String oldLastDateStr = oldLastIndex < 0? "-1": oldList.get(oldLastIndex).substring(0,2);
        for(int i=startRefreshIndex; i >= 0; i--){
            int ni = Integer.parseInt(newList.get(i).substring(0,2));
            int oi = Integer.parseInt(oldLastDateStr);
            int ret = ni - oi;
            if(ret > 0){
                startRefreshIndex = i - 1;
            } else if( ret == 0){
                oldList.set(oldLastIndex, newList.get(startRefreshIndex)+">>");
                startRefreshIndex++;
                break;
            } else{
                startRefreshIndex++;
                break;
            }
        }

        startRefreshIndex = Math.max(0, startRefreshIndex);
        for(; startRefreshIndex<newList.size(); startRefreshIndex++){
            oldList.add(newList.get(startRefreshIndex));
        }

        System.out.println("oldList = [" + oldList + "]");
    }

运行结果:

oldList = [[00_old_0, 10_old_1, 20_old_2, 30_old_3, 40_old_4, 41_old_4, 42_new_4, 50_new_5]]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 12,934评论 6 13
  • 最近天气很热,电脑更热,呼啦呼啦响,差不多都可以煎鸡蛋了。看视频也是卡的不行不行,QQ进程被自动杀死,CPU分分钟...
    黑小胡子阅读 8,602评论 1 0
  • 世界上没有完美的人、事、物,头脑里的词语联想一语而出;平常做事也求完美,可总有一点不遂人愿。每每想起,心里总会升起...
    凤凰仙女阅读 264评论 0 1
  • 今天手痒,把用户的权限提升到root级别,然后,重启的时候,就丢失了自己的账户,只能登录guest,经过一番寻找,...
    MORESIR阅读 5,531评论 0 0
  • 由外而内所体会的快乐; 是一种心情; 由内而外所感受的快乐; 是一种心境!!
    黑仕阅读 349评论 2 2

友情链接更多精彩内容