Set类型的操作命令

Redis中对Set类型的操作命令

一:sadd:添加元素,格式是:sadd set的key item的项值,item项可以有多个。

1 127.0.0.1:6379> sadd set 1 2 3 4
2 (integer) 4
3 127.0.0.1:6379> sadd set 1  //不可重复
4 (integer) 0
5 127.0.0.1:6379> sadd set 5
6 (integer) 1
7 127.0.0.1:6379>

java示例代码:

 1  public static void main(String[] args) {
 2         /**
 3          * redis中对于set类型的: sadd 操作示例
 4          * sadd:添加元素,格式是:sadd set的key item的项值,item项可以有多个。
 5          */
 6         Jedis jedis = new Jedis("127.0.0.1", 6379);
 7 
 8         /**
 9          *  相当于执行 sadd 1 2
10          */
11         Long set = jedis.sadd("set", "1");
12         System.out.println("set = " + set);
13 
14         /**
15          *  相当于执行 sadd 1 2 3 4 5
16          */
17         Long set1 = jedis.sadd("set", "1", "2", "3", "4");
18         System.out.println("set1 = " + set1);
19 
20         
21 
22     }

二:smembers:获取集合中所有元素,格式是:smembers set的key

1 127.0.0.1:6379> smembers set
2 1) "1"
3 2) "2"
4 3) "3"
5 4) "4"
6 5) "5"
7 127.0.0.1:6379>

java示例代码:

     /*
        redis中对于set类型的: smembers 操作示例
        smembers:获取集合中所有元素,格式是:smembers set的key
        相当于执行 smembers key
        */
        Set<String> set2 = jedis.smembers("set");
        System.out.println("set2=" + set2);

三:sismember:判断元素是否在集合中,格式是:sismember set的key item的项值

 1 127.0.0.1:6379> smembers set
 2 1) "1"
 3 2) "2"
 4 3) "3"
 5 4) "4"
 6 5) "5"
 7 127.0.0.1:6379> sismember set 1
 8 (integer) 1
 9 127.0.0.1:6379> sismember set 10
10 (integer) 0
11 127.0.0.1:6379>

java示例代码:

        /*
          redis中对于set类型的: sismember 操作示例
          sismember:判断元素是否在集合中,格式是:sismember set的key item的项值

           相当于执行 sismember set 1
         */
        Boolean sismember = jedis.sismember("set", "1");
        System.out.println("sismember="+sismember);

四:srem:删除元素,格式是:srem set的key item项的值

127.0.0.1:6379> smembers set
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
127.0.0.1:6379> srem set 5
(integer) 1
127.0.0.1:6379> smembers set
1) "1"
2) "2"
3) "3"
4) "4"
127.0.0.1:6379>

java示例代码:

1         /*
2         * srem:删除元素,格式是:srem set的key item项的值
3         * 
4         *  相当于执行 srem set 1
5         * */
6 
7         Long srem = jedis.srem("set", "1");
8         System.out.println("srem="+srem);

五:scard:获取集合中元素的个数,格式是:scard set的key

1 127.0.0.1:6379> smembers set
2 1) "1"
3 2) "2"
4 3) "3"
5 4) "4"
6 127.0.0.1:6379> scard set
7 (integer) 4
8 127.0.0.1:6379>

java示例代码:

1         /*
2         * scard:获取集合中元素的个数,格式是:scard set的key
3         *
4         * 相当于执行 scard set
5         * */
6 
7         Long scard = jedis.scard("set");
8         System.out.println("scard="+scard);

六:srandmember:随机获取集合中的元素,格式是:srandmember set的key[数量]。(数量为正数时,会随机获取这么多个不重复的元素;如果数量大于集合元素个数,返回全部;如果数量为负,会随机获取这么多个元素,可能有重复。)

 1 127.0.0.1:6379> smembers set
 2 1) "-4"
 3 2) "-3"
 4 3) "-2"
 5 4) "-1"
 6 5) "1"
 7 6) "2"
 8 7) "3"
 9 8) "4"
10 127.0.0.1:6379> srandmember set 2
11 1) "3"
12 2) "2"
13 127.0.0.1:6379> srandmember set 10
14 1) "-4"
15 2) "-3"
16 3) "-2"
17 4) "-1"
18 5) "1"
19 6) "2"
20 7) "3"
21 8) "4"
22 127.0.0.1:6379> srandmember set -5
23 1) "-3"
24 2) "-2"
25 3) "2"
26 4) "3"
27 5) "1"
28 127.0.0.1:6379> srandmember set -2
29 1) "-3"
30 2) "-2"
31 127.0.0.1:6379>

java示例代码:

1     /*
2         * srandmember:随机获取集合中的元素,格式是:srandmember set的key[数量]。
3         * (数量为正数时,会随机获取这么多个不重复的元素;如果数量大于集合元素个数,返回全部;如果数量为负,会随机获取这么多个元素,可能有重复。)
4         *
5         * 相当于执行 srandmember set 5
6         * */
7         List<String> srandmember = jedis.srandmember("set", 3);
8         System.out.println(srandmember);

