`
357029540
  • 浏览: 739821 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

RedisTemplate常用集合使用说明-opsForList(三)

阅读更多

         基础配置介绍已经在前面的《RedisTemplate常用集合使用说明(一)》中已经介绍了,现在我们直接介绍opsForList()方法的使用:

     1、leftPush(K key, V value)

 

  在变量左边添加元素值。

 

redisTemplate.opsForList().leftPush("list","a");
redisTemplate.opsForList().leftPush("list","b");
redisTemplate.opsForList().leftPush("list","c");

 

     2、index(K key, long index)

 

  获取集合指定位置的值。

 

String listValue = redisTemplate.opsForList().index("list",1) + "";
System.out.println("通过index(K key, long index)方法获取指定位置的值:" + listValue);

 

     3、range(K key, long start, long end)

 

获取指定区间的值。

 

List<Object> list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println("通过range(K key, long start, long end)方法获取指定范围的集合值:"+list);

    4、leftPush(K key, V pivot, V value)

 

     把最后一个参数值放到指定集合的第一个出现中间参数的前面,如果中间参数值存在的话。

 

redisTemplate.opsForList().leftPush("list","a","n");
list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println("通过leftPush(K key, V pivot, V value)方法把值放到指定参数值前面:" + list);

 

     5、leftPushAll(K key, V... values)

 

  向左边批量添加参数元素。

 

redisTemplate.opsForList().leftPushAll("list","w","x","y");
list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println("通过leftPushAll(K key, V... values)方法批量添加元素:" + list);

 

     6、leftPushAll(K key, Collection<V> values)

 

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

 

List newList = new ArrayList();
newList.add("o");
newList.add("p");
newList.add("q");
redisTemplate.opsForList().leftPushAll("list",newList);
list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println("通过leftPushAll(K key, Collection<V> values)方法以集合的方式批量添加元素:" + list);

 

     7、leftPushIfPresent(K key, V value)

 

  如果存在集合则添加元素。

 

redisTemplate.opsForList().leftPushIfPresent("presentList","o");
list =  redisTemplate.opsForList().range("presentList",0,-1);
System.out.println("通过leftPushIfPresent(K key, V value)方法向已存在的集合添加元素:" + list);

 

     8、rightPush(K key, V value)

 

   向集合最右边添加元素。

 

redisTemplate.opsForList().rightPush("list","w");
list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println("通过rightPush(K key, V value)方法向最右边添加元素:" + list);

 

      9、rightPush(K key, V pivot, V value)

 

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

 

redisTemplate.opsForList().rightPush("list","w","r");
list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println("通过rightPush(K key, V pivot, V value)方法向最右边添加元素:" + list);

 

     10、rightPushAll(K key, V... values)

 

   向右边批量添加元素。

 

redisTemplate.opsForList().rightPushAll("list","j","k");
list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println("通过rightPushAll(K key, V... values)方法向最右边批量添加元素:" + list);

 

    11、rightPushAll(K key, Collection<V> values)

 

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

 

newList.clear();
newList.add("g");
newList.add("h");
redisTemplate.opsForList().rightPushAll("list",newList);
list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println("通过rightPushAll(K key, Collection<V> values)方法向最右边以集合方式批量添加元素:" + list);

 

    12、rightPushIfPresent(K key, V value)

 

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

redisTemplate.opsForList().rightPushIfPresent("presentList","d");
list =  redisTemplate.opsForList().range("presentList",0,-1);
System.out.println("通过rightPushIfPresent(K key, V value)方法已存在的集合向最右边添加元素:" + list);

    13、size(K key)

 

获取集合长度。

 

long listLength = redisTemplate.opsForList().size("list");
System.out.println("通过size(K key)方法获取集合list的长度为:" + listLength);

     14、leftPop(K key)

 

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

 

Object popValue = redisTemplate.opsForList().leftPop("list");
System.out.print("通过leftPop(K key)方法移除的元素是:" + popValue);
list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println(",剩余的元素是:" + list);

 

     15、leftPop(K key, long timeout, TimeUnit unit)

 

   移除集合中左边的元素在等待的时间里,如果超过等待的时间仍没有元素则退出。

 

popValue = redisTemplate.opsForList().leftPop("presentList",1, TimeUnit.SECONDS);
System.out.print("通过leftPop(K key, long timeout, TimeUnit unit)方法移除的元素是:" + popValue);
list =  redisTemplate.opsForList().range("presentList",0,-1);
System.out.println(",剩余的元素是:" + list);

     16、rightPop(K key)

        移除集合中右边的元素。

       

popValue = redisTemplate.opsForList().rightPop("list");
System.out.print("通过rightPop(K key)方法移除的元素是:" + popValue);
list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println(",剩余的元素是:" + list);

 

      17rightPop(K key, long timeout, TimeUnit unit)

 

   移除集合中右边的元素在等待的时间里,如果超过等待的时间仍没有元素则退出。

 

popValue = redisTemplate.opsForList().rightPop("presentList",1, TimeUnit.SECONDS);
System.out.print("通过rightPop(K key, long timeout, TimeUnit unit)方法移除的元素是:" + popValue);
list =  redisTemplate.opsForList().range("presentList",0,-1);
System.out.println(",剩余的元素是:" + list);

     18rightPopAndLeftPush(K sourceKey, K destinationKey)

 

  移除集合中右边的元素,同时在左边加入一个元素。

popValue = redisTemplate.opsForList().rightPopAndLeftPush("list","12");
System.out.print("通过rightPopAndLeftPush(K sourceKey, K destinationKey)方法移除的元素是:" + popValue);
list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println(",剩余的元素是:" + list);

     19rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit)

  移除集合中右边的元素在等待的时间里,同时在左边添加元素,如果超过等待的时间仍没有元素则退出。

popValue = redisTemplate.opsForList().rightPopAndLeftPush("presentList","13",1,TimeUnit.SECONDS);
System.out.println("通过rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit)方法移除的元素是:" + popValue);
list =  redisTemplate.opsForList().range("presentList",0,-1);
System.out.print(",剩余的元素是:" + list);

  20set(K key, long index, V value)

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

redisTemplate.opsForList().set("presentList",3,"15");
list =  redisTemplate.opsForList().range("presentList",0,-1);
System.out.print("通过set(K key, long index, V value)方法在指定位置添加元素后:" + list);

  21remove(K key, long count, Object value)

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

long removeCount = redisTemplate.opsForList().remove("list",0,"w");
list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println("通过remove(K key, long count, Object value)方法移除元素数量:" + removeCount);
System.out.println(",剩余的元素:" + list);

      22trim(K key, long start, long end)

  截取集合元素长度,保留长度内的数据。

redisTemplate.opsForList().trim("list",0,5);
list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println("通过trim(K key, long start, long end)方法截取后剩余元素:" + list);

 

 

 

 

 

1
0
分享到:
评论

相关推荐

    spring整合redis以及使用RedisTemplate的方法

    `RedisTemplate`提供了丰富的API,包括`opsForHash()`, `opsForList()`, `opsForSet()` 和 `opsForZSet()` 等,分别对应Redis的哈希、列表、集合和有序集合操作。这使得在Spring应用中操作Redis变得非常方便。 总之...

    SpringDataRedis对Redis的数据类型的常用操作API的使用代码举例.zip

    这个压缩包文件“SpringDataRedis对Redis的数据类型的常用操作API的使用代码举例.zip”显然包含了关于如何利用Spring Data Redis进行Redis数据类型操作的示例代码。下面我们将深入探讨Spring Data Redis对Redis主要...

    spring集成redis源代码

    5. **RedisTemplate的常用方法** - `opsForValue()`:操作单个值,如设置、获取、增加、减少等。 - `opsForHash()`:操作哈希,支持添加、删除、获取键值对等。 - `opsForList()`:操作列表,进行插入、删除、...

    Java 语言在spring boot项目中针对 Redis数据库的各种操作工具类

    本篇文章将深入探讨如何在Spring Boot项目中集成Redis,并详细解析提供的`RedisService`和`RedisConfig`类以及`Redis工具类使用说明.txt`文档中的知识点。 首先,集成Redis到Spring Boot项目中,我们需要在`pom.xml...

    ssm框架集成redis

    例如,使用RedisTemplate的opsForHash()进行哈希操作,使用opsForList()处理列表数据,或者使用opsForSet()管理集合数据。同时,为了提高并发性能和数据一致性,可以引入Redlock或分布式锁策略。 在提供的压缩包...

    专高2_练习手册_高性能架构_第18单元1

    `opsForValue()`用于操作字符串类型数据,`opsForHash()`处理哈希类型,`opsForSet()`管理集合,而`opsForList()`则用于列表类型的数据操作。 5. 在Maven的pom.xml文件中,排除依赖的标签是`&lt;exclusions&gt;`,可以...

    SpringBoot整合redis及工具类

    使用`opsForList()`接口。 4. **集合操作**:存储不重复的元素,如`sAdd(key, value)`(添加)和`sMembers(key)`(获取所有成员)。通过`opsForSet()`方法实现。 5. **有序集合操作**:类似于集合,但每个元素都有...

    SpringBoot 如何集成 Redis ?

    3. **使用 RedisTemplate 或 StringRedisTemplate**: SpringBoot 会自动配置 RedisTemplate 和 StringRedisTemplate,你可以通过它们进行数据的存取操作。例如,存储字符串键值对: ```java @Autowired private ...

Global site tag (gtag.js) - Google Analytics