1、add(K key, V… var2)
向key中批量添加值
redisTemplate.opsForSet().add("set", "aa", "bb", "cc");
redisTemplate.opsForSet().add("set", "ee");
2、members(K key)
获取key中的值
Set set = redisTemplate.opsForSet().members("set");
System.out.println("set = " + set);
3、size(K key)
获取key对应集合的长度
Long set = redisTemplate.opsForSet().size("set");
System.out.println("set = " + set);
4、randomMember(K key)
随机获取key对应的集合中的元素
Object set = redisTemplate.opsForSet().randomMember("set");
System.out.println("set = " + set);
5、randomMembers(K key, long long1)
随机获取key对应集合中指定个数的元素
- set本身是不会有重复的值
- 随机获取可能获取到一样的,所以返回的list集合中可能会有重复的值
List set = redisTemplate.opsForSet().randomMembers("set", 2);
System.out.println("set = " + set);
6、isMember(K key, Object o1)
判断key对应的集合中是否包含元素o1
Boolean member = redisTemplate.opsForSet().isMember("set", "aa");
System.out.println("member = " + member);
7、distinctRandomMembers(K key, long long1)
随机获取key对应集合中指定个数的元素,并且去重
Set set = redisTemplate.opsForSet().distinctRandomMembers("set", 3);
System.out.println("set = " + set);
8、move(K key1, V v1, K key2)
将key1对应集合中的值v1,转移到key2集合中
- key2不存在直接新增
- v1不存在,转移失败返回false
Boolean move = redisTemplate.opsForSet().move("set", "aa", "set2");
System.out.println("move = " + move);
9、pop(K key)
随机弹出key对应集合中的一个元素
Object set = redisTemplate.opsForSet().pop("set");
System.out.println("set = " + set);
10、pop(K key, long count)
随机移除key对应集合中的count个元素
List set = redisTemplate.opsForSet().pop("set", 2);
System.out.println("set = " + set);
11、remove(K key, Object… values)
批量移除key对应集合中指定的元素
redisTemplate.opsForSet().remove("set", "cc","aa");
12、scan(K key, ScanOptions options)
匹配获取键值对
- ScanOptions.NONE为获取全部键值对
- ScanOptions.scanOptions().match(“C”).build()匹配获取键位map1的键值对,不能模糊匹配。
Cursor<Object> cursor = redisTemplate.opsForSet().scan("set", ScanOptions.scanOptions().match("ee").build());
while (cursor.hasNext()){
Object object = cursor.next();
System.out.println("object = " + object);
}
13、difference(K key, Collection otherKeys)
获取key与其他集合之间的差值
- key中有,otherKeys中没有的
List list = new ArrayList();
list.add("set2");
Set set = redisTemplate.opsForSet().difference("set", list);
System.out.println("set = " + set);
14、difference(K key, K otherKey)
获取key与另一个otherKey集合之间的差值
- key中有,otherKeys中没有的
Set difference = redisTemplate.opsForSet().difference("set", "set2");
System.out.println("difference = " + difference);
15、differenceAndStore(K key, K otherKey, K destKey)
获取key与另一个otherKey所对应的集合之间的差值,并将结果存入指定的destKey中
- destKey存在,它原本集合中的所有值会被清空并且替换为获取的差值
- destKey不存在,直接新增
- key与otherKey没有获取到差值,destKey如果存在,会被删除
Long aLong = redisTemplate.opsForSet().differenceAndStore("set", "set2", "set3");
System.out.println("aLong = " + aLong);
16、differenceAndStore(K key, Collection otherKeys, K destKey)
获取key与另外一些otherKeys集合之间的差值,并将结果存入指定的destKey中
- destKey存在,它原本集合中的所有值会被清空并且替换为获取的差值
- destKey不存在,直接新增
- key与otherKeys没有获取到差值,destKey如果存在,会被删除
List list = new ArrayList();
list.add("set2");
Long aLong = redisTemplate.opsForSet().differenceAndStore("set", list, "set3");
System.out.println("aLong = " + aLong);
17、intersect(K key, K otherKey)
获取两个集合中的交集元素
Set intersect = redisTemplate.opsForSet().intersect("set", "set2");
System.out.println("intersect = " + intersect);
18、intersect(K key, Collection otherKeys)
获取多个key对应集合之间的交集
List list = new ArrayList();
list.add("set2");
list.add("set3");
Set set = redisTemplate.opsForSet().intersect("set", list);
System.out.println("set = " + set);
19、intersectAndStore(K key, K otherKey, K destKey)
获取key集合与另一个otherKey集合之间的交集元素,并将其放入指定的destKey集合中
- 没有获取到交集元素时,注意:若指定集合destKey存在,它将会被删除
Long aLong = redisTemplate.opsForSet().intersectAndStore("set", "set2", "set4");
System.out.println("aLong = " + aLong);
20、intersectAndStore(K key, Collection otherKeys, K destKey)
获取key集合与其他otherKeys集合之间的交集元素,并将其放入指定的destKey集合中
- 没有获取到交集元素时,注意:若指定集合destKey存在,它将会被删除
List list = new ArrayList();
list.add("set2");
Long aLong = redisTemplate.opsForSet().intersectAndStore("set", list, "set5");
System.out.println("aLong = " + aLong);
21、union(K key, K otherKey)
获取两个集合的合集,并且去重
Set union = redisTemplate.opsForSet().union("set", "set2");
System.out.println("union = " + union);
22、union(K key, Collection otherKeys)
获取多个集合的合集,去重
List list = new ArrayList();
list.add("set2");
Set union = redisTemplate.opsForSet().union("set", list);
System.out.println("union = " + union);
23、unionAndStore(K key, K otherKey, K destKey)
获取两个集合之间的合集,并放入指定key对应的新集合中
- 若指定key对应的集合存在,会被覆盖掉
Long aLong = redisTemplate.opsForSet().unionAndStore("se3", "set2", "set4");
System.out.println("aLong = " + aLong);
24、unionAndStore(K key, Collection otherKeys, K destKey)
获取多个集合之间的合集,并放入指定key对应的新集合中
- 若指定key对应的集合存在,会被覆盖掉
List list = new ArrayList();
list.add("set2");
list.add("set3");
redisTemplate.opsForSet().unionAndStore("set", list, "set4");
25、拓展
存储对象、对象集合为转换为JSON字符串,方便解析获取
List<MPEntity> list = mpService.list();
redisTemplate.opsForSet().add("set7", JSON.toJSONString(list), "bb", "cc");