基础配置介绍已经在前面的《RedisTemplate常用集合使用说明(一)]》中已经介绍了,现在我们直接介绍opsForZSet()方法的使用:

1、add(K key, V value, double score)

添加元素到变量中同时指定元素的分值。

redisTemplate.opsForZSet().add("zSetValue","A",1);
redisTemplate.opsForZSet().add("zSetValue","B",3);
redisTemplate.opsForZSet().add("zSetValue","C",2);
redisTemplate.opsForZSet().add("zSetValue","D",5);

2、range(K key, long start, long end)

​ 获取变量指定区间的元素。

Set zSetValue = redisTemplate.opsForZSet().range("zSetValue",0,-1);
System.out.println("通过range(K key, long start, long end)方法获取指定区间的元素:" + zSetValue);

3、rangeByLex(K key, RedisZSetCommands.Range range)

用于获取满足非score的排序取值。这个排序只有在有相同分数的情况下才能使用,如果有不同的分数则返回值不确定。

RedisZSetCommands.Range range = new RedisZSetCommands.Range();
//range.gt("A");
range.lt("D");
zSetValue = redisTemplate.opsForZSet().rangeByLex("zSetValue", range);
System.out.println("通过rangeByLex(K key, RedisZSetCommands.Range range)方法获取满足非score的排序取值元素:" + zSetValue);

4、rangeByLex(K key, RedisZSetCommands.Range range, RedisZSetCommands.Limit limit)

​ 用于获取满足非score的设置下标开始的长度排序取值。

RedisZSetCommands.Limit limit = new RedisZSetCommands.Limit();
limit.count(2);
//起始下标为0
limit.offset(1);
zSetValue = redisTemplate.opsForZSet().rangeByLex("zSetValue", range,limit);
System.out.println("通过rangeByLex(K key, RedisZSetCommands.Range range, RedisZSetCommands.Limit limit)方法获取满足非score的排序取值元素:" + zSetValue);

5、add(K key, Set<ZSetOperations.TypedTuple> tuples)

通过TypedTuple方式新增数据。

ZSetOperations.TypedTuple<Object> typedTuple1 = new DefaultTypedTuple<Object>("E",6.0);
ZSetOperations.TypedTuple<Object> typedTuple2 = new DefaultTypedTuple<Object>("F",7.0);
ZSetOperations.TypedTuple<Object> typedTuple3 = new DefaultTypedTuple<Object>("G",5.0);
Set<ZSetOperations.TypedTuple<Object>> typedTupleSet = new HashSet<ZSetOperations.TypedTuple<Object>>();
typedTupleSet.add(typedTuple1);
typedTupleSet.add(typedTuple2);
typedTupleSet.add(typedTuple3);
redisTemplate.opsForZSet().add("typedTupleSet",typedTupleSet);
zSetValue = redisTemplate.opsForZSet().range("typedTupleSet",0,-1);
System.out.println("通过add(K key, Set<ZSetOperations.TypedTuple<V>> tuples)方法添加元素:" + zSetValue);

6、rangeByScore(K key, double min, double max)

根据设置的score获取区间值。

zSetValue = redisTemplate.opsForZSet().rangeByScore("zSetValue",1,2);
System.out.println("通过rangeByScore(K key, double min, double max)方法根据设置的score获取区间值:" + zSetValue);

7、rangeByScore(K key, double min, double max,long offset, long count)

根据设置的score获取区间值从给定下标和给定长度获取最终值。

zSetValue = redisTemplate.opsForZSet().rangeByScore("zSetValue",1,5,1,3);
System.out.println("通过rangeByScore(K key, double min, double max, long offset, long count)方法根据设置的score获取区间值:" + zSetValue);

8、rangeWithScores(K key, long start, long end)

获取RedisZSetCommands.Tuples的区间值。

