[Redis data structure] Set set

Source: Internet
Author: User

In redis, there are two Collection types: unordered and non-repeated (SET), and ordered (zset ); this article describes the usage rules of unordered sets. Understanding the set types in redis can be analogous to the set in mathematics. There are three relationships in the Set: intersection, union, and difference set. The concept of difference set is somewhat different. For details, see the syntax usage description.

1. Add Elements

Syntax: sadd [set] [element] example: sadd set1 one sadd set1 one description: Creates a set1 combination and adds one element to the set. If set1 already exists, then, only the element is added. The second addition of the one element fails because the set is unique.

2. Delete Elements

# There are two ways to delete an element: one is to delete a specified element; the other is to delete a specified element at random # syntax for deleting a specified element: Srem [set] [element] example: srem set1 one # delete one element in set1 # randomly delete an element Syntax: spop [set] [element] example: spop set1 # randomly delete an element in the set1 set, and return the deleted element.

3. Intersection

Syntax 1: sinter [set1] [set2]... [setn] example: sinter set1 set2 set3 Description: Get the common elements in three sets # Syntax 1 can get the intersection between sets, but the intersection element set is temporary, if you want to store the intersection result to the specified # set, you can use the syntax 2. syntax 2: sinterstore [storage set] [set1] [set2]... [setn] example: sinterstore set4 set1 set2 set3: storing the intersection results of set1, set2, and set3 in set4

4. Union

# There are two types of syntax rules like intersection. Syntax 1: sunion [set1] [set2]... [setn] syntax 2: sunionstore [set1] [set2]... [setn] example: sunion set1 set2 set3 sunuinstore set1 set2 set3

5. difference set

# There are two types of syntax rules like intersection. Syntax 1: sdiff [set1] [set2]... [setn] syntax 2: sdiffstore [set1] [set2]... [setn] example: sdiff set1 set2 set3 sdiffstore set1 set2: in mathematics, the difference set is a set of elements with the same values, but the former set is different from the latter set. For example: set1 = {1, 2}, set2 = {2, 3}, set3 = {3, 4}, sdiff set1 set2 set3 result is {1 }, that is, the list of different elements in set1, set2, and set3 sets.

6. traverse the set

Syntax: smembers [set] example: smembers set1 # traverses the set1 set and unordered output

7. Move Elements

Syntax: smove [set1] [set2] [element] example: smove set1 set2 one # delete one element in set1 and add it to set2

8. Get the SET SIZE

Syntax: scard [set] example: scard set1 # Get the size of the set1 set

9. Random Element Acquisition

Syntax: srandmember [set] example: srandmember set1 # randomly retrieve an element in the set1 set, but do not delete it

10. determine whether an element is contained.

Syntax: sismember [set] [element] example: sisimember set1 one # judge whether one is an element in set1. If yes, 1 is returned. Otherwise, 0 is returned.


This article from the "Java program" blog, please be sure to keep this source http://793404905.blog.51cto.com/6179428/1549008

[Redis data structure] Set set

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.