1.set
1. An unordered and unique set of string elements
sadd key member1 [member2]: add multiple members
Scar key: count members
sdiff key1 [key2]: find out the difference set of two sets, the members that key2 does not have in key1
sidffstore des key1 [key2]: save the difference set to the des set
Single key1 [key2]: find the intersection of two sets
sismember key member: judge whether member is a member of key
smembers key: return all members
smove src des member: move member from set src to des
Pop key: randomly pop up a member
srandmember key [count]: returns one or more members randomly. Count is the total number of returned members.
srem key member1 [member2]: remove members
Union key1 [key2]: Union
2. In java program
// Set the RedisTemplate value serializer to StringRedisSerializer to test the snippet ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class); Set set = null; // Add element to list redisTemplate.boundSetOps("set1").add("v1", "v2", "v3", "v4", "v5", "v6"); redisTemplate.boundSetOps("set2").add("v0", "v2", "v4", "v6", "v8"); // Find the set length redisTemplate.opsForSet().size("set1"); // Difference set set = redisTemplate.opsForSet().difference("set1", "set2"); // Seeking Union set = redisTemplate.opsForSet().intersect("set1", "set2"); // Determine whether the elements in the collection boolean exists = redisTemplate.opsForSet().isMember("set1", "v1"); // Get all elements of the collection set = redisTemplate.opsForSet().members("set1"); // Pop an element at random from the collection String val = (String) redisTemplate.opsForSet().pop("set1"); // Get elements of a collection at random val = (String) redisTemplate.opsForSet().randomMember("set1"); // Get elements of 2 sets at random List list = redisTemplate.opsForSet().randomMembers("set1", 2L); // Delete the elements of a collection. The parameters can be multiple redisTemplate.opsForSet().remove("set1", "v1"); // Find the union of two sets redisTemplate.opsForSet().union("set1", "set2"); // Find the difference set of two sets and save it to the set diff set redisTemplate.opsForSet().differenceAndStore("set1", "set2", "diff_set"); // Find the intersection of two sets and save it to the set inter set redisTemplate.opsForSet().intersectAndStore("set1", "set2", "inter_set"); // Find the union of two sets, and save it to the union set. redisTemplate.opsForSet().unionAndStore("set1", "set2", "union_set");