A set data structure is a collection of unordered, unique elements. It is similar to a set in mathematics. The set data structure is often used to implement other data structures like queues and stacks.
Here are some operations performed on a set:
Operation | Element |
---|---|
Add | 1 |
Add | 2 |
Add | 3 |
Remove | 3 |
Remove | 2 |
Remove | 1 |
This command adds the specified member to the set.
sadd user:1 "subham"
sadd user:1 "codexam"
sadd user:1 "subham"
smembers user:1
1) "subham"
2) "codexam"
Visualize the set in the Redis Commander.
Index | Element |
---|---|
0 | subham |
1 | codexam |
This command removes the specified member from the set.
sadd user:1 "subham"
sadd user:1 "codexam"
srem user:1 "subham"
smembers user:1
1) "codexam"
This command checks if the specified member is present in the set.
sadd user:1 "subham"
sadd user:1 "codexam"
sismember user:1 "subham"
(integer) 1
This command returns all the members of the set.
sadd user:1 "subham"
sadd user:1 "codexam"
smembers user:1
1) "subham"
2) "codexam"
This command removes and returns a random member from the set.
sadd user:1 "subham"
sadd user:1 "codexam"
spop user:1
"codexam"
This command returns a random member from the set.
sadd user:1 "subham"
sadd user:1 "codexam"
srandmember user:1
"codexam"
This command moves the specified member from one set to another.
sadd user:1 "subham"
sadd user:1 "codexam"
sadd user:2 "xamcodexam"
smove user:1 user:2 "subham"
smembers user:1
1) "codexam"
smembers user:2
1) "xamcodexam"
2) "subham"
This command returns the number of members in the set.
sadd user:1 "subham"
sadd user:1 "codexam"
scard user:1
(integer) 2
This command returns the intersection of all the sets specified.
sadd user:1 "subham"
sadd user:1 "codexam"
sadd user:2 "xamcodexam"
sinter user:1 user:2
1) "subham"
This command returns the union of all the sets specified.
sadd user:1 "subham"
sadd user:1 "codexam"
sadd user:2 "xamcodexam"
sunion user:1 user:2
1) "subham"
2) "codexam"
3) "xamcodexam"
This command returns the difference between the first set and all the other sets specified.
sadd user:1 "subham"
sadd user:1 "codexam"
sadd user:2 "xamcodexam"
sdiff user:1 user:2
1) "codexam"
This command stores the intersection of all the sets specified.
sadd user:1 "subham"
sadd user:1 "codexam"
sadd user:2 "xamcodexam"
sinterstore user:3 user:1 user:2
(integer) 1
smembers user:3
1) "subham"
This command stores the union of all the sets specified.
sadd user:1 "subham"
sadd user:1 "codexam"
sadd user:2 "xamcodexam"
sunionstore user:3 user:1 user:2
(integer) 3
smembers user:3
1) "subham"
2) "codexam"
3) "xamcodexam"
This command stores the difference between the first set and all the other sets specified.
sadd user:1 "subham"
sadd user:1 "codexam"
sadd user:2 "xamcodexam"
sdiffstore user:3 user:1 user:2
(integer) 1
This command scans the set for members matching the specified pattern.
sadd user:1 "subham"
sadd user:1 "codexam"
sscan user:1 0 match "sub*"
1) "0"
2) 1) "subham"