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);
相关推荐
努力努力再努力wz3 分钟前
【C++高阶系列】外存查找的极致艺术:数据库偏爱的B+树底层架构剖析与C++完整实现!(附B+树实现的源码)
linux·运维·服务器·数据结构·数据库·c++·b树
北顾笙9807 分钟前
day22-数据结构力扣
数据结构·算法·leetcode
Yiyi_Coding7 分钟前
在Windows系统安装Docker
windows·docker·容器
全球通史7 分钟前
Windows + WSL2 + RTX 2080 Ti 搭建 RDK X5 OpenExplorer GPU 量化环境实战
windows
她说彩礼65万14 分钟前
C语言 指针运算
c语言·数据结构·算法
2301_764441339 小时前
LISA时空跃迁分析,地理时空分析
数据结构·python·算法
韭菜钟10 小时前
WIndows下一键切换网卡IP脚本
windows·网络协议·tcp/ip
玉树临风ives10 小时前
atcoder ABC 452 题解
数据结构·算法
Dontla10 小时前
go语言Windows安装教程(安装go安装Golang安装)(GOPATH、Go Modules)
开发语言·windows·golang
无敌昊哥战神11 小时前
深入理解 C 语言:巧妙利用“0地址”手写 offsetof 宏与内存对齐机制
c语言·数据结构·算法