Set<ZSetOperations.TypedTuple<Object>> typedTupleSet = redisTemplate.opsForZSet().rangeWithScores("typedTupleSet",1,3);
Iterator<ZSetOperations.TypedTuple<Object>> iterator = typedTupleSet.iterator();
while (iterator.hasNext()){ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();Object value = typedTuple.getValue();double score = typedTuple.getScore();System.out.println("通过rangeWithScores(K key, long start, long end)方法获取RedisZSetCommands.Tuples的区间值:" + value + "---->" + score );
}

9、rangeByScoreWithScores(K key, double min, double max)

获取RedisZSetCommands.Tuples的区间值通过分值。

Set<ZSetOperations.TypedTuple<Object>> typedTupleSet = redisTemplate.opsForZSet().rangeByScoreWithScores("typedTupleSet",5,8);
iterator = typedTupleSet.iterator();
while (iterator.hasNext()){ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();Object value = typedTuple.getValue();double score = typedTuple.getScore();System.out.println("通过rangeByScoreWithScores(K key, double min, double max)方法获取RedisZSetCommands.Tuples的区间值通过分值:" + value + "---->" + score );
}

10、rangeByScoreWithScores(K key, double min, double max, long offset, long count)

获取RedisZSetCommands.Tuples的区间值从给定下标和给定长度获取最终值通过分值。

Set<ZSetOperations.TypedTuple<Object>> typedTupleSet = redisTemplate.opsForZSet().rangeByScoreWithScores("typedTupleSet",5,8,1,1);
iterator = typedTupleSet.iterator();
while (iterator.hasNext()){ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();Object value = typedTuple.getValue();double score = typedTuple.getScore();System.out.println("通过rangeByScoreWithScores(K key, double min, double max, long offset, long count)方法获取RedisZSetCommands.Tuples的区间值从给定下标和给定长度获取最终值通过分值:" + value + "---->" + score );
}

11、count(K key, double min, double max)

获取区间值的个数。

long count = redisTemplate.opsForZSet().count("zSetValue",1,5);
System.out.println("通过count(K key, double min, double max)方法获取区间值的个数:" + count);

12、rank(K key, Object o)

获取变量中元素的索引,下标开始位置为0。

long index = redisTemplate.opsForZSet().rank("zSetValue","B");
System.out.println("通过rank(K key, Object o)方法获取变量中元素的索引:" + index);

13、scan(K key, ScanOptions options)

​ 匹配获取键值对,ScanOptions.NONE为获取全部键值对;ScanOptions.scanOptions().match(“C”).build()匹配获取键位map1的键值对,不能模糊匹配。

//Cursor<Object> cursor = redisTemplate.opsForSet().scan("setValue", ScanOptions.NONE);
Cursor<ZSetOperations.TypedTuple<Object>> cursor = redisTemplate.opsForZSet().scan("zSetValue", ScanOptions.NONE);
while (cursor.hasNext()){ZSetOperations.TypedTuple<Object> typedTuple = cursor.next();System.out.println("通过scan(K key, ScanOptions options)方法获取匹配元素:" + typedTuple.getValue() + "--->" + typedTuple.getScore());
}

14、score(K key, Object o)

获取元素的分值。

double score = redisTemplate.opsForZSet().score("zSetValue","B");
System.out.println("通过score(K key, Object o)方法获取元素的分值:" + score);

15、zCard(K key)

获取变量中元素的个数。

long zCard = redisTemplate.opsForZSet().zCard("zSetValue");
System.out.println("通过zCard(K key)方法获取变量的长度:" + zCard);

16、incrementScore(K key, V value, double delta)

修改变量中的元素的分值。

double incrementScore = redisTemplate.opsForZSet().incrementScore("zSetValue","C",5);
System.out.print("通过incrementScore(K key, V value, double delta)方法修改变量中的元素的分值:" + incrementScore);
score = redisTemplate.opsForZSet().score("zSetValue","C");
System.out.print(",修改后获取元素的分值:" + score);
zSetValue = redisTemplate.opsForZSet().range("zSetValue",0,-1);
System.out.println(",修改后排序的元素:" + zSetValue);

