1、add(K key, V value, double score)
向指定key中添加元素,按照score值由小到大进行排列
- 集合中对应元素已存在,会被覆盖,包括score
redisTemplate.opsForZSet().add("zSet", "aaa", 1);
redisTemplate.opsForZSet().add("zSet", "bbb", 2);
redisTemplate.opsForZSet().add("zSet", "ccc", 3);
2、add(K key, Set<TypedTuple> tuples)
向指定key中添加元素,按照score值由小到大进行排列
- 集合中对应元素已存在,会被覆盖,包括score
ZSetOperations.TypedTuple<String> objectTypedTuple1 = new DefaultTypedTuple<>("eee",9.6);
ZSetOperations.TypedTuple<String> objectTypedTuple2 = new DefaultTypedTuple<>("fff",1.5);
ZSetOperations.TypedTuple<String> objectTypedTuple3 = new DefaultTypedTuple<>("ggg",7.4);
Set<ZSetOperations.TypedTuple<String>> typles = new HashSet<>();
typles.add(objectTypedTuple1);
typles.add(objectTypedTuple2);
typles.add(objectTypedTuple3);
redisTemplate.opsForZSet().add("zSet", typles);
3、incrementScore(K key, V v1, double delta)
增加key对应的集合中元素v1的score值,并返回增加后的值
- v1不存在,直接新增一个元素
4、score(K key, Object o)
获取key对应集合中o元素的score值
Double score = redisTemplate.opsForZSet().score("zSet", "aaa");
System.out.println("score = " + score);
5、size(K key)
获取集合的大小,地层调用的还是 zCard(K key)
Long zSet = redisTemplate.opsForZSet().size("zSet");
System.out.println("zSet = " + zSet);
6、zCard(K key)
获取集合大小
Long zSet = redisTemplate.opsForZSet().zCard("zSet");
System.out.println("zSet = " + zSet);
7、count(K key, double min, double max)
获取指定score区间里的元素个数
- 包括min、max
8、range(K key, long start, long end)
获取指定下标之间的值
- (0,-1)就是获取全部
Set zSet = redisTemplate.opsForZSet().range("zSet", 0, 2);
System.out.println("zSet = " + zSet);
9、rangeByScore(K key, double min, double max)
获取指定score区间的值
Set zSet = redisTemplate.opsForZSet().rangeByScore("zSet", 1, 5);
System.out.println("zSet = " + zSet);
10、rangeByScore(K key, double min, double max, long offset, long count)
获取指定score区间的值,然后从给定下标和给定长度获取最终值
Set zSet = redisTemplate.opsForZSet().rangeByScore("zSet", 1, 5, 1, 2);
System.out.println("zSet = " + zSet);
11、rank(K key, Object o)
获取指定元素在集合中的索引,索引从0开始
Long rank = redisTemplate.opsForZSet().rank("zSet", "aaa");
System.out.println("rank = " + rank);
12、reverseRank(K key, Object o)
获取倒序排列的索引值,索引从0开始
Long rank = redisTemplate.opsForZSet().reverseRank("zSet", "eee");
System.out.println("rank = " + rank);
13、reverseRange(K key, long start, long end)
逆序获取对应下标的元素
Set zSet = redisTemplate.opsForZSet().reverseRange("zSet", 0, 3);
System.out.println("zSet = " + zSet);
14、remove(K key, Object… values)
移除集合中指定的值
Long remove = redisTemplate.opsForZSet().remove("zSet", "aaa", "bbb");
System.out.println("remove = " + remove);
15、removeRange(K key, long start, long end)
移除指定下标的值
redisTemplate.opsForZSet().removeRange("zSet", 0, 1);
16、removeRangeByScore(K key, double min, double max)
移除指定score区间内的值