Redis的Set是string类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据,Redis 中 集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1)。

SetOperations提供了对无序集合的一系列操作。首先初始化spring工厂获得redisTemplate和opsForSet

 private RedisTemplate<String,Object> redisTemplate;
private SetOperations<String, Object> opsForSet; @SuppressWarnings("unchecked")
@Before
public void before(){
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml");
redisTemplate = (RedisTemplate<String,Object>)context.getBean("redisTemplate");
opsForSet = redisTemplate.opsForSet();
}
Long add(K key, V… values);
Long size(K key);
Set< V > members(K key);
    @Test
public void testAddSizeAndMembers(){
redisTemplate.delete("zhou1");
//无序集合中添加元素,返回添加个数 也可以直接在add里面添加多个值
System.out.println(opsForSet.add("zhou1", "a"));//1
//无序集合的大小长度
System.out.println(opsForSet.size("zhou1"));//
System.out.println(opsForSet.add("zhou1", "a","b","c","d","e"));//
System.out.println(opsForSet.size("zhou1"));//
System.out.println(opsForSet.add("zhou1", new Object[]{"f","g"}));//
System.out.println(opsForSet.size("zhou1"));//7
//返回集合中的所有成员
System.out.println(opsForSet.members("zhou1"));//[f, g, d, a, e, b, c]
}
Long remove(K key, Object… values);
    @Test
public void testRemove(){
redisTemplate.delete("zhou2");
User user = new User();
user.setId(1l);
opsForSet.add("zhou2", "a","b","c","d","e",user);
User user1 = new User();
//移除集合中一个或多个成员
System.out.println(opsForSet.remove("zhou2", "a","d","f",user1));//
System.out.println(opsForSet.members("zhou2"));//[e, b, c]
}
V pop(K key);
    @Test
public void testPop(){
redisTemplate.delete("zhou3");
opsForSet.add("zhou3", "a","b","c","d","e");
//移除并返回集合中的一个随机元素
System.out.println(opsForSet.pop("zhou3"));//e
System.out.println(opsForSet.members("zhou3"));//[d, a, b, c]
}
Boolean move(K key, V value, K destKey);
    @Test
public void testMove(){
redisTemplate.delete("zhou4");
redisTemplate.delete("zhou5");
opsForSet.add("zhou4", "a","b");
//将 member 元素从 source 集合移动到 destination 集合
System.out.println(opsForSet.move("zhou4", "a", "zhou4"));//true
System.out.println(opsForSet.move("zhou4", "c", "zhou5"));//false
System.out.println(opsForSet.move("zhou4", "a", "zhou5"));//true
opsForSet.add("zhou5", "b");
System.out.println(opsForSet.move("zhou4", "b", "zhou5"));//true
}
Boolean isMember(K key, Object o);
    @Test
public void testIsMember(){
//判断 member 元素是否是集合 key 的成员
System.out.println(opsForSet.isMember("zhou6", "a"));//false
}
Set< V > intersect(K key, K otherKey);
Set< V > intersect(K key, Collection< K > otherKeys);
Long intersectAndStore(K key, K otherKey, K destKey);
Long intersectAndStore(K key, Collection< K > otherKeys, K destKey);
    @Test
public void testIntersect(){
redisTemplate.delete("zhou7");
redisTemplate.delete("zhou8");
redisTemplate.delete("zhou9");
redisTemplate.delete("zhou10");
redisTemplate.delete("zhou11");
opsForSet.add("zhou7", "a","b","c","d","e");
opsForSet.add("zhou8", "c","d","e","f","g");
//key对应的无序集合与otherKey对应的无序集合求交集
Set<Object> intersect = opsForSet.intersect("zhou7", "zhou8");
System.out.println(intersect);//[d, c, e]
opsForSet.add("zhou9", "c","h");
//key对应的无序集合与多个otherKey对应的无序集合求交集
System.out.println(opsForSet.intersect("zhou7", Arrays.asList("zhou8","zhou9")));//[c]
//key无序集合与otherkey无序集合的交集存储到destKey无序集合中
System.out.println(opsForSet.intersectAndStore("zhou7", "zhou8","zhou10"));//
System.out.println(opsForSet.members("zhou10"));//[e, c, d]
//key对应的无序集合与多个otherKey对应的无序集合求交集存储到destKey无序集合中
System.out.println(opsForSet.intersectAndStore("zhou7", Arrays.asList("zhou8","zhou9"),"zhou11"));//
System.out.println(opsForSet.members("zhou11"));//[c]
}
Set< V > union(K key, K otherKey);
Set< V > union(K key, Collection< K > otherKeys);
Long unionAndStore(K key, K otherKey, K destKey);
Long unionAndStore(K key, Collection otherKeys, K destKey);
    @Test