七:spop:弹出元素,格式是:spop set的key

127.0.0.1:6379> smembers set
1) "-4"
2) "-3"
3) "-2"
4) "-1"
5) "1"
6) "2"
7) "3"
8) "4"
127.0.0.1:6379> spop set
"-3"
127.0.0.1:6379> spop set
"-1"
127.0.0.1:6379> spop set
"3"
127.0.0.1:6379> spop set
"4"
127.0.0.1:6379> spop set
"-2"
127.0.0.1:6379> spop set
"-4"
127.0.0.1:6379> spop set
"1"
127.0.0.1:6379> spop set
"2"
127.0.0.1:6379> spop set
(nil)
127.0.0.1:6379> spop set
(nil)
127.0.0.1:6379> spop set
(nil)
127.0.0.1:6379>

java示例代码:

1   /*
2          * spop:弹出元素,格式是:spop set的key
3          *  相当于执行 spop set
4          *
5          * */
6 
7         String spop = jedis.spop("set");
8         System.out.println(spop);

八:smove:移动元素。格式是:smove 源set的key 目的set的key item项的值 (一次只能移动一个item

127.0.0.1:6379> sadd set 1 2 3 4 5 6 7 8 9
(integer) 9
127.0.0.1:6379> smembers set
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
9) "9"
127.0.0.1:6379> smove set set1 1
(integer) 1127.0.0.1:6379> smembers set1
1) "1"
127.0.0.1:6379> smove set set1 1 2
(error) ERR wrong number of arguments for 'smove' command
127.0.0.1:6379>

java示例代码:

1         /*
2         * smove:移动元素。格式是:smove 源set的key 目的set的key item项的值 (一次只能移动一个item)
3         * 相当于执行 smove set set1 item项的值
4         *
5         * */
6 
7         Long smove = jedis.smove("set", "set1", "1");
8         System.out.println(smove);

九:sdiff:差集,返回在第一个set里面而不在后面任何一个set里面的项。格式是:sdiff set的key 用来比较的多个set的key

127.0.0.1:6379> smembers set
1) "2"
2) "3"
3) "4"
4) "5"
5) "6"
6) "7"
7) "8"
8) "9"
127.0.0.1:6379> smove set set1 2
(integer) 1
127.0.0.1:6379> smembers set1
1) "2"
127.0.0.1:6379> smove set set1 3
(integer) 1
127.0.0.1:6379> smembers set1
1) "2"
2) "3"
127.0.0.1:6379> sdiff set set1
1) "4"
2) "5"
3) "6"
4) "7"
5) "8"
6) "9"
127.0.0.1:6379> smove set set2 4
(integer) 1
127.0.0.1:6379> sdiff set set1 set2
1) "5"
2) "6"
3) "7"
4) "8"
5) "9"
127.0.0.1:6379>

java示例代码:

 1      /*
 2          * sdiff:差集,返回在第一个set里面而不在后面任何一个set里面的项。格式是:sdiff set的key 用来比较的多个set的key
 3          * sdiffstore:差集并保留结果,格式是:sdiffstore 存放结果的set的key set的key 用来比较的多个set的key
 4          *
 5          * set:     2 3 4 5 6 7 8 9
 6          * set1:    2 3
 7          * set2:    4
 8          */
 9 
10         /*相当于执行 sdiff set的key 用来比较的多个set的key*/
11         Set<String> sdiff = jedis.sdiff("set", "set1");
12         System.out.println("在set中,但是不在set1中的元素有:" + sdiff.toString());
13 
14         Set<String> sdiff1 = jedis.sdiff("set", "set1", "set2");
15         System.out.println("在set中,但是不在set1、set2中的元素有:" + sdiff1.toString());

sdiffstore:差集并保留结果,格式是:sdiffstore 存放结果的set的key set的key 用来比较的多个set的key

127.0.0.1:6379> smembers set
1) "2"
2) "3"
3) "4"
4) "5"
5) "6"
6) "7"
7) "8"
8) "9"
127.0.0.1:6379> smove set set1 2
(integer) 1
127.0.0.1:6379> smembers set1
1) "2"
127.0.0.1:6379> smove set set1 3
(integer) 1
127.0.0.1:6379> smembers set1
1) "2"
2) "3"
127.0.0.1:6379> sdiff set set1
1) "4"
2) "5"
3) "6"
4) "7"
5) "8"
6) "9"
127.0.0.1:6379> smove set set2 4
(integer) 1
127.0.0.1:6379> sdiff set set1 set2
1) "5"
2) "6"
3) "7"
4) "8"
5) "9"
127.0.0.1:6379> sdiffstore set3 set set1 set2
(integer) 5
127.0.0.1:6379> smembers set3
1) "5"
2) "6"
3) "7"
4) "8"
5) "9"
127.0.0.1:6379>

java示例代码:

1       /*
2          * 相当于执行 sdiffstore 存放结果的set的key set的key 用来比较的多个set的key
3          *
4          * */
5         Long sdiffstore = jedis.sdiffstore("set3", "set", "set1");
6         System.out.println("sdiffstore = " + sdiffstore);
7         Set<String> set3 = jedis.smembers("set3");
8         System.out.println("set3 = " + set3.toString());

