示例#1
0
  private void ListOperate() {
    {
      System.out.println("======================list==========================");
      // 清空数据
      System.out.println("清空库中所有数据:" + jedis.flushDB());

      System.out.println("=============增=============");
      shardedJedis.lpush("stringlists", "vector");
      shardedJedis.lpush("stringlists", "ArrayList");
      shardedJedis.lpush("stringlists", "vector");
      shardedJedis.lpush("stringlists", "vector");
      shardedJedis.lpush("stringlists", "LinkedList");
      shardedJedis.lpush("stringlists", "MapList");
      shardedJedis.lpush("stringlists", "SerialList");
      shardedJedis.lpush("stringlists", "HashList");
      shardedJedis.lpush("numberlists", "3");
      shardedJedis.lpush("numberlists", "1");
      shardedJedis.lpush("numberlists", "5");
      shardedJedis.lpush("numberlists", "2");
      System.out.println("所有元素-stringlists:" + shardedJedis.lrange("stringlists", 0, -1));
      System.out.println("所有元素-numberlists:" + shardedJedis.lrange("numberlists", 0, -1));

      System.out.println("=============删=============");
      // 删除列表指定的值 ,第二个参数为删除的个数(有重复时),后add进去的值先被删,类似于出栈
      System.out.println("成功删除指定元素个数-stringlists:" + shardedJedis.lrem("stringlists", 2, "vector"));
      System.out.println("删除指定元素之后-stringlists:" + shardedJedis.lrange("stringlists", 0, -1));
      // 删除区间以外的数据
      System.out.println("删除下标0-3区间之外的元素:" + shardedJedis.ltrim("stringlists", 0, 3));
      System.out.println("删除指定区间之外元素后-stringlists:" + shardedJedis.lrange("stringlists", 0, -1));
      // 列表元素出栈
      System.out.println("出栈元素:" + shardedJedis.lpop("stringlists"));
      System.out.println("元素出栈后-stringlists:" + shardedJedis.lrange("stringlists", 0, -1));

      System.out.println("=============改=============");
      // 修改列表中指定下标的值
      shardedJedis.lset("stringlists", 0, "hello list!");
      System.out.println("下标为0的值修改后-stringlists:" + shardedJedis.lrange("stringlists", 0, -1));
      System.out.println("=============查=============");
      // 数组长度
      System.out.println("长度-stringlists:" + shardedJedis.llen("stringlists"));
      System.out.println("长度-numberlists:" + shardedJedis.llen("numberlists"));
      // 排序
      /*
       * list中存字符串时必须指定参数为alpha,如果不使用SortingParams,而是直接使用sort("list"),
       * 会出现"ERR One or more scores can't be converted into double"
       */
      SortingParams sortingParameters = new SortingParams();
      sortingParameters.alpha();
      sortingParameters.limit(0, 3);
      System.out.println(
          "返回排序后的结果-stringlists:" + shardedJedis.sort("stringlists", sortingParameters));
      System.out.println("返回排序后的结果-numberlists:" + shardedJedis.sort("numberlists"));
      // 子串: start为元素下标,end也为元素下标;-1代表倒数一个元素,-2代表倒数第二个元素
      System.out.println("子串-第二个开始到结束:" + shardedJedis.lrange("stringlists", 1, -1));
      // 获取列表指定下标的值
      System.out.println("获取下标为2的元素:" + shardedJedis.lindex("stringlists", 2) + "\n");
    }
  }
 /**
  * 通过key从对应的list中删除指定的count个 和 value相同的元素
  *
  * @param key
  * @param count 当count为0时删除全部
  * @param value
  * @return 返回被删除的个数
  */
 public Long lrem(String key, long count, String value) {
   ShardedJedis jedis = null;
   Long res = null;
   try {
     jedis = pool.getResource();
     res = jedis.lrem(key, count, value);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
示例#3
0
 /**
  * 从list中删除value
  *
  * @param key
  * @param count 要删除个数
  * @param value
  * @return
  */
 public boolean removeListValue(String key, long count, String value) {
   ShardedJedis shardedJedis = null;
   try {
     shardedJedis = masterShardedJedisPool.getResource();
     shardedJedis.lrem(key, count, value);
     return true;
   } catch (Exception ex) {
     logger.error("getList error.", ex);
     ;
   } finally {
     returnResource(shardedJedis);
   }
   return false;
 }