RedisTemplate访问Redis数据结构(四)——Set

摘要:
Redis集合是字符串类型的无序集合。集合的成员是唯一的,这意味着集合中不能出现重复的数据。Redis中的集合是通过哈希表实现的,因此添加、删除和搜索的复杂性是O。SetOperations对无序集合提供了一系列操作。首先,初始化spring工厂,获取redisTemplate和opsForSetprivateRedisTemplateredisTemplate;privateSetOperations<String,Object>opsForSet;@SuppressWarnings@Beforepublicvoidbefore(){@SuppressWarningsApplicationContextcontext=newClassPathXmlApplicationContext;redisTemplate=context.getBean;opsForSet=redisTemplate.opsForSet();}Longadd(Kkey,V…

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"));//1
        System.out.println(opsForSet.add("zhou1", "a","b","c","d","e"));//4
        System.out.println(opsForSet.size("zhou1"));//5
        System.out.println(opsForSet.add("zhou1", new Object[]{"f","g"}));//4
        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));//2
        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"));//3
        System.out.println(opsForSet.members("zhou10"));//[e, c, d]
        //key对应的无序集合与多个otherKey对应的无序集合求交集存储到destKey无序集合中
        System.out.println(opsForSet.intersectAndStore("zhou7", Arrays.asList("zhou8","zhou9"),"zhou11"));//1
        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"));//7
        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"));//8
        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"));//2
        System.out.println(opsForSet.members("zhou20"));//[a, b]
        //key无序集合与多个otherkey无序集合的差集存储到destKey无序集合中
        System.out.println(opsForSet.differenceAndStore("zhou17", Arrays.asList("zhou18","zhou19"),"zhou21"));//2
        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》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇JMeter简介及使用JMeter来访问网站CF981E Addition on Segments(线段树分治+bitset)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

[转] hive0.14-insert、update、delete操作测试

FROM : http://blog.csdn.net/hi_box/article/details/40820341 首先用最普通的建表语句建一个表: [java] view plaincopy  hive>create table test(id int,name string)row format delimited fields ter...

Java多线程2

线程的生命周期 与人有生老病死一样,线程也同样要经历开始(等待)、运行、挂起和停止四种不同的状态。这四种状态都可以通过Thread类中的方法进行控制。下面给出了Thread类中和这四种状态相关的方法。 1 // 开始线程 2 public void start( ); 3 public void run( ); 4 5 // 挂...

Redis分布式锁

加锁 所以需要保证设置锁及其过期时间两个操作的原子性,spring data的 RedisTemplate 当中并没有这样的方法。但是在jedis当中是有这种原子操作的方法的,需要通过 RedisTemplate 的 execute 方法获取到jedis里操作命令的对象,代码如下: String result = redisTemplate.execut...

用python从redis的有序集合中一次性删除多个值

最近做的一个东西,需要用python从redis的有序集合中一次性删除多个值; redis的自带方法zrem是支持此功能的 # 移除多个元素 redis> ZREM page_rank baidu.com bing.com (integer) 2 python redis 的api 中也支持 zrem(self, name, *values)...

动手学深度学习13-权重衰减

权重衰减 高维线性回归实验 从零开始实现 初始化模型参数 定义L2范数惩罚项 定义训练和测试 使用权重衰减 pytorch简洁实现 小结 上一节中提提到的过拟合现象,在模型的训练误差远小于测试集上的误差。虽然增大训练接数据集可以减轻过拟合,但是获得额外的训练数据往往代价过大,本节介绍过拟合常用的方式:权重衰减(weight decay)。 权重衰...

Redis学习(一) —— 基本使用与原理

一、数据结构 string Redis字符串是可修改字符串,在内存中以字节数组形式存在。 下面是string在源码中的定义,SDS(Simple Dynamic String) struct SDS<T> { T capacity; // 数组容量 T len; // 数组长度 byte flags; // 特殊标识位,不理睬它...