Redis Learning Note (5)-set

Source: Internet
Author: User

Package Cn.com;import Java.util.hashmap;import Java.util.map;import java.util.set;import Redis.clients.jedis.Jedis /** * All operation Methods of the collection * * */public class Redis_set {public static Jedis Redis = new Jedis ("localhost", 6379);//Connection Redispublic static void Zadd () {map<string,double> map=new hashmap<string,double> (); Redis.zadd ("User", Map);} /** * Adding one or more elements to the collection key, the member element that already exists in the collection is ignored. * */public static void Sadd () {redis.flushdb ();//Clear Data Redis.sadd ("User", "a", "B", "C", "D", "E", "F"); Set<string> setlist=redis.smembers ("user"); for (String s:setlist) {System.out.println (s);}} /** * SCard Key * Returns the cardinality of the collection key (the number of elements in the collection) * */public static void SCard () {redis.flushdb ();//Clear Data Redis.sadd ("User", "a", "B", " C "," D "," E "," F "); Long number=redis.scard ("user"); SYSTEM.OUT.PRINTLN (number);} /** * Sdiff key [key ...] differential set * Returns all members of a collection, which is a set of differences between all given sets. * A nonexistent key is considered an empty set. * Equivalent month user minus user1 remaining user element * */public static void Sdiff () {redis.flushdb ();//Clear Data Redis.sadd ("User", "a", "B", "C", "Z"); Redis.sadd ("User1"A", "C", "D", "F"); Set<string> Setlist=redis.sdiff ("User", "user1"); for (String s:setlist) {System.out.println (s);}} /** * Sdiffstore destination key [key ...] The difference set puts the result into a new union * This command functions like Sdiff, but it saves the result to the destination collection instead of simply returning the result set. * If the destination collection already exists, overwrite it. * Destination can be the key itself. * N is the sum of the number of members for all given sets. * Example: The following example is to remove an element that is not contained in the user in the User1 (ZZZ) does not exist in the User1 * */public static void Sdiffstore () {redis.flushdb ();//Clear Data Redis.sadd ("User", "a", "B", "C", "zzzz"); Redis.sadd ("User1", "a", "B", "C", "D", "E", "F"); Long Size=redis.sdiffstore ("NewUser", "User", "user1"); SYSTEM.OUT.PRINTLN ("Size:" +size); Set<string> setlist=redis.smembers ("NewUser"); for (String s:setlist) {System.out.println (s);}} /** * SINTER key [key ...] asks for intersection * Returns all members of a collection, which is the intersection of all given collections. * A nonexistent key is considered an empty set. * When there is an empty set in a given collection, the result is also an empty set (according to the Law of collection Operations). * */public static void Sinter () {redis.flushdb ();//Clear Data Redis.sadd ("User", "a", "B", "C", "zzzz"), Redis.sadd ("user1", "a" , "B", "C", "D", "E", "F"); Set<string> setlist=redis.sinter ("User", "user1");(String s:setlist) {System.out.println (s);}} /** * SINTER key [key ...] the difference between the intersection and the SINTER is that the result set of the difference is placed in the new array as follows: NewUser * Returns all members of a collection, which is the intersection of all given collections. * A nonexistent key is considered an empty set. * When there is an empty set in a given collection, the result is also an empty set (according to the Law of collection Operations). * */public static void Sinterstore () {redis.flushdb ();//Clear Data Redis.sadd ("User", "a", "B", "C", "zzzz"); Redis.sadd ("User1 "A", "B", "C", "D", "E", "F"), long Size=redis.sinterstore ("NewUser", "User", "user1"); System.out.println ("The size of the intersection:" +size); Set<string> setlist=redis.smembers ("NewUser"); for (String s:setlist) {System.out.println (s);}} /** * sunion key [key ...] returns all members of a collection, which is the set of all the given collections. * A nonexistent key is considered an empty set. * */public static void Sunion () {redis.flushdb ();//Clear Data Redis.sadd ("User", "a", "B", "C", "zzzz"), Redis.sadd ("user1", "a" , "B", "C", "D", "E", "F"); Set<string> setlist=redis.sunion ("User", "user1"); for (String s:setlist) {System.out.println (s);}}  /** * sunion key [key ...] Putting the union of two sets into a new Union NewUser * Returns all members of a collection, which is the Union of all given collections. * A nonexistent key is considered an empty set. * */public static void Sunionstore () {redis.flushdb ();//Clear Data Redis. Sadd ("User", "a", "B", "C", "zzzz"); Redis.sadd ("User1", "a", "B", "C", "D", "E", "F"); long L=redis.sunionstore ("NewUser" , "User", "user1"); Set<string> setlist=redis.smembers ("NewUser"); for (String s:setlist) {System.out.println (s);}} /** * Sismember Key member * Determines whether the member element is a member of the collection key. * */public static void Sismember () {redis.flushdb ();//Clear Data Redis.sadd ("User", "a", "B", "C", "zzzz"); Boolean b= Redis.sismember ("User", "a"); Boolean b1=redis.sismember ("User", "no Oh"); System.out.println ("returns a true and false"); System.out.println (b); System.out.println (B1);} /** * Gets the collection Object * */public static void Smembers () {redis.flushdb ();//Clear Data Redis.sadd ("User", "a", "B", "C", "D", "E", "F"); Set<string> setlist=redis.smembers ("user"); for (String s:setlist) {System.out.println (s);}} /** * Smove Source Destination Member * Smove is an atomic operation. * @param srckey Source Collection key * @param dstkey Target Collection key * @param member element to move * Move element member in source collection to target collection * return value: * If the member element is successful     removed, returns 1. * If the member element is not a member of the source collection and there is no action on the destination collectionLine, then return 0. */public static void Smove () {redis.flushdb ();//Clear Data Redis.sadd ("User", "a", "B", "C", "zzzz"), Redis.sadd ("user1", "D", " E "," F "); Redis.smove (" User "," user1 "," zzzz "); System.out.println ("The remaining elements of ======user should be a,b,c======"); Set<string> setlist=redis.smembers ("user"); for (String s:setlist) {System.out.println (s);} SYSTEM.OUT.PRINTLN ("The element of ======user 1 should be d,e,f,zzz======"); Set<string> setlist1=redis.smembers ("user1"); for (String S:setlist1) {System.out.println (s);}} /** * SPOP Key * Removes and returns a random element in the collection. * You can use the Srandmember command if you want to get only a random element but don't want the element to be removed from the collection. * */public static void Spop () {redis.flushdb ();//Clear Data Redis.sadd ("User", "1", "2", "3", "4", "5", "6", "7"); String s=redis.spop ("user"); SYSTEM.OUT.PRINTLN ("randomly popping and removing elements:" +s); Set<string> setlist=redis.smembers ("user"); for (String str:setlist) {System.out.println (str);}} /** * If the command executes, only the key parameter is supplied, then a random element in the collection is returned. * */public static void Srandmember () {redis.flushdb ();//Clear Data Redis.sadd ("User", "1", "2", "3", "4", "5", "6", "7"); String S=redis.srandmember ("User"); System.out.println ("Random popup element Not removed:" +s); Set<string> setlist=redis.smembers ("user"); for (String str:setlist) {System.out.println (str);}} /** * Srem Key member [member ...] * removes one or more member elements from the collection key, and the nonexistent member element is ignored. * When key is not a collection type, an error is returned. * */public static void Srem () {redis.flushdb ();//Clear Data Redis.sadd ("User", "1", "2", "3", "4", "5", "6", "7"); Redis.srem (" User "," 1 "," 3 "," 5 ");//delete 1,3,5set<string> setlist=redis.smembers (" user "); for (String str:setlist) { System.out.println (str);}} public static void Main (String [] args) {Sunionstore ();}}

  

Redis Learning Note (5)-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.