RedisTemplate-opsForSet集合操作api

news/2025/5/24 14:11:15

  1、add(K key, V... values)
  向变量中批量添加值。
redisTemplate.opsForSet().add("setValue","A","B","C","B","D","E","F");  
 

  2、members(K key)
  获取变量中的值。
Set set = redisTemplate.opsForSet().members("setValue");  
System.out.println("通过members(K key)方法获取变量中的元素值:" + set);  
 

  3、size(K key)
   获取变量中值的长度。
long setLength = redisTemplate.opsForSet().size("setValue");  
System.out.println("通过size(K key)方法获取变量中元素值的长度:" + setLength);  
 

  4、randomMember(K key)
   随机获取变量中的元素。
Object randomMember = redisTemplate.opsForSet().randomMember("setValue");  
System.out.println("通过randomMember(K key)方法随机获取变量中的元素:" + randomMember);  
 

  5、randomMembers(K key, long count)
  随机获取变量中指定个数的元素。
List randomMembers = redisTemplate.opsForSet().randomMembers("setValue",2);  
System.out.println("通过randomMembers(K key, long count)方法随机获取变量中指定个数的元素:" + randomMembers);  
 

  6、isMember(K key, Object o)
  检查给定的元素是否在变量中。
boolean isMember = redisTemplate.opsForSet().isMember("setValue","A");  
System.out.println("通过isMember(K key, Object o)方法检查给定的元素是否在变量中:" + isMember);  
 

  7、move(K key, V value, K destKey)
   转移变量的元素值到目的变量。
boolean isMove = redisTemplate.opsForSet().move("setValue","A","destSetValue");  
if(isMove){  
    set = redisTemplate.opsForSet().members("setValue");  
    System.out.print("通过move(K key, V value, K destKey)方法转移变量的元素值到目的变量后的剩余元素:" + set);  
    set = redisTemplate.opsForSet().members("destSetValue");  
    System.out.println(",目的变量中的元素值:" + set);  
}  
 

  8、pop(K key)
   弹出变量中的元素。
Object popValue = redisTemplate.opsForSet().pop("setValue");  
System.out.print("通过pop(K key)方法弹出变量中的元素:" + popValue);  
set = redisTemplate.opsForSet().members("setValue");  
System.out.println(",剩余元素:" + set)  


  9、remove(K key, Object... values)
   批量移除变量中的元素。
long removeCount = redisTemplate.opsForSet().remove("setValue","E","F","G");  
System.out.print("通过remove(K key, Object... values)方法移除变量中的元素个数:" + removeCount);  
set = redisTemplate.opsForSet().members("setValue");  
System.out.println(",剩余元素:" + set);

  
  10、scan(K key, ScanOptions options)
   匹配获取键值对,ScanOptions.NONE为获取全部键值对;ScanOptions.scanOptions().match("C").build()匹配获取键位map1的键值对,不能模糊匹配。
//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("通过scan(K key, ScanOptions options)方法获取匹配的值:" + object);  
}  


  11、difference(K key, Collection<K> otherKeys)
   通过集合求差值。
List list = new ArrayList();  
list.add("destSetValue");  
Set differenceSet = redisTemplate.opsForSet().difference("setValue",list);  
System.out.println("通过difference(K key, Collection<K> otherKeys)方法获取变量中与给定集合中变量不一样的值:" + differenceSet);  
        
        
  12、difference(K key, K otherKey)
   通过给定的key求2个set变量的差值。
differenceSet = redisTemplate.opsForSet().difference("setValue","destSetValue");  
System.out.println("通过difference(K key, Collection<K> otherKeys)方法获取变量中与给定变量不一样的值:" + differenceSet);  


  13、differenceAndStore(K key, K otherKey, K destKey)
    将求出来的差值元素保存。