public void testUnion(){
redisTemplate.delete("zhou12");
redisTemplate.delete("zhou13");
redisTemplate.delete("zhou14");
redisTemplate.delete("zhou15");
redisTemplate.delete("zhou16");
opsForSet.add("zhou12", "a","b","c","d","e");
opsForSet.add("zhou13", "c","d","e","f","g");
//key无序集合与otherKey无序集合的并集
Set<Object> union = opsForSet.union("zhou12", "zhou13");
System.out.println(union);//[f, g, d, a, e, c, b]
opsForSet.add("zhou14", "c","h");
//key无序集合与多个otherKey无序集合的并集
System.out.println(opsForSet.union("zhou12", Arrays.asList("zhou13","zhou14")));//[h, f, g, d, a, e, c, b]
//key无序集合与otherkey无序集合的并集存储到destKey无序集合中
System.out.println(opsForSet.unionAndStore("zhou12", "zhou13","zhou15"));//
System.out.println(opsForSet.members("zhou15"));//[f, g, d, a, e, c, b]
//key无序集合与多个otherkey无序集合的并集存储到destKey无序集合中
System.out.println(opsForSet.unionAndStore("zhou12", Arrays.asList("zhou13","zhou14"),"zhou16"));//
System.out.println(opsForSet.members("zhou16"));//[h, f, g, d, a, e, c, b]
}
Set difference(K key, K otherKey);
Set difference(K key, Collection otherKeys);
Long differenceAndStore(K key, K otherKey, K destKey);
Long differenceAndStore(K key, Collection otherKeys, K destKey);
    @Test
public void testDifference(){
redisTemplate.delete("zhou17");
redisTemplate.delete("zhou18");
redisTemplate.delete("zhou19");
redisTemplate.delete("zhou20");
redisTemplate.delete("zhou21");
opsForSet.add("zhou17", "a","b","c","d","e");
opsForSet.add("zhou18", "c","d","e","f","g");
//key无序集合与otherKey无序集合的差集
Set<Object> difference = opsForSet.difference("zhou17", "zhou18");
System.out.println(difference);//[a, b]
opsForSet.add("zhou19", "c","h");
//key无序集合与多个otherKey无序集合的差集
System.out.println(opsForSet.difference("zhou17", Arrays.asList("zhou18","zhou19")));//[a, b]
//key无序集合与otherkey无序集合的差集存储到destKey无序集合中
System.out.println(opsForSet.differenceAndStore("zhou17", "zhou18","zhou20"));//
System.out.println(opsForSet.members("zhou20"));//[a, b]
//key无序集合与多个otherkey无序集合的差集存储到destKey无序集合中
System.out.println(opsForSet.differenceAndStore("zhou17", Arrays.asList("zhou18","zhou19"),"zhou21"));//
System.out.println(opsForSet.members("zhou21"));//[a, b]
}
V randomMember(K key);
   @Test
public void testRandomMember(){
redisTemplate.delete("zhou22");
opsForSet.add("zhou22", "a","b","c","d","e");
//随机获取key无序集合中的一个元素
System.out.println(opsForSet.randomMember("zhou22"));//e
System.out.println(opsForSet.randomMember("zhou22"));//d
System.out.println(opsForSet.randomMember("zhou22"));//c
System.out.println(opsForSet.randomMember("zhou22"));//b
System.out.println(opsForSet.randomMember("zhou22"));//e
//获取多个key无序集合中的元素,count表示个数
System.out.println(opsForSet.randomMembers("zhou22",8));//[e, a, e, e, d, e, b, e]
System.out.println(opsForSet.randomMembers("zhou22",4));//[d, c, d, d]
//获取多个key无序集合中的元素(去重),count表示个数
System.out.println(opsForSet.distinctRandomMembers("zhou22",6));//[c, e, d, a, b]
System.out.println(opsForSet.distinctRandomMembers("zhou22",4));//c, b, e, d]
}
Cursor scan(K key, ScanOptions options);
    @Test
public void testScan(){
redisTemplate.delete("zhou23");
opsForSet.add("zhou23", "a","b","c","d","e");
//遍历set,类似于Interator
Cursor<Object> curosr = opsForSet.scan("zhou23", ScanOptions.NONE);
while(curosr.hasNext()){
System.out.println(curosr.next());//e a d c b
}
}

转载自:https://blog.csdn.net/weixin_37490221/article/details/78135202