17、reverseRange(K key, long start, long end)

索引倒序排列指定区间元素。

zSetValue = redisTemplate.opsForZSet().reverseRange("zSetValue",0,-1);
System.out.println("通过reverseRange(K key, long start, long end)方法倒序排列元素:" + zSetValue);

18、reverseRangeByScore(K key, double min, double max)

倒序排列指定分值区间元素。

zSetValue = redisTemplate.opsForZSet().reverseRangeByScore("zSetValue",1,5);
System.out.println("通过reverseRangeByScore(K key, double min, double max)方法倒序排列指定分值区间元素:" + zSetValue);

19、reverseRangeByScore(K key, double min, double max, long offset, long count)

倒序排列从给定下标和给定长度分值区间元素。

zSetValue = redisTemplate.opsForZSet().reverseRangeByScore("zSetValue",1,5,1,2);
System.out.println("通过reverseRangeByScore(K key, double min, double max, long offset, long count)方法倒序排列从给定下标和给定长度分值区间元素:" + zSetValue);

20、reverseRangeByScoreWithScores(K key, double min, double max)

倒序排序获取RedisZSetCommands.Tuples的分值区间值。

Set<ZSetOperations.TypedTuple<Object>> typedTupleSet = redisTemplate.opsForZSet().reverseRangeByScoreWithScores("zSetValue",1,5);
iterator = typedTupleSet.iterator();
while (iterator.hasNext()){ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();Object value = typedTuple.getValue();double score1 = typedTuple.getScore();System.out.println("通过reverseRangeByScoreWithScores(K key, double min, double max)方法倒序排序获取RedisZSetCommands.Tuples的区间值:" + value + "---->" + score1 );
}

21、reverseRangeByScoreWithScores(K key, double min, double max, long offset, long count)

倒序排序获取RedisZSetCommands.Tuples的从给定下标和给定长度分值区间值。

Set<ZSetOperations.TypedTuple<Object>> typedTupleSet = redisTemplate.opsForZSet().reverseRangeByScoreWithScores("zSetValue",1,5,1,2);
iterator = typedTupleSet.iterator();
while (iterator.hasNext()){ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();Object value = typedTuple.getValue();double score1 = typedTuple.getScore();System.out.println("通过reverseRangeByScoreWithScores(K key, double min, double max, long offset, long count)方法倒序排序获取RedisZSetCommands.Tuples的从给定下标和给定长度区间值:" + value + "---->" + score1 );
}

22、reverseRangeWithScores(K key, long start, long end)

索引倒序排列区间值。

Set<ZSetOperations.TypedTuple<Object>> typedTupleSet = redisTemplate.opsForZSet().reverseRangeWithScores("zSetValue",1,5);
iterator = typedTupleSet.iterator();
while (iterator.hasNext()){ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();Object value = typedTuple.getValue();double score1 = typedTuple.getScore();System.out.println("通过reverseRangeWithScores(K key, long start, long end)方法索引倒序排列区间值:" + value + "----->" + score1);
}

23、reverseRank(K key, Object o)

获取倒序排列的索引值。

long reverseRank = redisTemplate.opsForZSet().reverseRank("zSetValue","B");
System.out.println("通过reverseRank(K key, Object o)获取倒序排列的索引值:" + reverseRank);

24、intersectAndStore(K key, K otherKey, K destKey)

获取2个变量的交集存放到第3个变量里面。

redisTemplate.opsForZSet().intersectAndStore("zSetValue","typedTupleSet","intersectSet");
zSetValue = redisTemplate.opsForZSet().range("intersectSet",0,-1);
System.out.println("通过intersectAndStore(K key, K otherKey, K destKey)方法获取2个变量的交集存放到第3个变量里面:" + zSetValue);

