Redis data type-collection type

The husband Tao Gong has been 清风through the ages, and who is there to dare to call him a man?
Insert picture description here


Introduction

1. Basic commands

1. Add/remove elements

SADD key member [member …] // 添加元素

特性

  • Used to add 一个or 多个elements to the collection , the key does not exist to 自动create.
  • 不能有相同Elements in the set , adding the same element will be 忽略dropped.
  • The return value is 成功加入the number of elements in the collection ( 忽略掉的元素不算)

SREM key member [member …] // 删除元素

特性

  • Remove 一个or 多个element from the collection
  • The return value is the 删除成功number of elements this time
  • 不存在Elements will be 忽略dropped

Case:

  • SADD
127.0.0.1:6379> sadd letters a //向集合中添加一个元素
(integer) 1
127.0.0.1:6379> sadd letters a b c //元素a已经存在,返回值为 2
(integer) 2
127.0.0.1:6379> sadd letters a b c //3个元素都存在,返回值为 0
(integer) 0
  • SREM
127.0.0.1:6379> srem letters a //删除元素a 返回值 1
(integer) 1
127.0.0.1:6379> srem letters  b d //d元素不存在,返回值为 1
(integer) 1
127.0.0.1:6379> srem letters  g //g元素不存在,返回值为 0
(integer) 0

practice:Use Java to operate Redis databaseSADDSREM

@Test
    public void test() {
    
    
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        Long a = jedis.sadd("letters", new String[]{
    
    "a"});
        Long a_b_c = jedis.sadd("letters", new String[]{
    
    "a", "b", "c"});

        System.out.print("添加元素:a" + "  ");
        System.out.println("返回值:" + a);

        System.out.print("添加元素:a、b、c" + "  ");
        System.out.println("返回值:" + a_b_c);

        Long rm_a = jedis.srem("letters", new String[]{
    
    "a"});
        Long rm_a_b_c = jedis.srem("letters", new String[]{
    
    "a", "b", "c"});
        Long rm_d = jedis.srem("letters", "d");

        System.out.print("删除元素:a" + "  ");
        System.out.println("返回值:" + rm_a);

        System.out.print("删除元素:a、b、c" + "  ");
        System.out.println("返回值:" + rm_a_b_c);

        System.out.print("删除元素:d" + "  ");
        System.out.println("返回值:" + rm_d);
    }
添加元素:a  返回值:1
添加元素:a、b、c  返回值:2
删除元素:a  返回值:1
删除元素:a、b、c  返回值:2
删除元素:d  返回值:0

2. Get all the elements in the set

SMEMBERS key

特性

  • Get the 所有elements in the collection
  • keyWill return if it doesn't exist
127.0.0.1:6379> sadd letters a b c //添加3个元素
(integer) 3
127.0.0.1:6379> smembers letters //获取所有的元素
1) "b"
2) "c"
3) "a"
127.0.0.1:6379> smembers other //获取一个不存在的集合 返回空
(empty list or set)

3. Determine whether the element is in the set

SISMEMBER key member

特性

  • No matter how many elements there are in the set, 时间复杂度it is:O(1)
  • memberReturn 1if it exists, return if it doesn't exist0
  • keyReturn if it doesn't exist0
  • 一次Only the 一个element can be judged , otherwise an error will be reported:(error) ERR wrong number of arguments for 'sismember' command
127.0.0.1:6379> smembers letters //获取所有元素
1) "b"
2) "c"
3) "a"
127.0.0.1:6379> sismember letters a //判断元素 a
(integer) 1
127.0.0.1:6379> sismember letters a b //错误语法判断元素 报错
(error) ERR wrong number of arguments for 'sismember' command
127.0.0.1:6379> sismember letters d //判断不存在元素
(integer) 0

4. Operations between sets

SDIFF key [key …] // 差集Calculation

特性

  • The SDIFF command is used to 多个集合perform 差集calculations.
  • Set Athe set Bdifference set is denoted A-B, all the representative 属于Aand the 不属于Bset (shown below) is composed of elements, and A-B={x|x∈A且x∈B}.
  • SDIFF command supports 同时传入多个键(key), when there are three or more, calculated order is calculated 前面两个, and then the calculation result with 后面a calculation 依此类推.

Insert picture description here

Case:

First, we define 3a set: 1 2 3setA:; setB:; 2 3 4setC:, 2 3when A and B perform the difference operation A-Bbecause 存在2 3, in B 不存在1, so 结果为1, then we then calculate the result of AB 1and C集合perform the difference operation ( A-B)-C, which is 1And 2 3operation, so calculation 结果为1.

127.0.0.1:6379> sadd setA 1 2 3  
(integer) 3
127.0.0.1:6379> sadd setB 2 3 4
(integer) 3
127.0.0.1:6379> sdiff setA setB
1) "1"
127.0.0.1:6379> sadd setC 2 3
(integer) 2
127.0.0.1:6379> sdiff setA setB setC
1) "1"

SINTER key [key …] // 交集Calculation

特性

  • Used to 多个集合perform 交集operations on
  • Set A and set B represent a set A∩Bof all 属于Aand 属于Belements, ie A∩B={x|x∈A且x∈B}.
  • This command supports multiple simultaneous incoming keys 用法同交集运算.