redisTemplate.opsForSet().differenceAndStore("setValue","destSetValue","storeSetValue");  
set = redisTemplate.opsForSet().members("storeSetValue");  
System.out.println("通过differenceAndStore(K key, K otherKey, K destKey)方法将求出来的差值元素保存:" + set); 

 
  14、differenceAndStore(K key, Collection<K> otherKeys, K destKey)
    将求出来的差值元素保存。
redisTemplate.opsForSet().differenceAndStore("setValue",list,"storeSetValue");  
set = redisTemplate.opsForSet().members("storeSetValue");  
System.out.println("通过differenceAndStore(K key, Collection<K> otherKeys, K destKey)方法将求出来的差值元素保存:" + set); 

 
  15、distinctRandomMembers(K key, long count)
     获取去重的随机元素。
set = redisTemplate.opsForSet().distinctRandomMembers("setValue",2);  
System.out.println("通过distinctRandomMembers(K key, long count)方法获取去重的随机元素:" + set);  


  16、intersect(K key, K otherKey)
    获取2个变量中的交集。
set = redisTemplate.opsForSet().intersect("setValue","destSetValue");  
System.out.println("通过intersect(K key, K otherKey)方法获取交集元素:" + set); 

 
  17、intersect(K key, Collection<K> otherKeys) 
    获取多个变量之间的交集。
set = redisTemplate.opsForSet().intersect("setValue",list);  
System.out.println("通过intersect(K key, Collection<K> otherKeys)方法获取交集元素:" + set); 

 
  18、intersectAndStore(K key, K otherKey, K destKey)
     获取2个变量交集后保存到最后一个参数上。
redisTemplate.opsForSet().intersectAndStore("setValue","destSetValue","intersectValue");  
set = redisTemplate.opsForSet().members("intersectValue");  
System.out.println("通过intersectAndStore(K key, K otherKey, K destKey)方法将求出来的交集元素保存:" + set);  


  19、intersectAndStore(K key, Collection<K> otherKeys, K destKey)  
     获取多个变量的交集并保存到最后一个参数上。
redisTemplate.opsForSet().intersectAndStore("setValue",list,"intersectListValue");  
set = redisTemplate.opsForSet().members("intersectListValue");  
System.out.println("通过intersectAndStore(K key, Collection<K> otherKeys, K destKey)方法将求出来的交集元素保存:" + set); 

 
  20、union(K key, K otherKey)
     获取2个变量的合集。
set = redisTemplate.opsForSet().union("setValue","destSetValue");  
System.out.println("通过union(K key, K otherKey)方法获取2个变量的合集元素:" + set);

  
  21、union(K key, Collection<K> otherKeys)
    获取多个变量的合集。
set = redisTemplate.opsForSet().union("setValue",list);  
System.out.println("通过union(K key, Collection<K> otherKeys)方法获取多个变量的合集元素:" + set);  


  22、unionAndStore(K key, K otherKey, K destKey)
    获取2个变量合集后保存到最后一个参数上。
redisTemplate.opsForSet().unionAndStore("setValue","destSetValue","unionValue");  
set = redisTemplate.opsForSet().members("unionValue");  
System.out.println("通过unionAndStore(K key, K otherKey, K destKey)方法将求出来的交集元素保存:" + set);

  
  23、unionAndStore(K key, Collection<K> otherKeys, K destKey)     
    获取多个变量的合集并保存到最后一个参数上。
redisTemplate.opsForSet().unionAndStore("setValue",list,"unionListValue");  
set = redisTemplate.opsForSet().members("unionListValue");  
System.out.println("通过unionAndStore(K key, Collection<K> otherKeys, K destKey)方法将求出来的交集元素保存:" + set);

文章来源:https://blog.csdn.net/qq_41712271/article/details/103702026
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:https://dhexx.cn/news/show-19076.html

相关文章

三角形面积求法

Sa*h/2 (底*高/2)Sa*b*sinC/2 (两边和夹角)Ssqrt(p*(p-a)*(p-b)*(p-c)) p(abc)/2 (已知3边长)Sa*b*c/(4r) (已知3边长和外接圆半径r)S|cross(A,B,C)|/2 (叉积的一半)转载于:https://www.cnblogs.com/wmrv587/p/3477668.html