25、intersectAndStore(K key, Collection otherKeys, K destKey)

获取多个变量的交集存放到第3个变量里面。

List list = new ArrayList();
list.add("typedTupleSet");
redisTemplate.opsForZSet().intersectAndStore("zSetValue",list,"intersectListSet");
zSetValue = redisTemplate.opsForZSet().range("intersectListSet",0,-1);
System.out.println("通过intersectAndStore(K key, Collection<K> otherKeys, K destKey)方法获取多个变量的交集存放到第3个变量里面:" + zSetValue);

26、unionAndStore(K key, K otherKey, K destKey)

获取2个变量的合集存放到第3个变量里面。

redisTemplate.opsForZSet().unionAndStore("zSetValue","typedTupleSet","unionSet");
zSetValue = redisTemplate.opsForZSet().range("unionSet",0,-1);
System.out.println("通过unionAndStore(K key, K otherKey, K destKey)方法获取2个变量的交集存放到第3个变量里面:" + zSetValue);

27、unionAndStore(K key, Collection otherKeys, K destKey)

获取多个变量的合集存放到第3个变量里面。

redisTemplate.opsForZSet().unionAndStore("zSetValue",list,"unionListSet");
zSetValue = redisTemplate.opsForZSet().range("unionListSet",0,-1);
System.out.println("通过unionAndStore(K key, Collection<K> otherKeys, K destKey)方法获取多个变量的交集存放到第3个变量里面:" + zSetValue);

28、remove(K key, Object… values)

批量移除元素根据元素值。

long removeCount = redisTemplate.opsForZSet().remove("unionListSet","A","B");
zSetValue = redisTemplate.opsForZSet().range("unionListSet",0,-1);
System.out.print("通过remove(K key, Object... values)方法移除元素的个数:" + removeCount);
System.out.println(",移除后剩余的元素:" + zSetValue);

29、removeRangeByScore(K key, double min, double max)

根据分值移除区间元素。

removeCount = redisTemplate.opsForZSet().removeRangeByScore("unionListSet",3,5);
zSetValue = redisTemplate.opsForZSet().range("unionListSet",0,-1);
System.out.print("通过removeRangeByScore(K key, double min, double max)方法移除元素的个数:" + removeCount);
System.out.println(",移除后剩余的元素:" + zSetValue);

30、removeRange(K key, long start, long end)

​ 根据索引值移除区间元素。

removeCount = redisTemplate.opsForZSet().removeRange("unionListSet",3,5);
zSetValue = redisTemplate.opsForZSet().range("unionListSet",0,-1);
System.out.print("通过removeRange(K key, long start, long end)方法移除元素的个数:" + removeCount);
System.out.println(",移除后剩余的元素:" + zSetValue);

​ 在此,RedisTemplate.java类相关的集合操作就介绍完了。需要下载源码的同学可以在我上传的文件里面下载,也可以在https://github.com/422518490/springRedisTest.git这个github上面下载,同时提供了文档说明在上传文件里。

