Jedis对redis的操作详解

转载 2016年05月19日 18:50:50

转:http://blog.csdn.net/u013256816/article/details/51125842


通过前一篇《redis的简单使用》的简单介绍,本篇主要阐述Jedis对redis的五大类型的操作:字符串、列表、散列、集合、有序集合。

JedisUtil

这里的测试用例采用junit4进行运行,准备代码如下:

<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">final</span> String ipAddr = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"10.10.195.112"</span>;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">final</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> port = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6379</span>;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> Jedis jedis= <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>;

    <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@BeforeClass</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">init</span>()
    {
        jedis = JedisUtil.getInstance().getJedis(ipAddr, port);
    }

    <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@AfterClass</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">close</span>()
    {
        JedisUtil.getInstance().closeJedis(jedis,ipAddr, port);
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li></ul>

其中JedisUtil是对jedis做的简单封装,代码如下:

<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> org.apache.log4j.Logger;

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.util.HashMap;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.util.Map;

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> redis.clients.jedis.Jedis;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> redis.clients.jedis.JedisPool;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> redis.clients.jedis.JedisPoolConfig;

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">JedisUtil</span>
{</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> Logger logger = Logger.getLogger(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.getClass().getName());

    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-title" style="box-sizing: border-box;">JedisUtil</span>(){}

    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">RedisUtilHolder</span>{</span>
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">final</span> JedisUtil instance = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> JedisUtil();
    }

    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> JedisUtil <span class="hljs-title" style="box-sizing: border-box;">getInstance</span>(){
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> RedisUtilHolder.instance;
    }

    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> Map<String,JedisPool> maps = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> HashMap<String,JedisPool>();

    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> JedisPool <span class="hljs-title" style="box-sizing: border-box;">getPool</span>(String ip, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> port){
        String key = ip+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">":"</span>+port;
        JedisPool pool = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>;
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(!maps.containsKey(key))
        {
            JedisPoolConfig config = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> JedisPoolConfig();
            config.setMaxActive(RedisConfig.MAX_ACTIVE);
            config.setMaxIdle(RedisConfig.MAX_IDLE);
            config.setMaxWait(RedisConfig.MAX_WAIT);
            config.setTestOnBorrow(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);
            config.setTestOnReturn(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);

            pool = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> JedisPool(config,ip,port,RedisConfig.TIMEOUT);
            maps.put(key, pool);
        }
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>
        {
            pool = maps.get(key);
        }
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> pool;        
    }

    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> Jedis <span class="hljs-title" style="box-sizing: border-box;">getJedis</span>(String ip, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> port)
    {
        Jedis jedis = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>;
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> count = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;
        do
        {
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try</span>
            {
                jedis = getPool(ip,port).getResource();
            }
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (Exception e)
            {
                logger.error(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"get redis master1 failed!"</span>,e);
                getPool(ip,port).returnBrokenResource(jedis);
            }
        }
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span>(jedis == <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span> && count<RedisConfig.RETRY_NUM);
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> jedis;
    }

    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">closeJedis</span>(Jedis jedis, String ip, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> port){
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(jedis != <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>)
        {
            getPool(ip,port).returnResource(jedis);
        }
    }
}

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">RedisConfig</span>
{</span>
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//可用连接实例的最大数目,默认值为8;</span>
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> MAX_ACTIVE = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1024</span>;

    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> MAX_IDLE = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>;

    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> MAX_WAIT = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10000</span>;

    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> TIMEOUT = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10000</span>;

    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> RETRY_NUM = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>;
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li><li style="box-sizing: border-box; padding: 0px 5px;">60</li><li style="box-sizing: border-box; padding: 0px 5px;">61</li><li style="box-sizing: border-box; padding: 0px 5px;">62</li><li style="box-sizing: border-box; padding: 0px 5px;">63</li><li style="box-sizing: border-box; padding: 0px 5px;">64</li><li style="box-sizing: border-box; padding: 0px 5px;">65</li><li style="box-sizing: border-box; padding: 0px 5px;">66</li><li style="box-sizing: border-box; padding: 0px 5px;">67</li><li style="box-sizing: border-box; padding: 0px 5px;">68</li><li style="box-sizing: border-box; padding: 0px 5px;">69</li><li style="box-sizing: border-box; padding: 0px 5px;">70</li><li style="box-sizing: border-box; padding: 0px 5px;">71</li><li style="box-sizing: border-box; padding: 0px 5px;">72</li><li style="box-sizing: border-box; padding: 0px 5px;">73</li><li style="box-sizing: border-box; padding: 0px 5px;">74</li><li style="box-sizing: border-box; padding: 0px 5px;">75</li><li style="box-sizing: border-box; padding: 0px 5px;">76</li><li style="box-sizing: border-box; padding: 0px 5px;">77</li><li style="box-sizing: border-box; padding: 0px 5px;">78</li><li style="box-sizing: border-box; padding: 0px 5px;">79</li><li style="box-sizing: border-box; padding: 0px 5px;">80</li><li style="box-sizing: border-box; padding: 0px 5px;">81</li><li style="box-sizing: border-box; padding: 0px 5px;">82</li><li style="box-sizing: border-box; padding: 0px 5px;">83</li><li style="box-sizing: border-box; padding: 0px 5px;">84</li><li style="box-sizing: border-box; padding: 0px 5px;">85</li><li style="box-sizing: border-box; padding: 0px 5px;">86</li><li style="box-sizing: border-box; padding: 0px 5px;">87</li><li style="box-sizing: border-box; padding: 0px 5px;">88</li><li style="box-sizing: border-box; padding: 0px 5px;">89</li><li style="box-sizing: border-box; padding: 0px 5px;">90</li><li style="box-sizing: border-box; padding: 0px 5px;">91</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li><li style="box-sizing: border-box; padding: 0px 5px;">60</li><li style="box-sizing: border-box; padding: 0px 5px;">61</li><li style="box-sizing: border-box; padding: 0px 5px;">62</li><li style="box-sizing: border-box; padding: 0px 5px;">63</li><li style="box-sizing: border-box; padding: 0px 5px;">64</li><li style="box-sizing: border-box; padding: 0px 5px;">65</li><li style="box-sizing: border-box; padding: 0px 5px;">66</li><li style="box-sizing: border-box; padding: 0px 5px;">67</li><li style="box-sizing: border-box; padding: 0px 5px;">68</li><li style="box-sizing: border-box; padding: 0px 5px;">69</li><li style="box-sizing: border-box; padding: 0px 5px;">70</li><li style="box-sizing: border-box; padding: 0px 5px;">71</li><li style="box-sizing: border-box; padding: 0px 5px;">72</li><li style="box-sizing: border-box; padding: 0px 5px;">73</li><li style="box-sizing: border-box; padding: 0px 5px;">74</li><li style="box-sizing: border-box; padding: 0px 5px;">75</li><li style="box-sizing: border-box; padding: 0px 5px;">76</li><li style="box-sizing: border-box; padding: 0px 5px;">77</li><li style="box-sizing: border-box; padding: 0px 5px;">78</li><li style="box-sizing: border-box; padding: 0px 5px;">79</li><li style="box-sizing: border-box; padding: 0px 5px;">80</li><li style="box-sizing: border-box; padding: 0px 5px;">81</li><li style="box-sizing: border-box; padding: 0px 5px;">82</li><li style="box-sizing: border-box; padding: 0px 5px;">83</li><li style="box-sizing: border-box; padding: 0px 5px;">84</li><li style="box-sizing: border-box; padding: 0px 5px;">85</li><li style="box-sizing: border-box; padding: 0px 5px;">86</li><li style="box-sizing: border-box; padding: 0px 5px;">87</li><li style="box-sizing: border-box; padding: 0px 5px;">88</li><li style="box-sizing: border-box; padding: 0px 5px;">89</li><li style="box-sizing: border-box; padding: 0px 5px;">90</li><li style="box-sizing: border-box; padding: 0px 5px;">91</li></ul>

键操作

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    @Test public void testKey() throws InterruptedException
    {
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"清空数据:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.flushDB</span>())<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"判断某个键是否存在:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.exists</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"username"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"新增<'username','zzh'>的键值对:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.set</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"username"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zzh"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.exists</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"新增<'password','password'>的键值对:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.set</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"password"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"password"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.print</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"系统中所有的键如下:"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Set</span><String> keys = jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.keys</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"*"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(keys)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"删除键password:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.del</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"password"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"判断键password是否存在:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.exists</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"password"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"设置键username的过期时间为5s:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.expire</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"username"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        TimeUnit<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.SECONDS</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sleep</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"查看键username的剩余生存时间:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.ttl</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"username"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"移除键username的生存时间:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.persist</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"username"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"查看键username的剩余生存时间:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.ttl</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"username"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"查看键username所存储的值的类型:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.type</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"username"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li></ul>

输出结果:

<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">清空数据:OK
判断某个键是否存在:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">false</span>
新增<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'username'</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'zzh'</span>>的键值对:OK
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">false</span>
新增<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'password'</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'password'</span>>的键值对:OK
系统中所有的键如下:[username, password]
删除键password:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>
判断键password是否存在:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">false</span>
设置键username的过期时间为<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>s:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>
查看键username的剩余生存时间:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>
移除键username的生存时间:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>
查看键username的剩余生存时间:-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>
查看键username所存储的值的类型:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">string</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>

字符串操作

在Redis里面,字符串可以存储三种类型的值:

  • 字节串(byte string)
  • 整数
  • 浮点数

字节串

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    @Test public void testString() throws InterruptedException
    {
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.flushDB</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"===========增加数据==========="</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.set</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key1"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"value1"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.set</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"value2"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.set</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key3"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"value3"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"删除键key2:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.del</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"获取键key2:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.get</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"修改key1:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.set</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key1"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"value1Changed"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"获取key1的值:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.get</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key1"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"在key3后面加入值:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.append</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key3"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"End"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key3的值:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.get</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key3"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"增加多个键值对:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mset</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key01"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"value01"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key02"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"value02"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key03"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"value03"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"获取多个键值对:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mget</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key01"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key02"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key03"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"获取多个键值对:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mget</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key01"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key02"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key03"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key04"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"删除多个键值对:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.del</span>(new String[]{<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key01"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key02"</span>}))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"获取多个键值对:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mget</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key01"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key02"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key03"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.flushDB</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"===========新增键值对防止覆盖原先值=============="</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setnx</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key1"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"value1"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setnx</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"value2"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setnx</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"value2-new"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.get</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key1"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.get</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"===========新增键值对并设置有效时间============="</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setex</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key3"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"value3"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.get</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key3"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        TimeUnit<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.SECONDS</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sleep</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.get</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key3"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"===========获取原值,更新为新值=========="</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;//GETSET is an atomic set this value and return the old value command.</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getSet</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2GetSet"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.get</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"获得key2的值的字串:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getrange</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li></ul>

输出结果:

<code class="hljs haml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">=<span class="ruby" style="box-sizing: border-box;">==========增加数据===========
</span>OK
OK
OK
删除键key2:1
获取键key2:null
修改key1:OK
获取key1的值:value1Changed
在key3后面加入值:9
key3的值:value3End
增加多个键值对:OK
获取多个键值对:[value01, value02, value03]
获取多个键值对:[value01, value02, value03, null]
删除多个键值对:2
获取多个键值对:[null, null, value03]
=<span class="ruby" style="box-sizing: border-box;">==========新增键值对防止覆盖原先值==============
</span>1
1
0
value1
value2
=<span class="ruby" style="box-sizing: border-box;">==========新增键值对并设置有效时间=============
</span>OK
value3
null
=<span class="ruby" style="box-sizing: border-box;">==========获取原值,更新为新值==========
</span>value2
key2GetSet
获得key2的值的字串:y2G</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li></ul>

memcached和redis同样有append的操作,但是memcached有prepend的操作,redis中并没有。

整数和浮点数

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    @Test public void testNumber()
    {
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.flushDB</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.set</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key1"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"1"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.set</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.set</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key3"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2.3"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key1的值:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.get</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key1"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2的值:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.get</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key1的值加1:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.incr</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key1"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"获取key1的值:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.get</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key1"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2的值减1:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.decr</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"获取key2的值:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.get</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"将key1的值加上整数5:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.incrBy</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key1"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"获取key1的值:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.get</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key1"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"将key2的值减去整数5:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.decrBy</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"获取key2的值:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.get</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li></ul>

输出结果:

<code class="hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">key1的值:1
key2的值:2
key1的值加1:2
获取key1的值:2
key2的值减1:1
获取key2的值:1
将key1的值加上整数5:7
获取key1的值:7
将key2的值减去整数5:-4
获取key2的值:-4</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>

在redis2.6或以上版本中有这个命令:incrbyfloat,即将键存储的值加上浮点数amount,jedis-2.1.0中不支持这一操作。

列表

@Test public void testList()
{
    jedis.flushDB();
    System.out.println("===========添加一个list===========");
    jedis.lpush("collections", "ArrayList", "Vector", "Stack", "HashMap", "WeakHashMap", "LinkedHashMap");
    jedis.lpush("collections", "HashSet");
    jedis.lpush("collections", "TreeSet");
    jedis.lpush("collections", "TreeMap");
    System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1));//-1代表倒数第一个元素,-2代表倒数第二个元素
    System.out.println("collections区间0-3的元素:"+jedis.lrange("collections",0,3));
    System.out.println("===============================");
    // 删除列表指定的值 ,第二个参数为删除的个数(有重复时),后add进去的值先被删,类似于出栈
    System.out.println("删除指定元素个数:"+jedis.lrem("collections", 2, "HashMap"));
    System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1));
    System.out.println("删除下表0-3区间之外的元素:"+jedis.ltrim("collections", 0, 3));
    System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1));
    System.out.println("collections列表出栈(左端):"+jedis.lpop("collections"));
    System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1));
    System.out.println("collections添加元素,从列表右端,与lpush相对应:"+jedis.rpush("collections", "EnumMap"));
    System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1));
    System.out.println("collections列表出栈(右端):"+jedis.rpop("collections"));
    System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1));
    System.out.println("修改collections指定下标1的内容:"+jedis.lset("collections", 1, "LinkedArrayList"));
    System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1));
    System.out.println("===============================");
    System.out.println("collections的长度:"+jedis.llen("collections"));
    System.out.println("获取collections下标为2的元素:"+jedis.lindex("collections", 2));
    System.out.println("===============================");
    jedis.lpush("sortedList", "3","6","2","0","7","4");
    System.out.println("sortedList排序前:"+jedis.lrange("sortedList", 0, -1));
    System.out.println(jedis.sort("sortedList"));
    System.out.println("sortedList排序后:"+jedis.lrange("sortedList", 0, -1));
}