Case:

127.0.0.1:6379> smembers setA
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> smembers setB
1) "2"
2) "3"
3) "4"
127.0.0.1:6379> sinter setA setB
1) "2"
2) "3"

SUNION key [key …] // 并集Calculation

特性

  • The SUNION command is used to 多个集合perform 并集operations on
  • The union of set A and set B A∪Brepresents a set consisting of all 属于Aor 属于Belements, ie A∪B={x|x∈A或x∈B}.
  • This command supports multiple simultaneous incoming keys 用法同交集运算.

案例

127.0.0.1:6379> smembers setA
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> smembers setB
1) "2"
2) "3"
3) "4"
127.0.0.1:6379> sunion setA setB
1) "1"
2) "2"
3) "3"
4) "4"
127.0.0.1:6379> smembers setC
1) "2"
2) "3"
127.0.0.1:6379> sunion setA setB setC
1) "1"
2) "2"
3) "3"
4) "4"

Second, advanced commands

1. Get the number of elements in the collection

SCARD key

This command is used to get the number of elements in the collection 元素个数, if 键存在, then the number of collection elements is returned normally, 不存在then 返回0.

7.0.0.1:6379> sadd nums 1 2 3 4 5 6 7 8 9 0
(integer) 10
127.0.0.1:6379> smembers nums
 1) "0"
 2) "1"
 3) "2"
 4) "3"
 5) "4"
 6) "5"
 7) "6"
 8) "7"
 9) "8"
10) "9"
127.0.0.1:6379> scard nums
(integer) 10
127.0.0.1:6379> scard other //other键不存在,返回 0
(integer) 0

2. Perform set operations and store key results

The following three commands and STOREthe usage of removing the suffix are 相同only 计算结果stored 键destinationin the following three commands .

SDIFFSTORE destination key [key …]

SINTERSTORE destination key [key …]

SUNIONSTORE destination key [key …]

The usage of the above three commands is the SDIFF SINTER SUNIONsame as the previous usage, so I won't introduce them in detail here.

127.0.0.1:6379> sadd setA 1 2 3
(integer) 3
127.0.0.1:6379> sadd setB 2 3 4
(integer) 3
127.0.0.1:6379> sadd setC 4 5 6
(integer) 3
127.0.0.1:6379> sdiffstore AB setA setB //将集合setA与集合setB的计算结果存储到集合AB中
(integer) 1
127.0.0.1:6379> keys *
1) "nums"
2) "setB"
3) "setA"
4) "setC"
5) "AB"
127.0.0.1:6379> smembers AB
1) "1"

3. Randomly obtain elements in the set

SRANDMEMBER key [count]

特性

  • The SRANDMEMBER command is used to randomly 随机get 一个elements from the set ( 没有count参数情况下).
  • 有count参数Can be randomly obtained once多个元素
  • When the count is 整数the time to get to that countone 不重复element
  • When count is 整数and 大于集合中的元素个数, it will return to the collection全部元素
  • When the count is 负数, the randomly obtained from the collection |count|element, and these 元素可能相同.

Case:

127.0.0.1:6379> sadd num 1 2 3 4 5 6 7 8 9
(integer) 9
127.0.0.1:6379> smembers num
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
9) "9"
127.0.0.1:6379> srandmember num //随机获取一个元素
"4"
127.0.0.1:6379> srandmember num //随机获取一个元素
"9"
127.0.0.1:6379> srandmember num 2 //随机获取2个元素 带参数
1) "5"
2) "7"
127.0.0.1:6379> srandmember num 3 //随机获取3个元素 带参数
1) "2"
2) "7"
3) "4"
127.0.0.1:6379> srandmember num 999 //count参数大于集合中的元素,获得全部元素
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
9) "9"
127.0.0.1:6379> srandmember num -5 //count参数为负数,获取绝对值个元素,这些元素可能相同
1) "8"
2) "8"
3) "6"
4) "8"
5) "4"
127.0.0.1:6379> srandmember num -9
1) "8"
2) "2"
3) "4"
4) "8"
5) "9"
6) "3"
7) "3"
8) "9"
9) "4"
127.0.0.1:6379> srandmember num -11 // count负值大于集合中所有元素
 1) "8"
 2) "3"
 3) "4"
 4) "1"
 5) "6"
 6) "2"
 7) "2"
 8) "1"
 9) "4"
10) "6"
11) "4"

4. Pop an element from the collection

SPOP key

The function of this command is to 左边弹出remove 一个elements from the collection , and then return the value of the element. Because 集合无序, the element that pops up is随机

7.0.0.1:6379> sadd nums 1 2 3 4 5 6 7 8
(integer) 8
127.0.0.1:6379> smembers nums
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
127.0.0.1:6379> spop nums
"3"
127.0.0.1:6379> spop nums
"5"
127.0.0.1:6379> spop nums
"1"

: It can be seen from the above case result that the pop-up element is yes 随机.


Redis Chinese official website: http://www.redis.cn


未完待续,持续更新中...

To be continued, continuing to update...

Guess you like

Origin blog.csdn.net/qq_43073558/article/details/113875846