ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

opsforlist 存在贼覆盖_RedisTemplate常用集合使用说明-opsForList(三)

2022-10-26 13:02:24  阅读:156  来源: 互联网

标签:opsForList 覆盖 redisTemplate


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

1、leftPush(K key, V value)

在变量左边添加元素值。

Java代码

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

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

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

2、index(K key, long index)

获取集合指定位置的值。

Java代码

String listValue = redisTemplate.opsForList().index("list",1) + "";

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

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

获取指定区间的值。

Java代码

List 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)

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

Java代码

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)

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

Java代码

redisTemplate.opsForList().leftPushAll("list","w","x","y");

list = redisTemplate.opsForList().range("list",0,-1);

System.out.println("通过leftPushAll(K key, V... values)方法批量添加元素:" + list);

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

Java代码

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 values)方法以集合的方式批量添加元素:" + list);

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

Java代码

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)

向集合最右边添加元素。

Java代码

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)

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

Java代码

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)

向右边批量添加元素。

Java代码

redisTemplate.opsForList().rightPushAll("list","j","k");

list = redisTemplate.opsForList().range("list",0,-1);

System.out.println("通过rightPushAll(K key, V... values)方法向最右边批量添加元素:" + list);

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

Java代码

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 values)方法向最右边以集合方式批量添加元素:" + list);

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

Java代码

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)

获取集合长度。

Java代码

long listLength = redisTemplate.opsForList().size("list");

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

14、leftPop(K key)

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

Java代码

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)

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

Java代码

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);

移除集合中右边的元素。

Java代码

popValue = redisTemplate.opsForList().rightPop("list");

System.out.print("通过rightPop(K key)方法移除的元素是:" + popValue);

list = redisTemplate.opsForList().range("list",0,-1);

System.out.println(",剩余的元素是:" + list);

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

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

Java代码

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);

18、rightPopAndLeftPush(K sourceKey, K destinationKey)

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

Java代码

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);

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

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

Java代码

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);

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

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

Java代码

redisTemplate.opsForList().set("presentList",3,"15");

list = redisTemplate.opsForList().range("presentList",0,-1);

System.out.print("通过set(K key, long index, V value)方法在指定位置添加元素后:" + list);

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

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

Java代码

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);

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

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

Java代码

redisTemplate.opsForList().trim("list",0,5);

list = redisTemplate.opsForList().range("list",0,-1);

System.out.println("通过trim(K key, long start, long end)方法截取后剩余元素:" + list);

标签:opsForList,覆盖,redisTemplate
来源:

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有