输出结果:

<code class="hljs asciidoc has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">===========添加一个list===========
collections的内容:[TreeMap, TreeSet, HashSet, LinkedHashMap, WeakHashMap, HashMap, Stack, Vector, ArrayList]
collections区间0-3的元素:[TreeMap, TreeSet, HashSet, LinkedHashMap]
===============================
删除指定元素个数:1
collections的内容:[TreeMap, TreeSet, HashSet, LinkedHashMap, WeakHashMap, Stack, Vector, ArrayList]
删除下表0-3区间之外的元素:OK
collections的内容:[TreeMap, TreeSet, HashSet, LinkedHashMap]
collections列表出栈(左端):TreeMap
collections的内容:[TreeSet, HashSet, LinkedHashMap]
collections添加元素,从列表右端,与lpush相对应:4
collections的内容:[TreeSet, HashSet, LinkedHashMap, EnumMap]
collections列表出栈(右端):EnumMap
collections的内容:[TreeSet, HashSet, LinkedHashMap]
修改collections指定下标1的内容:OK
collections的内容:[TreeSet, LinkedArrayList, LinkedHashMap]
===============================
collections的长度:3
<span class="hljs-header" style="box-sizing: border-box;">获取collections下标为2的元素:LinkedHashMap
===============================</span>
sortedList排序前:[4, 7, 0, 2, 6, 3]
<span class="hljs-attribute" style="box-sizing: border-box;">[0, 2, 3, 4, 6, 7]</span>
sortedList排序后:[4, 7, 0, 2, 6, 3]</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li></ul>