RedisTemplate访问Redis数据结构(四)——Set的更多相关文章

  1. RedisTemplate访问Redis数据结构

    https://www.jianshu.com/p/7bf5dc61ca06 Redis 数据结构简介 Redis 可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字 ...

  2. RedisTemplate访问Redis数据结构(介绍和常用命令)

    Redis 数据结构简介 Redis 可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串).List(列表).Set(集合).Hash(散列)和 Zset(有序集 ...

  3. 如何使用RedisTemplate访问Redis数据结构之字符串操作

    Redis 数据结构简介 Redis 可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串).List(列表).Set(集合).Hash(散列)和 Zset(有序集 ...

  4. 如何使用RedisTemplate访问Redis数据结构

    RedisTemplate介绍 spring封装了RedisTemplate对象来进行对redis的各种操作,它支持所有的 redis 原生的api. RedisTemplate在spring代码中的 ...

  5. Redis(九):使用RedisTemplate访问Redis数据结构API大全

    RedisTemplate介绍 spring封装了RedisTemplate对象来进行对redis的各种操作,它支持所有的 redis 原生的api. RedisTemplate在spring代码中的 ...

  6. 如何使用RedisTemplate访问Redis数据结构之list

    Redis的List数据结构 这边我们把RedisTemplate序列化方式改回之前的 Jackson2JsonRedisSerializer<Object> jackson2JsonRe ...

  7. RedisTemplate访问Redis数据结构(一)——String

    当对String数据结构进行操作时,推荐直接使用spring-data-redis提供的StringRedisTemplate,其配置如下 <bean id="stringRedisT ...

  8. RedisTemplate访问Redis数据结构(前言)

    Redis五种基本数据结构 redis提供键值对的形式对数据进行存储.支持五种数据类型:String(字符串),List(链表),Hash(散列),Set(无序集合),ZSet(有序集合).下面是网上 ...

  9. 如何使用RedisTemplate访问Redis数据结构之Zset

    Redis的ZSet数据结构 Redis 有序集合和无序集合一样也是string类型元素的集合,且不允许重复的成员. 不同的是每个元素都会关联一个double类型的分数.redis正是通过分数来为集合 ...

随机推荐

  1. MySql 性能优化之 Explain

    MySQL 之 Explain 输出分析 背景 前面的文章写过 MySQL 的事务和锁,这篇文章我们来聊聊 MySQL 的 Explain,估计大家在工作或者面试中多多少少都会接触过这个.可能工作中实 ...

  2. [19/09/08-星期日] Python的几个概念和语法

    一.表达式.语句.程序.函数 1.表达式 就是一个类似于数学公式的东西 ,比如:10 + 5 8 - 4:表达式一般仅仅用了计算一些结果,不会对程序产生实质性的影响 如果在交互模式中输入一个表达式,解 ...

  3. 编写Servlet步骤以及Servlet生命周期是怎样的

    一.编写Servlet步骤 1.继承HttpServlet,HttpServlet在javax-servlet-api依赖下 2.重写doGet()或者doPost()方法 3.在web.xml中注册 ...

  4. P1219八皇后

    这个题是一道USACO的经典dfs,与我见面的时间起码七个月了. 放置n个皇后于n*n棋盘,他们不能互相吃(行,列,对角线),问有几种摆法?于是想到了dfs(自我认为有图的就不用DP).首先确定好了要 ...

  5. 洛谷 P5663 加工零件 & [NOIP2019普及组] (奇偶最短路)

    传送门 解题思路 很容易想到用最短路来解决这一道问题(题解法),因为两个点之间可以互相无限走,所以如果到某个点的最短路是x,那么x+2,x+4也一定能够达到. 但是如何保证这是正确的呢?比如说到某个点 ...

  6. virtualenvwrapper安装和使用

    virtualenvwrapper安装和使用步骤: 1.安装: *nix上安装的命令: pip install virtualenvwrapper windows上安装的命令: pip install ...

  7. [LeetCode] 84. 柱状图中最大的矩形

    题目链接 : https://leetcode-cn.com/problems/largest-rectangle-in-histogram/ 题目描述: 给定 n 个非负整数,用来表示柱状图中各个柱 ...

  8. 2019 湖湘杯 Reverse WP

    0x01 arguement 下载链接:https://www.lanzous.com/i7atyhc 1.准备 获取到信息: 32位的文件 upx加密文件 在控制台打开文件 使用"upx ...

  9. 【学习总结】快速上手Linux玩转典型应用-第4章-准备工作

    课程目录链接 快速上手Linux玩转典型应用-目录 目录 1. 准备工作一 2. 准备工作二 ===================================================== ...

  10. Vue框架前言

    Vue框架 Vue 框架: 官网 vue框架:渐进式JavaScript框架 vue一个环境:可以只控制页面中一个标签.可以控制一组标签.可以控制整个页面.可以控制整个项目 vue可以根据实际需求,选 ...