RedisTemplate常用集合使用说明-opsForZSet(六)
基礎(chǔ)配置介紹已經(jīng)在前面的《RedisTemplate常用集合使用說明(一)]》中已經(jīng)介紹了,現(xiàn)在我們直接介紹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)
? 獲取變量指定區(qū)間的元素。
?
Set zSetValue = redisTemplate.opsForZSet().range("zSetValue",0,-1); System.out.println("通過range(K key, long start, long end)方法獲取指定區(qū)間的元素:" + zSetValue);3、rangeByLex(K key, RedisZSetCommands.Range range)
用于獲取滿足非score的排序取值。這個排序只有在有相同分?jǐn)?shù)的情況下才能使用,如果有不同的分?jǐn)?shù)則返回值不確定。
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的設(shè)置下標(biāo)開始的長度排序取值。
RedisZSetCommands.Limit limit = new RedisZSetCommands.Limit(); limit.count(2); //起始下標(biāo)為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方式新增數(shù)據(jù)。
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)
根據(jù)設(shè)置的score獲取區(qū)間值。
zSetValue = redisTemplate.opsForZSet().rangeByScore("zSetValue",1,2); System.out.println("通過rangeByScore(K key, double min, double max)方法根據(jù)設(shè)置的score獲取區(qū)間值:" + zSetValue);7、rangeByScore(K key, double min, double max,long offset, long count)
根據(jù)設(shè)置的score獲取區(qū)間值從給定下標(biāo)和給定長度獲取最終值。
zSetValue = redisTemplate.opsForZSet().rangeByScore("zSetValue",1,5,1,3); System.out.println("通過rangeByScore(K key, double min, double max, long offset, long count)方法根據(jù)設(shè)置的score獲取區(qū)間值:" + zSetValue);8、rangeWithScores(K key, long start, long end)
獲取RedisZSetCommands.Tuples的區(qū)間值。
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的區(qū)間值:" + value + "---->" + score ); }9、rangeByScoreWithScores(K key, double min, double max)
獲取RedisZSetCommands.Tuples的區(qū)間值通過分值。
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的區(qū)間值通過分值:" + value + "---->" + score ); }10、rangeByScoreWithScores(K key, double min, double max, long offset, long count)
獲取RedisZSetCommands.Tuples的區(qū)間值從給定下標(biāo)和給定長度獲取最終值通過分值。
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的區(qū)間值從給定下標(biāo)和給定長度獲取最終值通過分值:" + value + "---->" + score ); }11、count(K key, double min, double max)
獲取區(qū)間值的個數(shù)。
long count = redisTemplate.opsForZSet().count("zSetValue",1,5); System.out.println("通過count(K key, double min, double max)方法獲取區(qū)間值的個數(shù):" + count);12、rank(K key, Object o)
獲取變量中元素的索引,下標(biā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)
獲取變量中元素的個數(shù)。
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)
索引倒序排列指定區(qū)間元素。
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)
倒序排列指定分值區(qū)間元素。
zSetValue = redisTemplate.opsForZSet().reverseRangeByScore("zSetValue",1,5); System.out.println("通過reverseRangeByScore(K key, double min, double max)方法倒序排列指定分值區(qū)間元素:" + zSetValue);19、reverseRangeByScore(K key, double min, double max, long offset, long count)
倒序排列從給定下標(biāo)和給定長度分值區(qū)間元素。
zSetValue = redisTemplate.opsForZSet().reverseRangeByScore("zSetValue",1,5,1,2); System.out.println("通過reverseRangeByScore(K key, double min, double max, long offset, long count)方法倒序排列從給定下標(biāo)和給定長度分值區(qū)間元素:" + zSetValue);20、reverseRangeByScoreWithScores(K key, double min, double max)
倒序排序獲取RedisZSetCommands.Tuples的分值區(qū)間值。
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的區(qū)間值:" + value + "---->" + score1 ); }21、reverseRangeByScoreWithScores(K key, double min, double max, long offset, long count)
倒序排序獲取RedisZSetCommands.Tuples的從給定下標(biāo)和給定長度分值區(qū)間值。
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的從給定下標(biāo)和給定長度區(qū)間值:" + value + "---->" + score1 ); }22、reverseRangeWithScores(K key, long start, long end)
索引倒序排列區(qū)間值。
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)方法索引倒序排列區(qū)間值:" + 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)
批量移除元素根據(jù)元素值。
long removeCount = redisTemplate.opsForZSet().remove("unionListSet","A","B"); zSetValue = redisTemplate.opsForZSet().range("unionListSet",0,-1); System.out.print("通過remove(K key, Object... values)方法移除元素的個數(shù):" + removeCount); System.out.println(",移除后剩余的元素:" + zSetValue);29、removeRangeByScore(K key, double min, double max)
根據(jù)分值移除區(qū)間元素。
removeCount = redisTemplate.opsForZSet().removeRangeByScore("unionListSet",3,5); zSetValue = redisTemplate.opsForZSet().range("unionListSet",0,-1); System.out.print("通過removeRangeByScore(K key, double min, double max)方法移除元素的個數(shù):" + removeCount); System.out.println(",移除后剩余的元素:" + zSetValue);30、removeRange(K key, long start, long end)
? 根據(jù)索引值移除區(qū)間元素。
removeCount = redisTemplate.opsForZSet().removeRange("unionListSet",3,5); zSetValue = redisTemplate.opsForZSet().range("unionListSet",0,-1); System.out.print("通過removeRange(K key, long start, long end)方法移除元素的個數(shù):" + removeCount); System.out.println(",移除后剩余的元素:" + zSetValue);? 在此,RedisTemplate.java類相關(guān)的集合操作就介紹完了。需要下載源碼的同學(xué)可以在我上傳的文件里面下載,也可以在https://github.com/422518490/springRedisTest.git這個github上面下載,同時提供了文檔說明在上傳文件里。
總結(jié)
以上是生活随笔為你收集整理的RedisTemplate常用集合使用说明-opsForZSet(六)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: rust(14)-if let,whil
- 下一篇: rust(16)-数组