Redis中还有阻塞式的列表弹出命令以及在列表之间移动元素的命令:blpop, brpop, rpoplpush, brpoplpush等。

集合(Set)

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    @Test public void testSet()
    {
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.flushDB</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"============向集合中添加元素============"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sadd</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e1"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e2"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e4"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e3"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e0"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e8"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e7"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e5"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sadd</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e6"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sadd</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e6"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet的所有元素为:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.smembers</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"删除一个元素e0:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.srem</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e0"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet的所有元素为:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.smembers</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"删除两个元素e7和e6:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.srem</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e7"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e6"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet的所有元素为:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.smembers</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"随机的移除集合中的一个元素:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.spop</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"随机的移除集合中的一个元素:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.spop</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet的所有元素为:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.smembers</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet中包含元素的个数:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.scard</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e3是否在eleSet中:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sismember</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e3"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e1是否在eleSet中:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sismember</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e1"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e1是否在eleSet中:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sismember</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e5"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"================================="</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sadd</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet1"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e1"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e2"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e4"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e3"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e0"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e8"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e7"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e5"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sadd</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet2"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e1"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e2"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e4"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e3"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e0"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e8"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"将eleSet1中删除e1并存入eleSet3中:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.smove</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet1"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet3"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e1"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"将eleSet1中删除e2并存入eleSet3中:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.smove</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet1"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet3"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"e2"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet1中的元素:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.smembers</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet1"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet3中的元素:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.smembers</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet3"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"============集合运算================="</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet1中的元素:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.smembers</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet1"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet2中的元素:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.smembers</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet2"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet1和eleSet2的交集:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sinter</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet1"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet2"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet1和eleSet2的并集:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sunion</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet1"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet2"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet1和eleSet2的差集:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sdiff</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet1"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eleSet2"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;//eleSet1中有,eleSet2中没有</span>
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li></ul>

输出结果:

<code class="hljs asciidoc has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">============向集合中添加元素============
8
1
0
eleSet的所有元素为:[e3, e4, e1, e2, e0, e8, e7, e6, e5]
删除一个元素e0:1
eleSet的所有元素为:[e3, e4, e1, e2, e8, e7, e6, e5]
删除两个元素e7和e6:2
eleSet的所有元素为:[e3, e4, e1, e2, e8, e5]
随机的移除集合中的一个元素:e5
随机的移除集合中的一个元素:e2
eleSet的所有元素为:[e3, e4, e1, e8]
eleSet中包含元素的个数:4
e3是否在eleSet中:true
e1是否在eleSet中:true
<span class="hljs-header" style="box-sizing: border-box;">e1是否在eleSet中:false
=================================</span>
8
6
将eleSet1中删除e1并存入eleSet3中:1
将eleSet1中删除e2并存入eleSet3中:1
eleSet1中的元素:[e3, e4, e0, e8, e7, e5]
eleSet3中的元素:[e1, e2]
============集合运算=================
eleSet1中的元素:[e3, e4, e0, e8, e7, e5]
eleSet2中的元素:[e3, e4, e1, e2, e0, e8]
eleSet1和eleSet2的交集:[e3, e4, e0, e8]
eleSet1和eleSet2的并集:[e3, e4, e1, e2, e0, e8, e7, e5]
eleSet1和eleSet2的差集:[e7, e5]</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li></ul>

关于Set还有一些其他命令:srandmember, sdiffstore, sinterstore, sunionstore等。

散列

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    @Test public void testHash()
    {
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.flushDB</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        Map<String,String> map = new HashMap<>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        map<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.put</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key1"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"value1"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        map<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.put</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"value2"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        map<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.put</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key3"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"value3"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        map<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.put</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key4"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"value4"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hmset</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hash"</span>,map)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hset</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hash"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key5"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"value5"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"散列hash的所有键值对为:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hgetAll</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hash"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;//return Map<String,String></span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"散列hash的所有键为:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hkeys</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hash"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;//return Set<String></span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"散列hash的所有值为:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hvals</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hash"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;//return List<String></span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"将key6保存的值加上一个整数,如果key6不存在则添加key6:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hincrBy</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hash"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key6"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"散列hash的所有键值对为:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hgetAll</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hash"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"将key6保存的值加上一个整数,如果key6不存在则添加key6:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hincrBy</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hash"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key6"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"散列hash的所有键值对为:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hgetAll</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hash"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"删除一个或者多个键值对:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hdel</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hash"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"散列hash的所有键值对为:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hgetAll</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hash"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"散列hash中键值对的个数:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hlen</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hash"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"判断hash中是否存在key2:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hexists</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hash"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"判断hash中是否存在key3:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hexists</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hash"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key3"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"获取hash中的值:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hmget</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hash"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key3"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"获取hash中的值:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hmget</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hash"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key3"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key4"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li></ul>

输出结果:

<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">散列<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">hash</span>的所有键值对为:{key4=value4, key3=value3, key5=value5, key2=value2, key1=value1}
散列<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">hash</span>的所有键为:[key4, key3, key5, key2, key1]
散列<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">hash</span>的所有值为:[value4, value3, value1, value2, value5]
将key6保存的值加上一个整数,如果key6不存在则添加key6:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>
散列<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">hash</span>的所有键值对为:{key4=value4, key3=value3, key6=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>, key5=value5, key2=value2, key1=value1}
将key6保存的值加上一个整数,如果key6不存在则添加key6:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9</span>
散列<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">hash</span>的所有键值对为:{key4=value4, key3=value3, key6=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9</span>, key5=value5, key2=value2, key1=value1}
删除一个或者多个键值对:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>
散列<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">hash</span>的所有键值对为:{key4=value4, key3=value3, key6=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9</span>, key5=value5, key1=value1}
散列<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">hash</span>中键值对的个数:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>
判断<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">hash</span>中是否存在key2:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">false</span>
判断<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">hash</span>中是否存在key3:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">true</span>
获取<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">hash</span>中的值:[value3]
获取<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">hash</span>中的值:[value3, value4]</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li></ul>

有序集合

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    @Test public void testSortedSet()
    {
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.flushDB</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        Map<Double,String> map = new HashMap<>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        map<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.put</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.2</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        map<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.put</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4.0</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key3"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        map<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.put</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5.0</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key4"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        map<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.put</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.2</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key5"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.zadd</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key1"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.zadd</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset"</span>,map))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset中的所有元素:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.zrange</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset中的所有元素:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.zrangeWithScores</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset中的所有元素:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.zrangeByScore</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset中的所有元素:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.zrangeByScoreWithScores</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset中key2的分值:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.zscore</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset中key2的排名:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.zrank</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"删除zset中的元素key3:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.zrem</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key3"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset中的所有元素:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.zrange</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset中元素的个数:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.zcard</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset中分值在1-4之间的元素的个数:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.zcount</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2的分值加上5:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.zincrby</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key3的分值加上4:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.zincrby</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key3"</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset中的所有元素:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.zrange</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zset"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li></ul>

输出结果:

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">1
4
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">zset</span>中的所有元素:<span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[key5, key2, key1, key3, key4]</span>
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">zset</span>中的所有元素:<span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[[[107, 101, 121, 53]</span>,0<span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.2</span>], <span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[[107, 101, 121, 50]</span>,1<span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.2</span>], <span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[[107, 101, 121, 49]</span>,3<span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.0</span>], <span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[[107, 101, 121, 51]</span>,4<span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.0</span>], <span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[[107, 101, 121, 52]</span>,5<span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.0</span>]]
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">zset</span>中的所有元素:<span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[key5, key2, key1, key3, key4]</span>
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">zset</span>中的所有元素:<span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[[[107, 101, 121, 53]</span>,0<span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.2</span>], <span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[[107, 101, 121, 50]</span>,1<span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.2</span>], <span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[[107, 101, 121, 49]</span>,3<span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.0</span>], <span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[[107, 101, 121, 51]</span>,4<span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.0</span>], <span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[[107, 101, 121, 52]</span>,5<span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.0</span>]]
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">zset</span>中<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">key2</span>的分值:1<span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.2</span>
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">zset</span>中<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">key2</span>的排名:1
删除<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">zset</span>中的元素<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">key3</span>:1
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">zset</span>中的所有元素:<span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[key5, key2, key1, key4]</span>
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">zset</span>中元素的个数:4
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">zset</span>中分值在1<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">-4</span>之间的元素的个数:2
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">key2</span>的分值加上5:6<span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.2</span>
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">key3</span>的分值加上4:4<span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.0</span>
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">zset</span>中的所有元素:<span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[key5, key1, key3, key4, key2]</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li></ul>

有序集合还有诸如zinterstore, zunionstore, zremrangebyscore, zremrangebyrank, zrevrank, zrevrange, zrangebyscore等命令。

排序sort

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    @Test public void testSort()
    {
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.flushDB</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.lpush</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"collections"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ArrayList"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Vector"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Stack"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"HashMap"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"WeakHashMap"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"LinkedHashMap"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"collections的内容:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.lrange</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"collections"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        SortingParams sortingParameters = new SortingParams()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sort</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"collections"</span>,sortingParameters<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.alpha</span>()))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"==============================="</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.lpush</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sortedList"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"3"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"6"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"0"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"7"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"4"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sortedList排序前:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.lrange</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sortedList"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"升序:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sort</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sortedList"</span>, sortingParameters<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.asc</span>()))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"升序:"</span>+jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sort</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sortedList"</span>, sortingParameters<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.desc</span>()))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"==============================="</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.lpush</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"userlist"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"33"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;  </span>
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.lpush</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"userlist"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"22"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;  </span>
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.lpush</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"userlist"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"55"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;  </span>
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.lpush</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"userlist"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"11"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;  </span>
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hset</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"user:66"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"66"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;  </span>
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hset</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"user:55"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"55"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;  </span>
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hset</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"user:33"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"33"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;  </span>
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hset</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"user:22"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"79"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;  </span>
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hset</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"user:11"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"24"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;  </span>
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hset</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"user:11"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"add"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"beijing"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;  </span>
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hset</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"user:22"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"add"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"shanghai"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;  </span>
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hset</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"user:33"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"add"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"guangzhou"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;  </span>
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hset</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"user:55"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"add"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"chongqing"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;  </span>
        jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.hset</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"user:66"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"add"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"xi'an"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;  </span>
        sortingParameters = new SortingParams()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        sortingParameters<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.get</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"user:*->name"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;  </span>
        sortingParameters<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.get</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"user:*->add"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; </span>
        System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(jedis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sort</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"userlist"</span>,sortingParameters))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li></ul>