十一:sinter:交集,返回多个set里面都有的项。格式是:sinter 多个set的key 

 1 127.0.0.1:6379> smembers set
 2 1) "5"
 3 2) "6"
 4 3) "7"
 5 4) "8"
 6 5) "9"
 7 127.0.0.1:6379> smembers set1
 8 1) "2"
 9 2) "3"
10 127.0.0.1:6379> smembers set2
11 1) "4"
12 127.0.0.1:6379> smembers set3
13 1) "5"
14 2) "6"
15 3) "7"
16 4) "8"
17 5) "9"
18 127.0.0.1:6379> sinter set set1
19 (empty list or set)
20 127.0.0.1:6379> sinter set set1 set2 set 3
21 (empty list or set)
22 127.0.0.1:6379> keys *
23 1) "set1"
24 2) "set"
25 3) "set3"
26 4) "set2"
27 127.0.0.1:6379>

java示例代码:

     /*
         * sinter:交集,返回多个set里面都有的项。格式是:sinter 多个set的key
         * sinterstore:交集并保留结果。格式是:sinterstore 存放结果的key 多个set的key
         *   相当于执行 sinter 多个set的key
         * set:     5 6 7 8 9
         * set1:    2 3
         * set2:    4
         * set3:    5 6 7 8 9 
         */
        Set<String> sinter = jedis.sinter("set", "set1");
        System.out.println("sinter = " + sinter);

        Set<String> sinter2 = jedis.sinter("set", "set1", "set2");
        System.out.println("sinter2 = " + sinter2);

十二:sinterstore:交集并保留结果。格式是:sinterstore 存放结果的key 多个set的key、

 1 127.0.0.1:6379> keys *
 2 1) "set1"
 3 2) "set"
 4 3) "set3"
 5 4) "set2"
 6 127.0.0.1:6379> sinterstore set3 set set2
 7 (integer) 0  说明没有
 8 127.0.0.1:6379> sinterstore set3 set set1
 9 (integer) 0
10 127.0.0.1:6379>

java示例代码:

        /*
         *   相当于执行 sinterstore 存放结果的key 多个set的key
         */

        Long sinterstore = jedis.sinterstore("set3", "set", "set2");
        System.out.println("sinterstore = " + sinterstore);
        Set<String> myset4 = jedis.smembers("myset4");
        System.out.println("set3 = " + set3);

十三:sunion:并集。格式是:sunion 多个set的key

127.0.0.1:6379> smembers set
1) "5"
2) "6"
3) "7"
4) "8"
5) "9"
127.0.0.1:6379> sunion set set1
1) "2"
2) "3"
3) "5"
4) "6"
5) "7"
6) "8"
7) "9"
127.0.0.1:6379> sunion set set1 set2
1) "2"
2) "3"
3) "4"
4) "5"
5) "6"
6) "7"
7) "8"
8) "9"
127.0.0.1:6379> sunion set set1 set2 set3
1) "2"
2) "3"
3) "4"
4) "5"
5) "6"
6) "7"
7) "8"
8) "9"
127.0.0.1:6379>

java示例代码:

       /*
         *
         * sunion:并集。格式是:sunion 多个set的key
         * sunionstore:并集并保留结果。格式是:sunionstore 存放结果的set的key 多个set的key
         *
         * set:     5 6 7 8 9
         * set1:    2 3
         * set2:    4
         * set3:    5 6 7 8 9
         * 相当于执行 sunion 多个set的key
         */
   
        Set<String> sunion = jedis.sunion("set", "set1");
        System.out.println("sunion = " + sunion);

        Set<String> sunion2 = jedis.sunion("set", "set1", "set2");
        System.out.println("sunion2 = " + sunion2);

十四:sunionstore:并集并保留结果。格式是:sunionstore 存放结果的set的key 多个set的key

127.0.0.1:6379> smembers set
1) "5"
2) "6"
3) "7"
4) "8"
5) "9"
127.0.0.1:6379> smembers set1
1) "2"
2) "3"
127.0.0.1:6379> smembers set2
1) "4"
127.0.0.1:6379> smembers set3
(empty list or set)
127.0.0.1:6379> keys *
1) "set1"
2) "set"
3) "set2"
127.0.0.1:6379> sunionstore set4 set set1
(integer) 2
127.0.0.1:6379> smembers set4
1) "2"
2) "3"
127.0.0.1:6379>

java示例代码:

1  /*
2          *  相当于执行 sunionstore 存放结果的set的key 多个set的key
3          */
4         Long sunionstore = jedis.sunionstore("set4", "set", "set1");
5         System.out.println("sunionstore = " + sunionstore);
6         Set<String>  set4 = jedis.smembers("set4");
7         System.out.println("set4 = " + set4);

 

多回头复习复习。。。。。。。。。。。。。。。。。。。。。。。。

 

posted @ 2020-05-12 23:44  愿无违  阅读(424)  评论(0编辑  收藏  举报