Instructions for using RedisTemplate common collections - opsForList (3)

         The basic configuration introduction has been introduced in the previous " RedisTemplate Common Collection Usage Instructions (1) ", now we directly introduce the use of the opsForList() method:

     1、leftPush(K key, V value)

 

  Add the element value to the left of the variable.

 

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

 

     2、index(K key, long index)

 

  Gets the value at the specified position in the collection.

 

String listValue = redisTemplate.opsForList().index("list",1) + "";
System.out.println("Get the value of the specified position through the index(K key, long index) method: " + listValue);

 

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

 

Get the value of the specified interval.

 

List<Object> list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println("Get the set value of the specified range through the range(K key, long start, long end) method: "+list);

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

 

     Puts the last parameter value before the first occurrence of the intermediate parameter in the specified set, if the intermediate parameter value exists.

 

redisTemplate.opsForList().leftPush("list","a","n");
list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println("Put the value in front of the specified parameter value by the leftPush(K key, V pivot, V value) method: " + list);

 

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

 

  Bulk add parameter elements to the left.

 

redisTemplate.opsForList().leftPushAll("list","w","x","y");
list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println("Add elements in batches via the leftPushAll(K key, V... values) method: " + list);

 

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

 

   Adds elements to the left in batches as a set.

 

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("By leftPushAll(K key, Collection<V> values) method batch add elements in a collection mode: " + list);

 

     7、leftPushIfPresent(K key, V value)

 

  Adds element if set exists.

 

redisTemplate.opsForList().leftPushIfPresent("presentList","o");
list =  redisTemplate.opsForList().range("presentList",0,-1);
System.out.println("Add an element to an existing set via the leftPushIfPresent(K key, V value) method: " + list);

 

     8、rightPush(K key, V value)

 

   Adds an element to the far right of the collection.

 

redisTemplate.opsForList().rightPush("list","w");
list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println("Add an element to the rightmost via the rightPush(K key, V value) method: " + list);

 

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

 

   Adds the element value of the third parameter variable to the right of the first occurrence of the second parameter variable element in the collection.

 

redisTemplate.opsForList().rightPush("list","w","r");
list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println("Add elements to the far right by the rightPush(K key, V pivot, V value) method: " + list);

 

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

 

   Bulk add elements to the right.

 

redisTemplate.opsForList().rightPushAll("list","j","k");
list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println("Add elements to the rightmost batch by rightPushAll(K key, V... values) method: " + list);

 

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

 

   Adds elements to the right as a set.

 

newList.clear();
newList.add("g");
newList.add("h");
redisTemplate.opsForList().rightPushAll("list",newList);
list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println("By rightPushAll(K key, Collection<V> values) method, add elements to the rightmost in batches in a collection mode: " + list);

 

    12、rightPushIfPresent(K key, V value)

 

  Add an element to an existing collection.

redisTemplate.opsForList().rightPushIfPresent("presentList","d");
list =  redisTemplate.opsForList().range("presentList",0,-1);
System.out.println("Add an element to the far right of the existing set by the rightPushIfPresent(K key, V value) method: " + list);

    13、size(K key)

 

Get the collection length.

 

long listLength = redisTemplate.opsForList().size("list");
System.out.println("The length of the set list obtained by the size(K key) method is: " + listLength);

     14、leftPop(K key)

 

       Removes the first element from the left in the collection.

 

Object popValue = redisTemplate.opsForList().leftPop("list");
System.out.print("The element removed by the leftPop(K key) method is: " + popValue);
list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println(", the remaining elements are: " + list);

 

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

 

   Remove the left element in the collection during the waiting time, if there is still no element after the waiting time, exit.

 

popValue = redisTemplate.opsForList().leftPop("presentList",1, TimeUnit.SECONDS);
System.out.print("The element removed by the leftPop(K key, long timeout, TimeUnit unit) method is: " + popValue);
list =  redisTemplate.opsForList().range("presentList",0,-1);
System.out.println(", the remaining elements are: " + list);

     16、rightPop(K key)

        Removes the right element from the collection.

       

popValue = redisTemplate.opsForList().rightPop("list");
System.out.print("The element removed by the rightPop(K key) method is: " + popValue);
list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println(", the remaining elements are: " + list);

 

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

 

   Remove the element on the right from the set during the waiting time, and exit if there is still no element after the waiting time.

 

popValue = redisTemplate.opsForList().rightPop("presentList",1, TimeUnit.SECONDS);
System.out.print("The element removed by the rightPop(K key, long timeout, TimeUnit unit) method is: " + popValue);
list =  redisTemplate.opsForList().range("presentList",0,-1);
System.out.println(", the remaining elements are: " + list);

     18rightPopAndLeftPush(K sourceKey, K destinationKey)

 

  Removes the right element from the collection while adding an element to the left.

popValue = redisTemplate.opsForList().rightPopAndLeftPush("list","12");
System.out.print("The element removed by the rightPopAndLeftPush(K sourceKey, K destinationKey) method is: " + popValue);
list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println(", the remaining elements are: " + list);

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

  Remove the element on the right in the set while waiting, and add elements on the left at the same time, and exit if there is still no element after the waiting time.

popValue = redisTemplate.opsForList().rightPopAndLeftPush("presentList","13",1,TimeUnit.SECONDS);
System.out.println("The element removed by the rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit) method is: " + popValue);
list =  redisTemplate.opsForList().range("presentList",0,-1);
System.out.print(", the remaining elements are: " + list);

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

  Insert an element at the specified position of the collection . If there is an element at the specified position, it will be overwritten. If there is no element, it will be added. If it exceeds the collection subscript +n , an error will be reported.

redisTemplate.opsForList().set("presentList",3,"15");
list =  redisTemplate.opsForList().range("presentList",0,-1);
System.out.print("After adding the element at the specified position through the set(K key, long index, V value) method: " + list);

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

  Removes the first count event of an element equal to value from the list stored in key. count> 0 : delete the first element equal to the value shifted from left to right; count< 0 : delete the first element equal to the value shifted from right to left; count = 0 : delete all elements equal to value .

long removeCount = redisTemplate.opsForList().remove("list",0,"w");
list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println("Remove the number of elements through the remove(K key, long count, Object value) method: " + removeCount);
System.out.println(", remaining elements: " + list);

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

  Intercept the length of the collection element, and keep the data within the length.

redisTemplate.opsForList().trim("list",0,5);
list =  redisTemplate.opsForList().range("list",0,-1);
System.out.println("Remaining elements after interception by trim(K key, long start, long end) method: " + list);

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327060301&siteId=291194637