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);
相关推荐
小poop40 分钟前
轮转数组:从暴力到最优,一题掌握算法复杂度分析
数据结构·算法·leetcode
西安景驰电子3 小时前
PCIe 授时卡原理及应用
运维·服务器·windows·单片机·嵌入式硬件·fpga开发
wabs6664 小时前
关于图论【卡码网104.建造最大岛屿的思考】
数据结构·算法·图论
冻柠檬飞冰走茶4 小时前
PTA基础编程题目集 7-15 计算圆周率(C语言实现)
c语言·开发语言·数据结构·算法
wardenlzr5 小时前
VSCode 运行 Live Server 提示「Windows 找不到文件 Chrome」问题全解析
chrome·windows·vscode
好好沉淀6 小时前
Windows 下升级 Maven 3.6.1 到 3.9.9 踩坑全记录
java·windows·maven
weixin_436525076 小时前
Windows 上终止占用的端口进程
windows
古道青阳7 小时前
Duilib 技术全景剖析
c++·windows
jing.wang_20258 小时前
NVIDIA CUDA C++编程环境搭建--Windows + Ubuntu 22.04
c++·windows·ubuntu·gpu算力
冻柠檬飞冰走茶8 小时前
PTA基础编程题目集 7-13 日K蜡烛图(C语言实现)
c语言·开发语言·数据结构·算法