1. 程式人生 > >set集合類型的操作(無序集合)——redis

set集合類型的操作(無序集合)——redis

1.10 nan 127.0.0.1 第一次 ati 存儲 宋體 判斷 tom

1. set集合類型的操作(無序集合)

集合具有無序性(沒有順序)、確定性(描述是確定的)、唯一性(沒有重復的元素)

1.1. sadd key member [member ...]

語法:sadd key member [member ...]

作用:往集合key中添加元素

返回值:添加元素的個數

例:

127.0.0.1:6379> sadd fruit apple banana pear

(integer) 3

127.0.0.1:6379> sadd num 1 1

(integer) 1

1.2. smembers key

語法:smembers key

作用:返回集合

key中所有的元素

例:

127.0.0.1:6379> smembers fruit

1) "pear"

2) "banana"

3) "apple"

127.0.0.1:6379> smembers num

1) "1"

1.3. srandmember key [count]

語法:srandmember key [count]

作用:返回集合key中隨機的1個元素

例:

127.0.0.1:6379> smembers letter

1) "d"

2) "g"

3) "c"

4) "e"

5) "a"

127.0.0.1:6379> srandmember letter

"e"

127.0.0.1:6379> srandmember letter

"c"

127.0.0.1:6379> srandmember letter

"a"

127.0.0.1:6379> srandmember letter 2

1) "c"

2) "e"

127.0.0.1:6379> srandmember letter 3

1) "c"

2) "e"

3) "a"

127.0.0.1:6379> smembers letter

1) "d"

2) "g"

3) "c"

4) "e"

5) "a"

1.4. srem key member [member ...]

語法:

srem key member [member ...]

作用:刪除集合key中的member [member...]元素

返回值:忽略不存在的元素後,真正刪除掉的元素的個數

例:

127.0.0.1:6379> smembers num

1) "1"

127.0.0.1:6379> srem num 1

(integer) 1

127.0.0.1:6379> smembers num

(empty list or set)

127.0.0.1:6379> srem fruit pear

(integer) 1

127.0.0.1:6379> smembers fruit

1) "banana"

2) "apple"

1.5. spop key [count]

語法:spop key [count]

作用:返回並刪除集合key1個隨機的元素

例:

127.0.0.1:6379> sadd letter a b c d e f g

(integer) 7

127.0.0.1:6379> smembers letter

1) "c"

2) "d"

3) "g"

4) "b"

5) "e"

6) "a"

7) "f"

127.0.0.1:6379> spop letter

"b"

127.0.0.1:6379> smembers letter

1) "d"

2) "g"

3) "c"

4) "e"

5) "a"

6) "f"

127.0.0.1:6379> spop letter

"f"

127.0.0.1:6379> smembers letter

1) "d"

2) "g"

3) "c"

4) "e"

5) "a"

案例:

spop是返回並刪除一個隨機的元素,所以可以用來做抽獎,第一次spop出的可以作為第一名(一等獎),第二次spop出的可以作為第二名(二等獎)...

1.6. sismember key member

語法:sismember key member

作用:判斷member是否是集合key中的元素

返回值:是返回1;不是返回0

例:

127.0.0.1:6379> smembers letter

1) "d"

2) "g"

3) "c"

4) "e"

5) "a"

127.0.0.1:6379> sismember letter a

(integer) 1

127.0.0.1:6379> sismember letter k

(integer) 0

127.0.0.1:6379> sismember letter 0

(integer) 0

1.7. scard key

語法:scard key

作用:返回集合key中元素的個數

例:

127.0.0.1:6379> smembers letter

1) "d"

2) "g"

3) "c"

4) "e"

5) "a"

127.0.0.1:6379> scard letter

(integer) 5

1.8. sinter key [key ...]

語法:sinter key [key ...]

作用:求出key [key ...]幾個集合中的交集,並返回

例:

127.0.0.1:6379> sadd lisa c python java

(integer) 3

127.0.0.1:6379> sadd wang english python php

(integer) 3

127.0.0.1:6379> sadd poly python redis c++

(integer) 3

127.0.0.1:6379> sinter lisa wang poly

1) "python"

127.0.0.1:6379> sadd tom python java php redis

(integer) 4

127.0.0.1:6379> sinter tom poly

1) "redis"

2) "python"

1.9. sinterstore destination key [key ...]

語法:sinterstore destination key [key ...]

作用:求出key [key ...]幾個集合中的交集,並賦給destination或存儲在destination

返回值:返回真正存儲到destination中的元素的個數

例:

127.0.0.1:6379> smembers lisa

1) "python"

2) "java"

3) "c"

127.0.0.1:6379> smembers poly

1) "c++"

2) "redis"

3) "python"

127.0.0.1:6379> smembers tom

1) "redis"

2) "php"

3) "python"

4) "java"

127.0.0.1:6379> sinterstore lisa_tom lisa tom

(integer) 2

127.0.0.1:6379> smembers lisa_tom

1) "python"

2) "java"

1.10. sunion key [key ...]

語法:sunion key [key ...]

作用:求出key [key ...]幾個集合的並集,並返回

例:

127.0.0.1:6379> sadd lisa c python java

(integer) 3

127.0.0.1:6379> sadd wang english python php

(integer) 3

127.0.0.1:6379> sadd poly python redis c++

(integer) 3

127.0.0.1:6379> sunion lisa wang

1) "python"

2) "c"

3) "java"

4) "php"

5) "english"

127.0.0.1:6379> sadd tom python java php redis

(integer) 4

127.0.0.1:6379> sunion lisa wang tom poly

1) "php"

2) "c"

3) "c++"

4) "redis"

5) "python"

6) "english"

7) "java"

1.11. sdiff key [key ...]

語法:sdiff key [key ...]

作用:求出key[key ...]的差集

例:

127.0.0.1:6379> smembers lisa

1) "python"

2) "java"

3) "c"

127.0.0.1:6379> smembers tom

1) "redis"

2) "php"

3) "python"

4) "java"

127.0.0.1:6379> sdiff lisa tom

1) "c"

127.0.0.1:6379> sdiff tom lisa

1) "redis"

2) "php"

127.0.0.1:6379> smembers poly

1) "c++"

2) "redis"

3) "python"

127.0.0.1:6379> sdiff tom lisa poly

1) "php"

set集合類型的操作(無序集合)——redis