软件测试 数据处理过滤分析,基于数据包抓取与分析的软件网络接口测试技术研究_杜松阳.pdf...

基于数据包抓取与分析的软件网络接口测试技术研究_杜松阳2011 International Conference on Information, Services and Management Engineering (ISME2011)Research on the Testing Techniques of the SoftwareNetwork Interface Based on the Capture and Analysis ofthe Pac…

结构体指针memcpy出错_编程达人 C语言中复制一个结构体只能使用memcpy的方法吗?感觉有些麻烦,有别的方法吗?...

有读者看到后&#xff0c;认为C语言结构体的赋值并不等价于 memcpy&#xff0c;也有朋友评论说 ba 是“浅拷贝”&#xff0c;还有读者提到结构体赋值效率没有memcpy高&#xff0c;那么 b a 语句被执行后&#xff0c;究竟发生了什么呢&#xff1f;编写测试C语言代码得到答案最简…

HOJ 1056 Machine Schedule (二分图匹配,匈牙利算法)

题意&#xff1a; 给k个任务&#xff0c;每一个任务都能被Computer A的x_mode或Computer B的y_mode处理。mode的转换需要重启。问如何安排任务的处理顺序&#xff0c;可以使得重启的次数最少。 分析&#xff1a; 二分图匹配。将每一个任务看成一条边&#xff0c;端点分别是Comp…

spring+dubbo 整合最简易环境搭建

1 新建一个空工程 2 新建 maven工程 gonggongjiekou&#xff0c;此工程是作为 公用的接口&#xff0c;类 //定义pojo package cn.taobao.pojo; import java.io.Serializable; import java.util.Date;public class News implements Serializable {private Integer id;private…

[转载]c标签 if else c标签 总结

原文地址&#xff1a;c标签 if else c标签 总结作者&#xff1a;JavaLeaderJSTL标签用法 关键字&#xff1a;JSTL标签、<c:choose>、<c:forEach>、<c:forTokens>、<c:if>、<c:import>、<c:otherwise>、<c:out>、<c:param>、&l…

计算机 如何设置页面文件,电脑怎么设置设置虚拟内存或者页面文件?

虚拟内存作为物理内存的后备力量&#xff0c;随着大内存电脑的普及&#xff0c;虚拟内存好像无用武之地&#xff0c;但有的程序需要虚拟内存的存在方可正常运行&#xff0c;这里我来教大家怎么设置虚拟内存。本文以Windows XP为例。1、找到“我的电脑”&#xff0c;鼠标右键点击…

cbr流过低 ns2_NS2学习笔记(一)

NS2有两种运行方式:1.“脚本方式”&#xff0c;输入命令: ns tclscripl.tcl&#xff0c;其中 tclscripl.tcl 是一个Tcl脚本的文件名&#xff1b;2“命令行方式”&#xff0c;输入命令&#xff1a;ns&#xff0c;进入NS2的命令行环境&#xff0c;然后直接输入各种命令来交互式地…

在Windows上安装MongoDB(译)

简介 本教程提供了安装和运行的方法在Microsoft Windows MongoDB服务器&#xff08;即“mongod.exe”&#xff09;平台通过在命令提示符和概括的过程设立的MongoDB 作为 Windows 服务 。 与Windows的操作MongoDB的是类似MongoDB的其他 platforms大多数组件共享相同的运作模式。…

什么品牌的蓝牙耳机适合学生党用?学生党高性价比国产蓝牙耳机推荐

最近看到很多网友问什么品牌的蓝牙耳机适合学生党用&#xff1f;现在的蓝牙耳机品牌越来越多&#xff0c;适合学生党用的蓝牙耳机品牌也不少。下面&#xff0c;我来给大家推荐几款适合学生党的国产高性价比蓝牙耳机&#xff0c;可以当个参考。 一、南卡小音舱蓝牙耳机 参考价…