The basic configuration has been introduced in the previous " RedisTemplate Common Set Usage Instructions (1) ", now we directly introduce the use of the boundSetOps() method:
First define a BoundSetOperations
BoundSetOperations boundSetOperations = redisTemplate.boundSetOps("bso");
Bulk add values, get all values
// view all values after adding new values boundSetOperations.add("a","b","c"); boundSetOperations.members().forEach(v -> System.out.println("View all values after adding a new value: " + v));
2.scan(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<String> cursor = boundSetOperations.scan(ScanOptions.NONE); while (cursor.hasNext()){ System.out.println("Iterate over all values:" + cursor.next()); }
3.randomMember()
get a random value
System.out.println("Get a random value:" + boundSetOperations.randomMember());
4.randomMembers(long count)
Get a specified number of values at random
System.out.println("Randomly get the specified number of values:" + boundSetOperations.randomMembers(2));
5.distinctRandomMembers(long count)
Get a unique random number value
System.out.println("Get the unique random number value: " + boundSetOperations.distinctRandomMembers(2));
6.diff(Collection<K> keys)
Compare different values in multiple specific keys
//The comparison here should also be a specific set name, and the name cannot exist in the set set that has already been compared, otherwise an error will be reported Set list = new HashSet<>(); list.add("bso1"); boundSetOperations.diff(list).forEach(v -> System.out.println("Compare different elements of sets in the given set: " + v));
Compare different values in 2 specific keys
boundSetOperations.diff("bso2").forEach(v -> System.out.println("Compare different elements of the given set: " + v));
8.diffAndStore(Collection<K> keys, K destKey)和diffAndStore(K keys, K destKey)
Compare different values in keys and store
// Compare different sets and store boundSetOperations.diffAndStore("bso2","bso3"); redisTemplate.opsForSet().members("bso3").forEach(v -> System.out.println("Compare different sets and store: " + v));
9.intersect(Collection<K> keys)和intersect(K key)
Compare same values in keys
// Compare the same values in the given collection boundSetOperations.intersect("bso2").forEach(v -> System.out.println("Compare the same values in the given set: " + v)); boundSetOperations.intersect(list).forEach(v -> System.out.println("Compare the same values in the given set:" + v));
10.intersectAndStore(Collection<K> keys, K destKey)和intersectAndStore(K key, K destKey)
Compare the same value in the key and store
// Compare the same values in the given collection and store boundSetOperations.intersectAndStore("bso3","bso4"); redisTemplate.opsForSet().members("bso4").forEach(v -> System.out.println("Compare the same elements of the given set: " + v));
11.union(Collection<K> keys)和union(K key)
Combine all values in a specific key
// combine all values in the given collection boundSetOperations.union("bso2").forEach(v -> System.out.println("Union all values in the given set: " + v)); boundSetOperations.union(list).forEach(v -> System.out.println("Merge all values in the given set:" + v));
12. unionAndStore ( Collection < K > keys, K destKey) 和unionAndStore ( K key, K destKey)
Combine and store all values in a specific key
boundSetOperations.unionAndStore("bso3","bso5"); redisTemplate.opsForSet().members("bso5").forEach(v -> System.out.println("Merge all values in the given set: " + v));
transfer value to a specific key
//Move the value in the collection to another collection boolean moveSuc = boundSetOperations.move("bso6","a"); System.out.println("Whether it is successful to move the value in the collection to another collection:" + moveSuc); redisTemplate.opsForSet().members("bso6").forEach(v -> System.out.println("Move the values in the set to another set:" + v)); boundSetOperations.members().forEach(v -> System.out.println("Move the value in the set to the remaining value of the original set in another set:" + v));
14.pop()
Pop the values in the collection
// pop the value from the collection Object p = boundSetOperations.pop(); System.out.println("Pop up the value in the collection: " + p); boundSetOperations.members().forEach(v -> System.out.println("Pop up the values in the set and the remaining values in the original set:" + v));
Remove elements in batches
// remove specific element long removeCount = boundSetOperations.remove("c"); System.out.println("Remove the number of specific elements:" + removeCount); boundSetOperations.members().forEach(v -> System.out.println("Remaining value of the original set after removing the specified number of elements:" + v));