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);
相关推荐
小狮子&6 分钟前
ubuntu2604终端右键新建窗口
windows
变量未定义~1 小时前
单调栈、单调队列(模板)、子矩阵(模板)
数据结构·算法·蓝桥杯
不会就选b1 小时前
算法日常・每日刷题--<快速排序>2
数据结构·算法
shx_wei1 小时前
从函数调用到进程替换:使用 execl、CMake 与 PowerShell 构建一个多可执行程序
linux·c语言·windows·c
奶油话梅糖1 小时前
锐捷常用命令速查表
java·网络·windows
love530love2 小时前
CodexPro + MCP + Cloudflare 配置详解:HTTP2、QUIC 与连接排障
ide·人工智能·windows·ai agent
kyle~2 小时前
Schema---数据结构的形式化规则描述
数据结构·windows·microsoft
艾德金的溪3 小时前
【windows安装使用openclaw】
运维·windows·ai
love530love3 小时前
GPT + Codex CLI + CodexPro 三位一体:AI 编程协作工作流
人工智能·windows·gpt·chatgpt·devops·mcp
李小小钦3 小时前
D. Storming Arasaka(Codeforces 2238)
c语言·开发语言·数据结构·c++·算法