Redis之set类型操作
接口:
package com.net.test.redis.base.dao; /** * @author*** * @Time:2017年8月10日 下午2:32:12 * @version 1.0 * @description */ public interface IRedisDaoNoSotredSet { public void add(String key, String...values); public void diff(String key, String otherKey); public void diddStore(String destKey, String key, String otherKey); public void inter(String key, String otherKey); public void interStore(String destKey, String key, String otherKey); public void union(String key, String otherKey); public void unionStore(String destKey, String key, String otherKey); public void pop(String key); public void remove(String key,String...values); public void size(String key); public void smembers(String key); }
实现类:
package com.net.test.redis.base.dao.imp; import java.util.Set; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.SetOperations; import org.springframework.stereotype.Repository; import com.net.test.redis.base.dao.IRedisDaoNoSotredSet; /** * @author *** * @Time:2017年8月10日 下午2:53:33 * @version 1.0 * @description */ @Repository public class RedisDaoNoSotredSetImp implements IRedisDaoNoSotredSet{ @Autowired private RedisTemplate<String, String> redis; /** * @description 添加元素 * @param key * @param values */ @Override public void add(String key, String...values) { SetOperations<String, String> oper = redis.opsForSet(); int value = oper.add(key, values).intValue(); System.out.println("是否添加成功 :" + value); } /** * @description 找出两个集合不同的部分,并进行遍历 * @param key * @param otherKey */ @Override public void diff(String key, String otherKey) { SetOperations<String, String> oper = redis.opsForSet(); Set<String> set = oper.difference(key,otherKey); for(String str : set) { System.out.println("两个set的不同元素 : " + str); } } /** * @description 找出两个集合不同的部分,并存到destKey集合中 * @param destKey * @param key * @param otherKey */ @Override public void diddStore(String destKey, String key, String otherKey) { SetOperations<String, String> oper = redis.opsForSet(); int value = oper.differenceAndStore(key, otherKey, destKey).intValue(); System.out.println("返回值 : " + value); } /** * @description 找出两个集合的相同部分,并进行遍历 * @param key * @param otherKey */ @Override public void inter(String key, String otherKey) { SetOperations<String, String> oper = redis.opsForSet(); Set<String> set = oper.intersect(key, otherKey); for(String str : set) { System.out.println("两个set的相同元素 : " + str); } } /** * @description 找出两个集合的相同部分,存到新集合destKey中 * @param destKey * @param key * @param otherKey */ @Override public void interStore(String destKey, String key, String otherKey) { SetOperations<String, String> oper = redis.opsForSet(); int value = oper.intersectAndStore(key, otherKey, destKey).intValue(); System.out.println("返回值 : " + value); } /** * @description 找出兩個集合的并集,并進行遍歷 * @param key * @param otherKey */ @Override public void union(String key, String otherKey) { SetOperations<String, String> oper = redis.opsForSet(); Set<String> set = oper.union(key, otherKey); for(String str : set) { System.out.println("两个set的相同元素 : " + str); } } /** * @description 找出兩個集合的并集,并存放到新集合destKey中 * @param destKey * @param key * @param otherKey */ @Override public void unionStore(String destKey, String key, String otherKey) { SetOperations<String, String> oper = redis.opsForSet(); int value = oper.unionAndStore(key, otherKey, destKey).intValue(); System.out.println("返回值 : " + value); } /** * @description 无序集合,随机移除一个元素 * @param key */ @Override public void pop(String key) { SetOperations<String, String> oper = redis.opsForSet(); String value = oper.pop(key); System.out.println("随机移除的value为 : " + value); } /** * @description 根据给定的元素,去移除 * @param key * @param values */ @Override public void remove(String key, String...values) { SetOperations<String, String> oper = redis.opsForSet(); int value = oper.remove(key, (Object[])values).intValue(); System.out.println("是否成功移除选定元素 : " + value); } /** * @description 获取集合长度 * @param key */ @Override public void size(String key) { SetOperations<String, String> oper = redis.opsForSet(); int value = oper.size(key).intValue(); System.out.println("set 集合长度为 : " + value); } /** * @description 遍历整个集合 * @param key */ @Override public void smembers(String key) { SetOperations<String, String> oper = redis.opsForSet(); Set<String> set = oper.members(key); for(String str : set) { System.out.println("两个set的相同元素 : " + str); } } }
分类:
Redis
标签:
Redis之set类型操作
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)