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);
相关推荐
淡海水15 小时前
13-04-面试-源码级深度追问链
数据结构·unity·面试·c#·游戏引擎·源码·il2cpp
Logic10115 小时前
C语言/数据结构位运算题解:异或XOR找出时尚聚会中的“独特颜色“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
2401_8390805419 小时前
C++常见八股
数据结构·c++·算法
暮雨封夕19 小时前
跳表(Skip List)详解:原理、实现、使用场景与实际应用
数据结构
miller-tsunami20 小时前
排序的相关知识点
数据结构·排序算法
熊明才1 天前
WSL2 网络突然瘫痪(Network is unreachable / 没有 eth0)最短修复指南(2026年9月20日亲测可用)
linux·windows·wsl2
Logic1011 天前
C语言/数据结构滑动窗口题解:替换k个字符后的最长连续相同字符子串(LeetCode 424)
c语言·数据结构·字符串·滑动窗口·时间复杂度·频率统计·算法题
程序员阿鹏1 天前
简单介绍一下软引用
java·开发语言·jvm·数据结构·后端
爱奥尼欧1 天前
【C++】map、set 的底层是什么?红黑树五条性质、插入旋转到模拟实现
开发语言·数据结构·c++
xbzb1 天前
Windows 启动故障修复知识整理:蓝屏 / 无限重启 / 盘符 X / 无 PE 盘
windows