4. Data type two: Lists

Source: Internet
Author: User

1, about the form of list

A list data type (Lists) can store an ordered list of strings , add elements to the list two paragraphs in a common operation, or get a fragment of a list. The underlying implementation of the list type is a doubly linked list (double linked list), so the time complexity of adding elements to both ends of the list is O (1), and reading both ends of the element is also very fast. Similarly, the closer the element is to the middle position, the slower the reading is.

The list data types are organized as follows:

Figure 1. List Organization Chart

Use a doubly linked list (two-way Arrow) to organize many strings into a list (square brackets).

2, List of command set

Lpush key value [value ...] Lpushx key Valuerpush key value [value ...] Rpushx key Valuelpop keyblpop key [key ...] timeoutrpop keybrpop key [key ...] Timeoutrpoplpush source Destinationbrpoplpu SH source Destination Timeoutlinsert Key before| After pivot Valuelrem key count Valuelindex key Indexlset key index Valuellen keylrange key start stop

①,lpush key value [value ...]/ rpush key value [value ...]/ lpop k EY/ rpop key

function: similar to the stack and stack operation, can only be done separately from the two methods. Note that the empty list key does not exist, that is, when all the value in the list is popped by the pop operation, key is automatically deleted.

complexity of Time: O (1)

Example

②,lpushx key value/ rpushx key value

function: first check whether the key exists before lpush or rpush, or do nothing if there is a push operation for value.

complexity of Time: O (1)

Example

③,blpop key [key ...] Timeout/   brpop key [key ...] Timeout

function: blocking pop-up command. If the key does not exist, wait until the timeout is reached.

time complexity : O (1), same as Lpop or rpop time without blocking.

Example:

The first client detects that the list does not exist, using the blocked Blpop list 300, which is blocked for 300 seconds. Because list is empty, all command lines do not return:

Push a list in the second client, and the list has values:

Once a value appears in the list, it returns:

④,rpoplpush source destination/ brpoplpush Source Destination timeout

function:source pops an element through the Rpop command and presses the element through Lpush into the destination. Brpoplpush is a blocking command.

complexity of Time: O (1)

Example:

⑤,linsert key before| After pivot value

function : Inserts a value into the list and inserts the position reference pivot. If key does not exist, no action is made. Returns 1 if pivot does not exist

time complexity : O (n), where N is the number of value to look for before locating a pivot

Example:

⑥,lrem key count value

function: removes the first count value in the key list that is equal to the value of the element. Count Value:

Count > 0, delete elements from left to right before count values equal to value

Count < 0, delete the right-to-left first count value equals the element of value

Count = 0, removes all elements in the list that are equal to value

time complexity : O (n), n indicates the length of the list.

Example:

⑦,LSET key index value

function : Sets the value of the element in the list at index position to value.

time complexity : O (n), n is the length of the list. If you set the first element or the last element, the time complexity is O (1).

Example:

4. Data type two: Lists

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.