RedisTemplate 使用之List

java 复制代码
 @GetMapping("/listTest")
    public void listTest(){
        // 添加元素
        redisTemplate.opsForList().leftPush("myList", "element1");
        redisTemplate.opsForList().leftPush("myList", "element2");
        redisTemplate.opsForList().leftPush("myList", "element3");
        // 获取列表
        List<Object> list = redisTemplate.opsForList().range("myList", 0, -1);
        for (Object element : list) {
            System.out.println(element);
        }
    }

获取集合指定位置的值

java 复制代码
 Object o = redisTemplate.opsForList().index("myList", 0);

获取指定区间的值

java 复制代码
        List<Object> myList = redisTemplate.opsForList().range("myList", 0, -1);

把element4放在element3之前

java 复制代码
redisTemplate.opsForList().leftPush("myList", "element3","element4");

向左边批量添加参数元素

java 复制代码
redisTemplate.opsForList().leftPushAll("myList", "element5","element4");

以集合的方式向左边批量添加元素

java 复制代码
ArrayList<String> list = new ArrayList<>();

        list.add("element1");
        list.add("element2");
        list.add("element3");

        redisTemplate.opsForList().leftPushAll("myList", list);

如果存在集合则添加元素

java 复制代码
 		redisTemplate.opsForList().leftPush("myList","a");
        redisTemplate.opsForList().leftPush("myList","b");
        redisTemplate.opsForList().leftPush("myList","c");
        redisTemplate.opsForList().leftPushIfPresent("myList","d");

向集合最右边添加元素,顶是左

java 复制代码
redisTemplate.opsForList().rightPush("myList","a");
        redisTemplate.opsForList().rightPush("myList","b");
        redisTemplate.opsForList().rightPush("myList","c");

        List<Object> myList = redisTemplate.opsForList().range("myList", 0, -1);

向集合中第一次出现第二个参数变量元素的右边添加第三个参数变量的元素值

java 复制代码
redisTemplate.opsForList().rightPush("myList","a");
        redisTemplate.opsForList().rightPush("myList","b");
        redisTemplate.opsForList().rightPush("myList","c");

        redisTemplate.opsForList().rightPush("myList","c","d");

向右边批量添加元素

java 复制代码
redisTemplate.opsForList().rightPushAll("myList", "a", "b", "c");

以集合方式向右边添加元素

java 复制代码
ArrayList<Object> objects = new ArrayList<>();
        objects.add("a");
        objects.add("b");
        objects.add("c");
        
        redisTemplate.opsForList().rightPushAll("myList", objects);

向已存在的集合中添加元素

java 复制代码
redisTemplate.opsForList().rightPush("myList", "a");
        redisTemplate.opsForList().rightPush("myList", "b");
        redisTemplate.opsForList().rightPush("myList", "c");

        redisTemplate.opsForList().rightPushIfPresent("myList", "d");

获取集合长度

java 复制代码
redisTemplate.opsForList().rightPush("myList", "a");
        redisTemplate.opsForList().rightPush("myList", "b");
        redisTemplate.opsForList().rightPush("myList", "c");

        Long length = redisTemplate.opsForList().size("myList");

移除集合中的左边第一个元素

java 复制代码
redisTemplate.opsForList().leftPush("myList", "a");
        redisTemplate.opsForList().leftPush("myList", "b");
        redisTemplate.opsForList().leftPush("myList", "c");

        Object leftPop = redisTemplate.opsForList().leftPop("myList");
        System.out.println("leftPop:"+leftPop);

移除集合中右边的元素

java 复制代码
redisTemplate.opsForList().leftPush("myList", "a");
        redisTemplate.opsForList().leftPush("myList", "b");
        redisTemplate.opsForList().leftPush("myList", "c");

        Object rightPop = redisTemplate.opsForList().rightPop("myList");
        System.out.println("rightPop:"+rightPop);

在集合的指定位置插入元素,如果指定位置已有元素,则覆盖,没有则新增,超过集合下标+n则会报错。

java 复制代码
redisTemplate.opsForList().leftPush("myList", "a");
        redisTemplate.opsForList().leftPush("myList", "b");
        redisTemplate.opsForList().leftPush("myList", "c");

        redisTemplate.opsForList().set("myList", 1, "d");

从存储在键中的列表中删除等于值的元素的第一个计数事件。count> 0:删除等于从左到右移动的值的第一个元素;count< 0:删除等于从右到左移动的值的第一个元素;count = 0:删除等于value的所有元素

java 复制代码
//leftPush顺序是c->b->a,下往上
//rightPush顺序是a->b->c,上往下
redisTemplate.opsForList().leftPush("myList", "a");
        redisTemplate.opsForList().leftPush("myList", "b");
        redisTemplate.opsForList().leftPush("myList", "c");

        redisTemplate.opsForList().remove("myList", 1, "a");

修剪列表,只保留索引 0 到 1 的元素

java 复制代码
redisTemplate.opsForList().leftPush("myList", "a");
        redisTemplate.opsForList().leftPush("myList", "b");
        redisTemplate.opsForList().leftPush("myList", "c");

        redisTemplate.opsForList().trim("myList", 0, 1);
相关推荐
Darling噜啦啦11 小时前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
qq_369224331 天前
Windows全系通用!ntdll.dll文件丢失、报错、闪退问题的完整排查与修复教程
windows·dll·dll修复·dll丢失·dll错误
小小工匠1 天前
Redis - 事务机制:能实现 ACID 属性吗
数据结构·redis·性能优化·并发·持久化
玖玥拾1 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
Qres8212 天前
算法复键——树状数组
数据结构·算法
阿米亚波2 天前
【Windows】QEMU 启动 openEuler aarch64/arm64 架构系统 + 离线软件源
linux·windows·经验分享·笔记·架构·arm
caimouse2 天前
Reactos 第 10 章 网络操作 — 10.3.1 NIC驱动
网络·windows
牛油果子哥q2 天前
并查集(DSU)超精讲,路径压缩、按秩合并、万能模板、连通性判定、最小生成树与刷题实战全解
数据结构·c++·最小生成树·并查集
初圣魔门首席弟子2 天前
Node.js 详细介绍(知识库版)
windows·qt·node.js·知识库
凌波粒2 天前
LeetCode--491.递增子序列(回溯算法)
数据结构·算法·leetcode