Redis Learning Note (4)-list

Source: Internet
Author: User

Package Cn.com;import Java.util.list;import Redis.clients.jedis.jedis;import Redis.clients.jedis.BinaryClient.LIST _position;public class Redis_list {public static Jedis Redis = new Jedis ("localhost", 6379);//Connection redis/** * Returns the specified interval in the list key Within the element, the interval is specified with an offset of start and stop. * */public static void Lrange () {redis.flushdb ();//Clear Data/** Initialize data */redis.lpush ("languages", "中文版", "chain"); Redis.lpush ("Languages", "中文版"); /** Fetch data */list<string> list=redis.lrange ("languages", 0,-1); for (String s:list) {System.out.println ("s:" +s);}} /** * Inserts one or more value values into the table header of the list key * If there are multiple value values, then each value value is inserted into the table header sequentially from left to right: * If key does not exist, an empty list is created and the Lpush operation is performed. * An error is returned when key exists but is not a list type. * */public static void Lpush () {redis.flushdb ();//Clear Data Redis.lpush ("languages", "中文版", "chain"); Redis.lpush (" Languages "," 中文版 ");//Add repeating element Redis.lpush (" languages "," 1 "," 2 "," 3 "); List<string> List=redis.lrange ("languages", 0,-1); for (String s:list) {System.out.println ("s:" +s);}} /** * LPUSHX Key value * Inserts values value into the table header of the list key, when andOnly if key exists and is a list. * As opposed to the Lpush command, when key does not exist, the LPUSHX command does nothing. * */public static void Lpushx () {redis.flushdb ();//Clear Data Redis.lpushx ("languages", "中文版"); List<string> List=redis.lrange ("languages", 0,-1); for (String s:list) {System.out.println ("s:" +s);}} /** * Rpush key value [value ...] * Inserts one or more value values into the footer of the list key (rightmost). * If key does not exist, an empty list is created and the Rpush operation is performed. * An error is returned when key exists but is not a list type. * */public static void Rpush () {redis.flushdb ();//Clear Data Redis.rpush ("languages", "中文版", "chain"); Redis.rpush (" Languages "," 中文版 ");//Add repeating element Redis.rpush (" languages "," 1 "," 2 "," 3 ");//Add repeating element list<string> List=redis.lrange ("Languages", 0,-1); for (String s:list) {System.out.println ("s:" +s);}} /** * LPUSHX Key value * Inserts values value into the tail of the list key when and only if key exists and is a list. * As opposed to the Lpush command, when key does not exist, the LPUSHX command does nothing. * */public static void Rpushx () {redis.flushdb ();//Clear Data Redis.rpush ("Languages", "1", "2", "3");//Add repeating element redis.rpushx (" Languages "," 中文版 "); List<string> List=redis.lrange ("languages", 0,-1); for (String s:lIST) {System.out.println ("s:" +s);}} /** * Remove and return the header element of the list key. * Returns NIL when key does not exist. * */public static void Lpop () {redis.flushdb ();//Clear Data Redis.lpush ("Languages", "1", "2", "3", "4", "5"); String Str=redis.lpop ("languages"); List<string> List=redis.lrange ("languages", 0,-1); SYSTEM.OUT.PRINTLN ("removed element:" +STR); SYSTEM.OUT.PRINTLN ("List of remaining elements:"); for (String s:list) {System.out.print ("" +s+ "");}} /** * Remove and return the tail element of the list key. * Returns NIL when key does not exist. * */public static void Rpop () {redis.flushdb ();//Clear Data Redis.lpush ("Languages", "1", "2", "3", "4", "5"); String Str=redis.rpop ("languages"); List<string> List=redis.lrange ("languages", 0,-1); SYSTEM.OUT.PRINTLN ("removed element:" +STR); SYSTEM.OUT.PRINTLN ("List of remaining elements:"); for (String s:list) {System.out.print ("" +s+ "");}} /** * If there is at least one non-empty list within a given key, then the header element of the first non-empty list encountered, * together with the name of the list to which the popup element belongs, the result is returned to the caller * */public static void Blpop () {REDIS.FLUSHDB ();//Clear Data//redis.lpush ("languages", "a", "B", "C", "D", "E");//Initialize Data Redis.lpush ("base", "a", "B", "C", "D");//Initialize Data list <String> List=redis.blpop (1000, "base"); for (String s:list) {System.out.println (s+ "");} List<string> list2=redis.lrange ("base", 0,-1); for (String s:list2) {System.out.println ("remaining" +s+ "");}} /** * If there is at least one non-empty list within a given key, then the tail element of the first non-empty list that is encountered is popped, and together with the name of the list to which the popup element belongs, the resulting result is returned to the caller * */public static void Brpop () {REDIS.FLUSHDB ();//Clear Data Redis.lpush ("base", "a", "B", "C", "D");//Initialize Data list<string> list=redis.brpop ("base"); for (String S:list) {System.out.println (s+ "");} List<string> list2=redis.lrange ("base", 0,-1); for (String s:list2) {System.out.println ("remaining" +s+ "");}}  /** * POPs an element from the head and puts the data that pops up in the new array with the same method (Rpoplpush) * When the list source is empty, the Brpoplpush command blocks the connection until the wait time-out, or there is another client that performs lpush on the source or Rpush command. * Timeout parameter timeout takes a number in seconds as the value. Setting the timeout parameter to 0 indicates that the blocking time can be extended indefinitely (block indefinitely). * */public static void Brpoplpush () {redis.flushdb ();//Clear Data Redis.lpush ("base", "a", "B", "C", "D");//Initialize data string s= Redis.brpoplpush ("base", "Dest", 0); System.out.println ("s:" +s); List<string> list=redis.lrange ("base", 0,-1); for (String str:list) {SystEm.out.println ("Base list" +str+ "");} List<string> List1=redis.lrange ("Dest", 0,-1); for (String Str:list1) {System.out.println ("dest list:" +str+ "");}} /** * POPs an element from the head and puts the data that pops into the new array * */public static void Rpoplpush () {redis.flushdb ();//Clear Data Redis.lpush ("base", "a", "B", "C" , "D");//Initialize Data string S=redis.rpoplpush ("base", "dest"); System.out.println ("popup element:" +s); List<string> list=redis.lrange ("base", 0,-1); for (String str:list) {System.out.println ("base list" +str+ "");} List<string> List1=redis.lrange ("Dest", 0,-1); for (String Str:list1) {System.out.println ("dest list:" +str+ "");}} /** * Lrem Key Count value * Removes the element in the list that is equal to the parameter value, based on the value of the parameter count. * Count > 0: Searches from the header to the end of the table, removing elements equal to value, count. * Count < 0: Searches from the end of the table to the header, removing the element equal to value, the number is the absolute value of count. * Count = 0: Removes all values that are equal to value in the table. * */public static void Lrem () {redis.flushdb ();//Clear Data Redis.lpush ("base", "a", "B", "C", "D", "E", "F", "G", "H");// Initialize the data long Index=redis.lrem ("base", 0, "a");//Remove the element containing a in the Set System.out.println (index); List<String> list=redis.lrange ("base", 0,-1); System.out.println ("======== remaining element =========="); for (String str:list) {System.out.println (str+ "");}} /** * LSET Key index value * Sets the value of the element with the list key subscript as index to value. * An error is returned when the index parameter is out of range, or an empty list (key does not exist) is LSET. * */public static void LSet () {redis.flushdb ();//Clear Data Redis.lpush ("base", "a", "B", "C", "D", "E", "F", "G", "H");// Initialize the data redis.lset ("base", 6, "2b");//Set the B element to 2blist<string> List=redis.lrange ("base", 0,-1); for (String s:list) { System.out.println (s);}} /** * Let the list keep only the elements within the specified interval, and the elements that are not within the specified range will be deleted. * For example, execute the command LTRIM List 4 6, which means that only the 4 to 6 elements of the list are preserved, and the remaining elements are all deleted. * */public static void LTrim () {redis.flushdb ();//Clear Data String [] s=new string[]{"A", "B", "C", "D", "E", "F", "G", "H"}; Redis.lpush ("base", s);//Initialize Data Redis.ltrim ("base", 4, 6);//value reserved b,c,d corresponding index is 4,5,6 many have been preserved list<string> list= Redis.lrange ("base", 0,-1); for (String str:list) {System.out.println (str);}} /** * Returns the length of the list key. * If key does not exist, then key is interpreted as an empty list, returning 0. * If key is not a list type, returns an error * */public static VOID Llen () {redis.flushdb ();//Clear Data Redis.lpush ("base", "a", "B", "C", "D", "E", "F", "G", "H");//Initialize Data long length= Redis.llen ("base"); System.out.println ("Length:" +length);} /** * Returns the element in the list key, subscript as index. * Subscript (Index) parameter start and stop all base on 0, that is, 0 represents the first element of the list, 1 represents the second element of the list, and so on. * You can also use a negative subscript, 1 for the last element of the list, 2 for the penultimate element of the list, and so on. * If key is not a list type, an error is returned. * */public static void Lindex () {redis.flushdb ();//Clear Data Redis.lpush ("base", "a", "B", "C", "D", "E", "F", "G", "H");// Initializes the data string S=redis.lindex ("base", 2); System.out.println (s);} /** * Linsert Key before| After Pivot value * Inserts a value in the list key, either before or after the value pivot. * No action is taken when pivot does not exist in the list key. Pivot refers to an element in an array * When key does not exist, key is treated as an empty list, and no action is taken. * If key is not a list type, an error is returned. * */public static void Linsert () {redis.flushdb ();//Clear Data Redis.lpush ("base", "a", "B", "C", "D", "E", "F", "G", "H");// Initializes the data Redis.linsert ("base", list_position. Before, "B", "2b");//Insert 2b//redis.linsert before B ("base", list_position. After, "B", "2b");//after B insert 2blist<string> list=redis.lrange ("base", 0,-1);for (String str:list) {System.out.println (str);}} public static void Main (String [] args) {Linsert ();}}

  

Redis Learning Note (4)-list

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.