RedisTemplate常用集合使用说明-opsForZSet(六)相关推荐

  1. RedisTemplate常用集合使用说明-opsForSet(五)

    基础配置介绍已经在前面的<RedisTemplate常用集合使用说明(一)>中已经介绍了,现在我们直接介绍opsForSet()方法的使用: 1.add(K key, V- values) ...

  2. RedisTemplate常用集合使用说明-opsForList(三)

    ​ 基础配置介绍已经在前面的<RedisTemplate常用集合使用说明(一)>中已经介绍了,现在我们直接介绍opsForList()方法的使用: 1.leftPush(K key, V ...

  3. RedisTemplate常用集合使用说明-opsForValue(二)

    ​ 基础配置介绍已经在前面的<RedisTemplate常用集合使用说明(一)>中已经介绍了,现在我们直接介绍opsForValue()方法的使用: 1.set(K key, V valu ...

  4. opsforlist 存在贼覆盖_RedisTemplate常用集合使用说明-opsForList(三)

    基础配置介绍已经在前面的<RedisTemplate常用集合使用说明(一)>中已经介绍了,现在我们直接介绍opsForList()方法的使用: 1.leftPush(K key, V va ...

  5. RedisTemplate常用集合之boundValueOps

    一.boundValueOps 简单的Key-Value操作. 1.BoundValueOperations BoundValueOperations就是一个绑定key的对象,我们可以通过这个对象来进 ...

  6. Kotlin学习记录(四)—— 常用集合的使用

    接上篇: Kotlin学习记录(三)-- 子线程获取数据,实现简单ListView 集合在程序中的比例的很重的,Kotlin针对集合进行了更加简洁和有效的封装,这边文章就简单的介绍一下list,map ...

  7. Kotlin中常用集合的使用

    集合在程序中的比例的很重的,Kotlin针对集合进行了更加简洁和有效的封装,这边文章就简单的介绍一下list,map,set这三个最常用集合的使用. 首先在Kotlin中,明确区分了集合的只读和可变. ...

  8. java常用集合详解

    文章目录 一.常用集合大纲 1.常用集合框架及介绍 2.集合和数组的区别 二.Collection 集合(接口) 三.List集合(接口) 1.存储遍历方式 2.ArrayList(实现类) 3.Li ...

  9. C++STL常用集合算法

    C++STL常用集合算法 学习目标 算法简介 set_intersection 功能描述 函数原型 示例 总结 set_union 功能描述 函数原型 示例 总结 set_difference 功能描 ...

最新文章

  1. js 判断是否为null
  2. python3.6.3安装-CentOS7.2安装Python3.6.3
  3. python while函数_详解python while 函数及while和for的区别
  4. 【多线程高并发】深入浅出volatile关键字
  5. 循环录入某学生 5 门课的成绩并计算平均分,如果某分数录入为负,停止录入并提示录入错误
  6. spring mvc学习(36):jstl的jar包的下载
  7. 【原创】大公司为什么还在采用过时的技术
  8. 计算机辅助审计的特点是,浅谈新环境下计算机辅助审计的特点和应用_1
  9. jersey2.22.2异常java.lang.NoSuchMethodError: org.glassfish.jersey.CommonProperties.getValue
  10. Flink系列-实时数仓之Flink实时写入ClickHouse并实时大屏Tableau
  11. el表达式 循环_EL表达式+JSTL+Ajax 047
  12. [转载]DevOps建立全生命周期管理
  13. 使用epublib自动生成epub文件
  14. Excel2016 保存\复制 卡死问题解决
  15. Ternary Tree
  16. android模拟器如何输入中文,不能输入中文
  17. for in 循环详解
  18. ❤️熬夜7天肝出5万字【禅道/缺陷报告/测试报告/接口测试及用例/Fildder】超详细总结❤️
  19. 贾扬清离职 Facebook,即将加盟阿里硅谷研究院
  20. 计算机定时关机教程,电脑定时关机怎么设置

热门文章

  1. php mvc 逻辑层在哪,mvc的业务逻辑应该放哪里?
  2. nginx mysql 查询系统_nginx/mysql查看内存占用
  3. linux重装hal服务安装,linux ubuntu 安装微信客户端
  4. 电脑生成siri语音_米家智能台灯1S全新升级,支持小爱和Siri的语音控制
  5. #39;boost/iterator/iterator_adaptor.hpp#39; file not found之xcode生成时报错的解决方案
  6. JSON.stringify转换Date不正确的解決方法
  7. IEPNGFix:Unclickable children of element 解决办法
  8. 怎样让VB6程序只能运行一次
  9. 1-1.Win10系统利用Pycharm社区版安装Django搭建一个简单Python Web项目的步骤之一
  10. Kubernetes 持久化存储 Cephfs