redisTemplate一opsForList操作 |
您所在的位置:网站首页 › list删除全部元素 › redisTemplate一opsForList操作 |
1、leftPush(Kkey,Vvalue) 在变量左边添加元素值。 redisTemplate.opsForList().leftPush("list","a"); redisTemplate.opsForList().leftPush("list","b"); redisTemplate.opsForList().leftPush("list","c"); 2、index(Kkey, long index) 获取集合指定位置的值。 String listValue = redisTemplate.opsForList().index("list",1) +""; System.out.println("通过index(K key, long index)方法获取指定位置的值:"+ listValue); 3、range(Kkey, long start, long end) 获取指定区间的值。 List list = redisTemplate.opsForList().range("list",0,-1); System.out.println("通过range(K key, long start, long end)方法获取指定范围的集合值:"+list); 4、leftPush(Kkey,Vpivot,Vvalue) 把最后一个参数值放到指定集合的第一个出现中间参数的前面,如果中间参数值存在的话。 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(Kkey,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(Kkey,Collection values) 以集合的方式向左边批量添加元素。 List newList =newArrayList(); 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); 7、leftPushIfPresent(Kkey,Vvalue) 如果存在集合则添加元素。 redisTemplate.opsForList().leftPushIfPresent("presentList","o"); list = redisTemplate.opsForList().range("presentList",0,-1); System.out.println("通过leftPushIfPresent(K key, V value)方法向已存在的集合添加元素:"+ list); 8、rightPush(Kkey,Vvalue) 向集合最右边添加元素。 redisTemplate.opsForList().rightPush("list","w"); list = redisTemplate.opsForList().range("list",0,-1); System.out.println("通过rightPush(K key, V value)方法向最右边添加元素:"+ list); 9、rightPush(Kkey,Vpivot,Vvalue) 向集合中第一次出现第二个参数变量元素的右边添加第三个参数变量的元素值。 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(Kkey,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(Kkey,Collection 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 values)方法向最右边以集合方式批量添加元素:"+ list); 12、rightPushIfPresent(Kkey,Vvalue) 向已存在的集合中添加元素。 redisTemplate.opsForList().rightPushIfPresent("presentList","d"); list = redisTemplate.opsForList().range("presentList",0,-1); System.out.println("通过rightPushIfPresent(K key, V value)方法已存在的集合向最右边添加元素:"+ list); 13、size(Kkey) 获取集合长度。 longlistLength = redisTemplate.opsForList().size("list"); System.out.println("通过size(K key)方法获取集合list的长度为:"+ listLength); 14、leftPop(Kkey) 移除集合中的左边第一个元素。 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(Kkey, long timeout,TimeUnitunit) 移除集合中左边的元素在等待的时间里,如果超过等待的时间仍没有元素则退出。 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(Kkey) 移除集合中右边的元素。 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(Kkey, long timeout,TimeUnitunit) 移除集合中右边的元素在等待的时间里,如果超过等待的时间仍没有元素则退出。 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(KsourceKey,KdestinationKey) 移除集合中右边的元素,同时在左边加入一个元素。 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(KsourceKey,KdestinationKey, long timeout,TimeUnitunit) 移除集合中右边的元素在等待的时间里,同时在左边添加元素,如果超过等待的时间仍没有元素则退出。 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(Kkey, long index,Vvalue) 在集合的指定位置插入元素,如果指定位置已有元素,则覆盖,没有则新增,超过集合下标+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); 21、remove(Kkey, long count,Objectvalue) 从存储在键中的列表中删除等于值的元素的第一个计数事件。count> 0:删除等于从左到右移动的值的第一个元素;count< 0:删除等于从右到左移动的值的第一个元素;count = 0:删除等于value的所有元素。 longremoveCount = 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(Kkey, 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); |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |