zoukankan      html  css  js  c++  java
  • (六)Redis之数据结构之sorted-set

    一、常用方法

    • Sorted-Set和Set的区别
    • Sorted-Set中的成员在集合中的位置是有序的
    1. 添加元素
    2. 获得元素
    3. 删除元素
    4. 范围查询

      1和2和3和4  添加/获得/删除元素/范围查询

    package myRedis01;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    import org.junit.After;
    import org.junit.Test;
    
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.Tuple;
    
    public class JedisTest {
    
        Jedis jedis = new Jedis("127.0.0.1", 6379); // 创建客户端 设置IP和端口
    
    
        
        
        /**
         *  添加/获得/删除元素
         */
        @Test
        public void addAndGetAndDel() {
            /**
             * 添加集合期末成绩 元素是平时分,考试,作业 评分分别是80,10,10
             */
            jedis.zadd("期末成绩", 80, "平时分");
            jedis.zadd("期末成绩", 10, "考试");
            jedis.zadd("期末成绩", 10, "作业");
            
            /**
             * 获得元素
             */
            Set<Tuple> sets=jedis.zrangeWithScores("期末成绩", 0, -1);
            for(Tuple t:sets) {
                System.out.println(t.getElement()+"	"+t.getScore());
            }
            System.out.println("==============");
            
            /**
             * 删除元素
             * 删除得分在70~90分的元素
             */
            jedis.zremrangeByScore("期末成绩", 70, 90);
            
            Set<Tuple> sets2=jedis.zrangeWithScores("期末成绩", 0, -1);
            for(Tuple t:sets2) {
                System.out.println(t.getElement()+"	"+t.getScore());
            }
            
        }
        
    
        @After
        public void close() {
            jedis.close();
        }
    
    }

     

  • 相关阅读:
    Leetcode Reverse Words in a String
    topcoder SRM 619 DIV2 GoodCompanyDivTwo
    topcoder SRM 618 DIV2 MovingRooksDiv2
    topcoder SRM 618 DIV2 WritingWords
    topcoder SRM 618 DIV2 LongWordsDiv2
    Zepto Code Rush 2014 A. Feed with Candy
    Zepto Code Rush 2014 B
    Codeforces Round #245 (Div. 2) B
    Codeforces Round #245 (Div. 2) A
    Codeforces Round #247 (Div. 2) B
  • 原文地址:https://www.cnblogs.com/shyroke/p/8007290.html
Copyright © 2011-2022 走看看