输出结果:

<code class="hljs asciidoc has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">collections的内容:[LinkedHashMap, WeakHashMap, HashMap, Stack, Vector, ArrayList]
<span class="hljs-attribute" style="box-sizing: border-box;">[ArrayList, HashMap, LinkedHashMap, Stack, Vector, WeakHashMap]</span>
===============================
sortedList排序前:[4, 7, 0, 2, 6, 3]
升序:[0, 2, 3, 4, 6, 7]
升序:[7, 6, 4, 3, 2, 0]
===============================
<span class="hljs-attribute" style="box-sizing: border-box;">[24, beijing, 79, shanghai, 33, guangzhou, 55, chongqing]</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>

参考资料 
1. java操作redis 
2. java对redis的基本操作 
3. 《Redis In Action》Josiah L. Carlson 
4. redis的简单使用


相关文章推荐

Jedis对Redis五大类型操作简单介绍

这里的测试用例采用junit4进行运行,准备代码如下:private static final String ipAddr = "10.10.195.112"; private static fina...

在Java中使用Jedis操作Redis,在高并发的情况下,应用卡死、报无法获取连接错误的处理方式

1.JedisUtil:工具类,单例,避免获取多个jedisPool 对象 package JedisTest; import org.apache.commons.lang.StringUtils...

