Redis Learning Note (6)-sortedset

Source: Internet
Author: User

Package Cn.com;import Java.util.hashmap;import java.util.map;import java.util.set;import Redis.clients.jedis.Jedis; Import redis.clients.jedis.tuple;/** * All operation Methods of an ordered set * * */public class Redis_sortedset {public static Jedis Redis = new Je Dis ("localhost", 6379);//Connect Redis/** * Zadd key score member [[Score member] [SCORE member] ...] * One or more member elements and their SCO The RE value is added to the ordered set key. * If a member is already a member of an ordered set, update the member score value and ensure that the member is in the correct position by re-inserting the member element. * The score value can be an integer value or a double-precision floating-point number. * If key does not exist, an empty ordered set is created and the Zadd operation is performed. * An error is returned when key exists but is not an ordered set type. * */public static void Zadd () {redis.flushdb ();//Clear Data map<string,double> agemap=new hashmap<string,double> (); Agemap.put ("Age1", 1.0), Agemap.put ("Age2", 2.0), Agemap.put ("Age4", 4.0), Agemap.put ("Age3", 3.0); Agemap.put (" Age9 ", 9.0); Agemap.put (" Age5 ", 5.0); Redis.zadd (" User ", Agemap); Set<string> list=redis.zrange ("user", 0,-1); for (String s:list) {System.out.println (s);}} /** * Returns the cardinality of the ordered set key. * */public static void Zcard () {Redis.fluSHDB ();//Clear Data map<string,double> agemap=new hashmap<string,double> (); Agemap.put ("Age1", 1.0); Agemap.put ("Age2", 2.0), Agemap.put ("Age4", 4.0), Agemap.put ("Age3", 3.0), Agemap.put ("Age9", 9.0), Agemap.put ("Age5", 5.0); Redis.zadd ("User", Agemap); Long length=redis.zcard ("user"); System.out.println (length);} /** * Zcount key min Max * Returns the number of members in an ordered set key, where the score value is between Min and Max (the default includes the score value equals min or max). * */public static void Zcount () {redis.flushdb ();//Clear Data map<string,double> agemap=new hashmap<string,double > (); Agemap.put ("Age1", 1.0); Agemap.put ("Age2", 2.0); Agemap.put ("Age4", 4.0); Agemap.put ("Age3", 3.0); Agemap.put ( "Age9", 9.0), Agemap.put ("Age5", 5.0); Redis.zadd ("User", Agemap); Long number=redis.zcount ("user", 5,9); SYSTEM.OUT.PRINTLN (number);} /** * Zincrby Key Increment member * is the member value of the member score of the ordered set key plus the increment increment. * Can be passed a negative value increment, let score minus the corresponding value, such as Zincrby Key-5 member, is to let member score value minus 5. * When key does not exist, or member is not a member of key, Zincrby key increment member Equivalent to Zadd key increment member. * When key is not an ordered set type, an error is returned. The *score value can be an integer value or a double-precision floating-point number * */public static void Zincrby () {redis.flushdb ();//Clear Data map<string,double> agemap=new Hashmap<string,double> (); Agemap.put ("Age1", 1.0); Agemap.put ("Age2", 2.0); Agemap.put ("Age4", 4.0); agemap.put ("Age3", 3.0), Agemap.put ("Age9", 9.0), Agemap.put ("Age5", 5.0); Redis.zadd ("User", Agemap); Redis.zincrby ("User", 55, " Age5 "); Set<string> list=redis.zrange ("user", 0,-1); for (String s:list) {System.out.println (s);}} /** * Zinterstore Destination Numkeys key [key ...] [WEIGHTS weight [weight ...] [AGGREGATE sum| min| MAX] * Computes the intersection of one or more ordered sets given, where the number of a given key must be specified with the Numkeys parameter and the intersection (result set) is stored to destination. * By default, the score value of a member in the result set is the sum of the score values for that member under all given sets. * */public static void Zinterstore () {redis.flushdb ();//Clear Data Redis.zadd ("User", 1, "Age1"); Redis.zadd ("User", 3, "Age3") ; Redis.zadd ("User", 6, "Age6"), Redis.zadd ("User", 2, "Age2"), Redis.zadd ("User", 8, "Age8"), Redis.zadd ("user1", 11, " Age1 "); Redis.zadd (" User1 "," Age3Redis.zadd ("user1", "Age6"), Redis.zadd ("user1", "Age2"), Redis.zadd ("User1", and "Age8"); Redis.zadd ("User1 "Age9"); Redis.zinterstore ("Newset", New string[]{"user", "user1"}); Set<string> List=redis.zrange ("Newset", 0,-1); for (String s:list) {System.out.println (s);}} /** * Returns the ordered set key, where all score values are between Min and Max (including equals min or Max) members.  Ordered set members are arranged in ascending order of score values (from small to large).  * The optional limit parameter specifies the number and interval of results returned (just like SELECT LIMIT offset, count in SQL), note that when offset is large, the operation that locates offset may need to traverse the entire ordered set, which is the worst-case complication of O (N) time. * The optional Withscores parameter determines whether the result set returns only the members of the ordered set, or the ordered set members and their score values. * */public static void Zrangebyscore () {redis.flushdb ();//Clear Data Redis.zadd ("User", 1, "Age1"), Redis.zadd ("User", 11, " Age11 "), Redis.zadd (" User ", 111," age111 "), Redis.zadd (" User ", 3," Age3 "), Redis.zadd (" User "," age33 "); Redis.zadd (" User ", 333," age333 "), Redis.zadd (" User ", 6," Age6 "), Redis.zadd (" User "," Age6 "), Redis.zadd (" User ", 666," age666 "); Redis.zadd ("User", 2, "Age2"), Redis.zadd ("User", "Age22"), Redis.zadd ("User", 222, "Age222 "); Redis.zadd (" User ", 8," Age8 "), Redis.zadd (" User ", 9," Age9 "), Redis.zadd (" User ", 9," Age9 "); Set<string> list=redis.zrangebyscore ("User", (+), "for" (String s:list) {System.out.println (s);}} /** * Zrank Key Member * Returns the rank of the member member in the ordered set key. Where ordered set members are arranged in ascending order of score values (from small to large). * Rankings are based on 0, which means that the members with the lowest score value rank 0. * */public static void rand () {redis.flushdb (); Redis.zadd ("User", 1, "Age1"), Redis.zadd ("User", One, "Age11"); Redis.zadd ("User", 111, "age111"), Long Number=redis.zrank ("User", "Age11"),//number display bit 1 Low name is starting from 0 Age11 ranked first System.out.println ( number);}  /** * Zrem key member [member ...] * Remove one or more members from an ordered set key, and non-existent members will be ignored. * When key exists but is not an ordered set type, an error is returned * */public static void Zrem () {redis.flushdb () redis.zadd ("User", 1, "Age1"), Redis.zadd ("User" , 2, "Age2"), Redis.zadd ("User", 3, "Age3"), Redis.zadd ("User", 4, "Age4"), Redis.zadd ("User", 5, "Age5"), Redis.zrem (" User "," Age2 "," age5 "); Set<string> list=redis.zrange ("user", 0,-1); for (String s:list) {System.out.println (s);}} /** * ranked in Stat-endThe tuple between (STRT and end refers to the rank in the collection is not score) * Tuple contains key and score * */public static void Zrangewithscores () {redis.flushdb () ; Redis.zadd ("User", One, "Age1"), Redis.zadd ("User", "Age2"), Redis.zadd ("User", "Age3"), Redis.zadd ("User", 44, " Age4 "); Redis.zadd (" User "," age5 "); set<tuple> list= redis.zrangewithscores ("user", 0, 2); for (Tuple s:list) {System.out.println (s.getelement () + " Store: "+s.getscore ());}} /** * Rank the tuple between stat-end (STRT and end refers to the stored score) * Tuple contains key and score * */public static void Zrangebyscorewithscores () {redis.flushdb (); Redis.zadd ("User", One, "Age1"), Redis.zadd ("User", "Age2"), Redis.zadd ("User", "Age3"); Redis.zadd ("User", "Age4"), Redis.zadd ("User", "age5"); Set<tuple> list=redis.zrangebyscorewithscores ("user", 0, +); for (Tuple s:list) {System.out.println ( S.getelement () + "store:" +s.getscore ());}} /** * Remove the element between the sort number Strt-end * */public static void Zremrangebyrank () {redis.flushdb (); Redis.zadd ("User", One, "age1"); Redis . Zadd ("User", page, "AGE2 "), Redis.zadd (" User ", 333," age33 "), Redis.zadd (" User "," Age4 "), Redis.zadd (" User "," Age5 "), Redis.zadd (" user "); "Age6"), Redis.zadd ("User", "Age7"), Redis.zremrangebyrank ("user", 0, 3); Set<string> list=redis.zrange ("user", 0,-1); for (String s:list) {System.out.println (s);}} /** * Reverse Permutation set * @param key (max Max score) (min min score) * */public static void Zrevrangebyscore () {redis.flushdb (); Redis.zadd ( "User", One, "Age1"), Redis.zadd ("User", "Age2"), Redis.zadd ("User", 333, "age33"), Redis.zadd ("User", "age4"); Redis.zadd ("User", "age5"); Set<string> list=redis.zrevrangebyscore ("user", 0); for (String s:list) {System.out.println (s);}} /** * Reverse take score range max-min between elements * */public static void Zrevrangebyscorewithscores () {redis.flushdb (); Redis.zadd ("User", 11 , "Age1"), Redis.zadd ("User", "Age2"), Redis.zadd ("User", 333, "age33"), Redis.zadd ("User", "age4"); Redis.zadd (" User "," Age5 "), Redis.zadd (" User "," Age6 "), Redis.zadd (" User "," Age7 "); Set<tuple>List=redis.zrevrangebyscorewithscores ("User", S:list), for (Tuple) {System.out.println (s.getelement () + "store: "+s.getscore ());}} /** * Reverse Permutation set * @param key star-end to sort range * */public static void Zrevrangewithscores () {redis.flushdb (); Redis.zadd ("User", 1 1, "Age1"); Redis.zadd ("User", "Age2"), Redis.zadd ("User", 333, "age33"), Redis.zadd ("User", "age4"); Redis.zadd ( "User", "age5"); Set<tuple> list=redis.zrevrangewithscores ("user", 0, 3); for (Tuple s:list) {System.out.println (s.getelement () + "Score:" +s.getscore ());}} /** * Delete the value of key in score value between Star-end * */public static void Zremrangebyscore () {redis.flushdb (); Redis.zadd ("User", one, "AG E1 "), Redis.zadd (" User "," Age2 "), Redis.zadd (" User "," Age3 "), Redis.zadd (" User "," Age4 "), Redis.zadd (" User ", "Age5"); Redis.zremrangebyscore ("User", 0, 22); Set<string> list=redis.zrange ("user", 0,-1); for (String s:list) {System.out.println (s);}} /** * Returns the rank value in member in key * */public static void Zrevrank () {redis.flushdb (); Redis.zadD ("User", One, "Age1"), Redis.zadd ("User", "Age2"), Redis.zadd ("User", "Age3"), Redis.zadd ("User", "age4"); Redis.zadd ("User", "age5"), Long L=redis.zrevrank ("User", "Age3"); System.out.println (l); }/** * Returns the member in the ordered set key, within the specified interval. * The positions of the members are sorted by increasing the score value (from small to large). * Members with the same score value are sorted by dictionary order (lexicographical order). * */public static void Zrange () {redis.flushdb ();//Clear Data map<string,double> agemap=new hashmap<string,double > (); Agemap.put ("Age1", 1.0); Agemap.put ("Age2", 2.0); Agemap.put ("Age4", 4.0); Agemap.put ("Age3", 3.0); Agemap.put ( "Age9", 9.0), Agemap.put ("Age5", 5.0), Redis.zadd ("user", Agemap), Redis.zincrby ("User", "age5"); Set<string> list=redis.zrange ("user", 0,-1); for (String s:list) {System.out.println (s);}} /** * For the set of two sets * */public static void Zunionstore () {redis.flushdb ();//Clear Data Redis.zadd ("User", 1, "Age1"); Redis.zadd (" User ", 3," Age3 "), Redis.zadd (" User ", 6," Age6 "), Redis.zadd (" User ", 2," Age2 "), Redis.zadd (" User "," Age10 "); Redis.zadd ("User1", One, "Age1"), Redis.Zadd ("user1", "Age3"), Redis.zadd ("user1", "Age6"), Redis.zadd ("user1", "Age2"); Redis.zadd ("User1", 88, " Age8 "), Redis.zadd (" user1 "," Age9 "), Redis.zunionstore (" Newset ", New string[]{" user "," user1 "}); Set<string> List=redis.zrange ("Newset", 0,-1); for (String s:list) {System.out.println (s);}} public static void Main (String [] args) {zrevrangebyscorewithscores ();}}

  

Redis Learning Note (6)-sortedset

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.