set operation

 

A Redis Set is an unordered collection of type String. Set members are unique, which means that there cannot be duplicate data in the set. The set in Redis is implemented through a hash table, so the complexity of adding, deleting, and searching is O(1)

 

1. Add one or more members to the collection SADD key member1 [member2] 

2. Get the number of members of the set SCARD key

3. Determine whether the member element is a member of the set key SISMEMBER key member 

4. Returns the SMEMBERS key of all members in the collection 

5. Remove one or more members from the set SREM key member1 [member2] 

6. Remove and return a random element in the collection SPOP key  count

7. Returns the difference SDIFF of all sets given key1 [key2] 

8. Returns the union of all the given sets SUNION key1[key2] 

9. Returns the intersection of all sets given SINTER key1[key2] 

 

set code operation:

package com.study.util;

import java.util.Set;

import redis.clients.jedis.Jedis;

public  class ReturnSet {

    public static void main(String[] args) {
        Jedis jedis = RedisUtil.getJedis();
        
        //想集合set中添加a,b,c,d,e,f
        jedis.sadd("set", "a","b","c","d","e","f");
        //获取set集合元素
        Set<String> valueList = jedis.smembers("set");
        System.out.print("set集合元素:");
        for (String value : valueList) {
            System.out.print(value);
        }
        System.out.println();
        
        // Get the number of members of the set collection 
        long length = jedis.scard("set" );
        System.out.println( "Number of members of the set collection: "+ length);
        
        // Determine whether the strings b and g exist in the set 
        boolean bflag = jedis.sismember("set", "b" );
        System.out.println( "Whether b exists in the set set:" + bflag);
         boolean gflag = jedis.sismember("set", "g" );
        System.out.println( "Whether g exists in the set:" + gflag);
        
        // Remove the elements in the set set ab 
        jedis.srem("set", "a","b" );
        valueList = jedis.smembers("set");
        System.out.print( "Set collection element after removal:" );
         for (String value : valueList) {
            System.out.print(value);
        }
        System.out.println();
        
        // Randomly remove an element of the set set and return 
        String popValue = jedis.spop("set" );
        System.out.println( "The randomly removed element is: " + popValue);
        valueList = jedis.smembers("set");
        System.out.print( "Set collection elements after random removal:" );
         for (String value : valueList) {
            System.out.print(value);
        }
        System.out.println();
        
        // Add dijk 
        jedis.sadd("set1", "d","i","j","k" ) to set set1;
         // Get the difference between set set and set1 set 
        Set<String> diffSet = jedis.sdiff("set","set1" );
        System.out.println( "The difference between set set and set1 set: " );
         for (String value : diffSet) {
            System.out.print(value);
        }
        System.out.println();
        
        // Get the union of set set and set1 set 
        Set<String> unionSet =jedis.sunion("set","set1" );
        System.out.println( "Union of set set and set1 set: " );
         for (String value : unionSet) {
            System.out.print(value);
        }
        System.out.println();
        
        // Get the intersection of set collection and set1 collection 
        Set<String> interSet =jedis.sinter("set","set1" );
        System.out.println( "Intersection of set set and set1 set: " );
         for (String value : interSet) {
            System.out.print(value);
        }
        System.out.println();
        
        jedis.close();
    }
}
View Code

 

Code git address: https://gitee.com/sjcq/redis.git

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325160028&siteId=291194637