Jedis对redis的操作详解

通过前一篇《redis的简单使用》的简单介绍,本篇主要阐述Jedis对redis的五大类型的操作:字符串、列表、散列、集合、有序集合。JedisUtil这里的测试用例采用junit4进行运行,准备代码...

java JedisUtil 工具类连接redis服务器

redis 大大提高服务器常用数据的查询效率,减轻了数据库的并发压力,合理的运用redis有助于项目的优化和完善。

redis客户端之jedis 操作工具类

jedis 操作工具类 包括连接池的使用及 jedis对string 、set、sortset、list、hash的所有常规操作。 package com.jedis.util; impor...

java redis使用之利用jedis实现redis消息队列

应用场景 最近在公司做项目,需要对聊天内容进行存储,考虑到数据库查询的IO连接数高、连接频繁的因素,决定利用缓存做。 从网上了解到redis可以对所有的内容进行二进制的存储,而java是可以对所有对象...

CSDN日报20170828——《4个方法快速打造你的阅读清单》

程序人生 | 4个方法快速打造你的阅读清单作者:foruok 下面提供四种方法,无论你是否经常读书,都可以使用它们快速构建起你的阅读清单: 从问题到图书 信息缺口 关联 跟...

java操作Redis数据库的redis工具,RedisUtil,jedis工具JedisUtil,JedisPoolUtil

java操作Redis数据库的redis工具,RedisUtil,jedis工具JedisUtil,JedisPoolUtil,自己抽空写的,基本只要理解什么是get,什么是set就可以使用Redis...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)