[Redis data structure] zset (ordered set)

Source: Internet
Author: User

In the previous article, we talked about the command for using unordered set sets. This article describes the command and instance for using zset (ordered set. An element of zset consists of the weight and value. The weight indicates the position, that is, the order of the reason.

1. If an ordered set element is added

Syntax: zadd [zset set] [weight (numerical type)] [element value] # by default, the weight is sorted from small to large.

Example:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/48/DE/wKioL1QL1QPy3yVZAAGXIfsMlwc523.jpg "Title =" zadd example "alt =" wkiol1ql1qpy3yvzaagxifsmlwc523.jpg "/>

Note: In an unordered set, sadd an existing element does not exist. Although an existing element cannot be added to zset, however, the new element weight can be modified.

For example:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/48/DD/wKiom1QL127haxyXAAD3c_MzIXk277.jpg "Title =" zadd-01.png "alt =" wkiom1ql1_haxyxaad3c_mzixk277.jpg "/>

2. If you delete an element

Syntax: zrem [zset set]

Example:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/48/DD/wKiom1QL1q2goWWeAAAwEFt25cY401.jpg "Title =" zrem example "alt =" wkiom1ql1q2gowweaaaweft25cy401.jpg "/>

 3. Traverse Ordered Sets

# Sort weights from small to large traversal Syntax 1: zrange [zset set] [start Index] [end Index] <withscores> # Sort weights from large to small traversal syntax 2: zrevrange [zset set] [start Index] [end Index] <withscores>

Example:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/48/DF/wKioL1QL2IPgjiWYAAD_gDcrVyA783.jpg "Title =" zrange.png "alt =" wkiol1ql2ipgjiwyaad_gdcrvya783.jpg "/>

Description: The syntax includes withscores, which indicates the display weight.

4. Auto-increment/auto-increment element weight

Syntax: zincrby [zset set] [step size] [element] # weight description of the returned element: If the element does not exist, add the element and set the weight to the specified step size, otherwise, the weight of the modified element is the result of auto-increment or auto-increment.

Example: Modify the weight of element 51cto2 to 7.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/48/DF/wKioL1QL2d6z4bxNAADLpSUFrLI503.jpg "Title =" zincrby.png "alt =" wkiol1ql2d6z4bxnaadlpsufrli503.jpg "/>

 

 5. Obtain the index position of an element.

# Indexing (from small to large) in ascending order of weights Syntax 1: zrank [zset set] [element] # obtain the index location of a specified Element # retrieve the index in descending order of weight (from large to small) syntax 2: zrevrank [zset set] [element]

Example: The index values of 51cto2 are obtained in descending and ascending order respectively (the index starts from 0)

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/48/DD/wKiom1QL2vPSEUTgAAE3yXYpo9A483.jpg "Title =" zrank.png "alt =" wkiom1ql2vpseutgaae3yxypo9a483.jpg "/>

6. Obtain the element list of a weight interval.

Syntax: zrangebyscore [zset set] [start weight] [end weight] <withscores> Description: gets the element of a weight range. The withscores option is consistent with zrange.

Example: Get elements with a weight between 3 and 4

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/48/DD/wKiom1QL3DGSrJhYAAFbkWNIe1o790.jpg "Title =" zrangebyscore.png "alt =" wkiom1ql3dgsrjhyaafbkwnie1o790.jpg "/>

7. Obtain the number of elements in a weight interval.

Syntax: zcount [zset set] [start weight] [end weight] # number of returned Elements

Example: obtain the number of elements with weights between 3 and 4.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/48/DD/wKiom1QL3TWAv8ZQAADiKltuSaA038.jpg "Title =" zcount.png "alt =" wkiom1ql3twav8zqaadikltusaa038.jpg "/>

8. Obtain the weight of a specified element.

Syntax: zscore [zset set] [element] # returns the weight value of an element.

Example: Get the weight of 51cto2

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/48/E1/wKioL1QL7Y2xbyfLAAAwf50SdQ8462.jpg "Title =" zscore.png "alt =" wkiol1ql7y2xbyflaaawf50sdq8462.jpg "/>

 9. Get the SET SIZE

Syntax: zcard [zset set] # returns the size of the element in the set.

Example: Get the number of elements in the set1 set

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/48/DF/wKiom1QL7ZPgJtNFAABENov7rmg105.jpg "Title =" zcardpng.png "alt =" wkiom1ql7zpgjtnfaabenov7rmg105.jpg "/>

 10. Batch Delete

# Batch Delete syntax by index location 1: zremrangebyrank [zset set] [start Index] [end Index] # batch Delete syntax by weight interval 2: zremrangebyscore [zset set] [start Index] [end Index]

Example:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/48/E2/wKioL1QL8FCA3gU_AAJBi1gc5mk246.jpg "Title =" zremrange.png "alt =" wkiol1ql8fca3gu_aajbi1gc5mk246.jpg "/>

                            

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

[Redis data structure] zset (ordered 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.