The basic configuration has been introduced in the previous " RedisTemplate Common Set Usage Instructions (1) ", now we directly introduce the use of the opsForSet() method:
1、add(K key, V... values)
Bulk add values to variables.
redisTemplate.opsForSet().add("setValue","A","B","C","B","D","E","F");
2、members(K key)
Get the value in the variable.
Set set = redisTemplate.opsForSet().members("setValue"); System.out.println("Get the element value in the variable through the members(K key) method: " + set);
3、size(K key)
Get the length of the value in the variable.
long setLength = redisTemplate.opsForSet().size("setValue"); System.out.println("Get the length of the element value in the variable through the size(K key) method: " + setLength);
4、randomMember(K key)
Get elements in a variable at random.
Object randomMember = redisTemplate.opsForSet().randomMember("setValue"); System.out.println("Randomly get the elements in the variable through the randomMember(K key) method: " + randomMember);
5、randomMembers(K key, long count)
Gets a specified number of elements in a variable at random.
List randomMembers = redisTemplate.opsForSet().randomMembers("setValue",2); System.out.println("Randomly obtain the specified number of elements in the variable through the randomMembers(K key, long count) method: " + randomMembers);
6、isMember(K key, Object o)
Checks if the given element is in a variable.
boolean isMember = redisTemplate.opsForSet().isMember("setValue","A"); System.out.println("Check if the given element is in the variable by isMember(K key, Object o) method: " + isMember);
7、move(K key, V value, K destKey)
Transfers the element value of the variable to the destination variable.
boolean isMove = redisTemplate.opsForSet().move("setValue","A","destSetValue"); if(isMove){ set = redisTemplate.opsForSet().members("setValue"); System.out.print("Use the move(K key, V value, K destKey) method to transfer the element value of the variable to the remaining elements after the destination variable: " + set); set = redisTemplate.opsForSet().members("destSetValue"); System.out.println(", the element value in the destination variable: " + set); }
8、pop(K key)
Pops the element in the variable.
Object popValue = redisTemplate.opsForSet().pop("setValue"); System.out.print("Pop up the element in the variable by the pop(K key) method: " + popValue); set = redisTemplate.opsForSet().members("setValue"); System.out.println(", remaining elements: " + set)
9、remove(K key,
Object... values)
Batch remove elements from a variable.
long removeCount = redisTemplate.opsForSet().remove("setValue","E","F","G"); System.out.print("Remove the number of elements in the variable by the remove(K key, Object... values) method: " + removeCount); set = redisTemplate.opsForSet().members("setValue"); System.out.println(", remaining elements: " + set);
10、scan(K key,
ScanOptions options)
Match to obtain key-value pairs, ScanOptions.NONE is to obtain all key-value pairs; ScanOptions.scanOptions().match("C").build() matches to obtain the key-value pairs of key map1, which cannot be fuzzy matching.
//Cursor<Object> cursor = redisTemplate.opsForSet().scan("setValue", ScanOptions.NONE); Cursor<Object> cursor = redisTemplate.opsForSet().scan("setValue", ScanOptions.scanOptions().match("C").build()); while (cursor.hasNext()){ Object object = cursor.next(); System.out.println("Get the matching value through the scan(K key, ScanOptions options) method: " + object); }
11、difference(K key,
Collection<K> otherKeys)
Find the difference by set.
List list = new ArrayList(); list.add("destSetValue"); Set differenceSet = redisTemplate.opsForSet().difference("setValue",list); System.out.println("Get the value of the variable that is different from the variable in the given collection through the difference(K key, Collection<K> otherKeys) method: " + differenceSet);
12、difference(K key, K otherKey)
Find the difference between 2 set variables with the given key.
differenceSet = redisTemplate.opsForSet().difference("setValue","destSetValue"); System.out.println("Get the value in the variable that is different from the given variable through the difference(K key, Collection<K> otherKeys) method: " + differenceSet);
13、differenceAndStore(K key, K otherKey, K destKey)
Save the calculated difference element.
redisTemplate.opsForSet().differenceAndStore("setValue","destSetValue","storeSetValue"); set = redisTemplate.opsForSet().members("storeSetValue"); System.out.println("Save the obtained difference element by differenceAndStore(K key, K otherKey, K destKey) method: " + set);
14、differenceAndStore(K key,
Collection<K> otherKeys,
K destKey)
Save the calculated difference element.
redisTemplate.opsForSet().differenceAndStore("setValue",list,"storeSetValue"); set = redisTemplate.opsForSet().members("storeSetValue"); System.out.println("Save the obtained difference element by differenceAndStore(K key, Collection<K> otherKeys, K destKey) method: " + set);
15、distinctRandomMembers(K key, long count)
Get deduplicated random elements.
set = redisTemplate.opsForSet().distinctRandomMembers("setValue",2); System.out.println("Get the deduplicated random elements through the distinctRandomMembers(K key, long count) method:" + set);
16、intersect(K key, K otherKey)
Get the intersection in 2 variables.
set = redisTemplate.opsForSet().intersect("setValue","destSetValue"); System.out.println("Get the intersection element by intersect(K key, K otherKey) method: " + set);
17、intersect(K key,
Collection<K> otherKeys)
Get the intersection between multiple variables.
set = redisTemplate.opsForSet().intersect("setValue",list); System.out.println("Get the intersection element by intersect(K key, Collection<K> otherKeys) method: " + set);
18、intersectAndStore(K key,
K otherKey,
K destKey)
Get the intersection of 2 variables and save it to the last parameter.
redisTemplate.opsForSet().intersectAndStore("setValue","destSetValue","intersectValue"); set = redisTemplate.opsForSet().members("intersectValue"); System.out.println("Save the obtained intersection element by the method intersectAndStore(K key, K otherKey, K destKey):" + set);
19、intersectAndStore(K key, Collection<K> otherKeys, K destKey)
Get the intersection of multiple variables and save to the last parameter.
redisTemplate.opsForSet().intersectAndStore("setValue",list,"intersectListValue"); set = redisTemplate.opsForSet().members("intersectListValue"); System.out.println("Save the obtained intersection element by intersectAndStore(K key, Collection<K> otherKeys, K destKey) method: " + set);
20、union(K key,
K otherKey)
Get a collection of 2 variables.
set = redisTemplate.opsForSet().union("setValue","destSetValue"); System.out.println("Get the set elements of 2 variables through the union(K key, K otherKey) method: " + set);
21、union(K key, Collection<K> otherKeys)
Get a collection of multiple variables.
set = redisTemplate.opsForSet().union("setValue",list); System.out.println("Get the collection elements of multiple variables through the union(K key, Collection<K> otherKeys) method: " + set);
22 、unionAndStore(K key,
K otherKey,
K destKey)
After obtaining a collection of 2 variables, save it to the last parameter.
redisTemplate.opsForSet().unionAndStore("setValue","destSetValue","unionValue"); set = redisTemplate.opsForSet().members("unionValue"); System.out.println("Save the obtained intersection element by unionAndStore(K key, K otherKey, K destKey) method: " + set);
23、unionAndStore(K key, Collection<K> otherKeys, K destKey)
Get a collection of multiple variables and save to the last parameter.
redisTemplate.opsForSet().unionAndStore("setValue",list,"unionListValue"); set = redisTemplate.opsForSet().members("unionListValue"); System.out.println("Save the obtained intersection element by unionAndStore(K key, Collection<K> otherKeys, K destKey) method: " + set);