Redis集合数据结构操作Set|ZSet

112 阅读4分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第19天,点击查看活动详情

Redis的Set是string类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。Redis 中 集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1)。

ZSet有序集合,和无序集合一样也是string类型元素的集合,且不允许重复的成员。不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。 有序集合的成员是唯一的,但分数(score)却可以重复。

Set操作

Set提供SetOperations操作对象

public interface SetOperations<K,V>
SetOperations提供了对无序集合的一系列操作:

SetOperations<String, Object> setOperations = redisTemplate.opsForSet();

往Set集合添加元素,可以添加集合元素

  • Long add(K key, Object… values);
setOperations.add("settttttt", "aaa", "bbb", "ccc");
setOperations.add("settttthh", Lists.newArrayList("1", "2","3","4","5", "ccc"),Lists.newArrayList("1"),Lists.newArrayList("hhh"));
  • Set members(K key);
Set<Object> set = setOperations.members("settttttt");
System.out.println("settttttt 所有成员 members:"+set);

//随机获取key无序集合中的一个元素

  • V randomMember(K key);
Object randomO = setOperations.randomMember("settttttt");
System.out.println("随机获取key无序集合中的一个元素:"+randomO);

//获取多个key无序集合中的元素(去重),count表示个数 //结果可能小个需要的个数

  • Set distinctRandomMembers(K key, long count);
Set<Object> randomOs = setOperations.distinctRandomMembers("settttttt", 2);
System.out.println("随机获取多个key无序集合中的元素(去重):"+randomOs);

//获取多个key无序集合中的元素,count表示个数-未去重

  • List randomMembers(K key, long count);
List<Object> randomOss = setOperations.randomMembers("settttttt", 2);
System.out.println("随机获取多个key无序集合中的元素(不去重):"+randomOss);

//删除某个元素

  • Long remove(K key, Object... values);
Long re = setOperations.remove("settttthh","1");
System.out.println("删除标识:re:"+re+" 删除后数据"+setOperations.members("settttthh"));

//pop出一个随机元素 V pop(K key); //pop出count个随机元素 List pop(K key, long count);

Object o = setOperations.pop("settttttt");
System.out.println("pop :"+o);
System.out.println("pop after:"+setOperations.members("settttttt"));

//将 value 元素从 source 集合移动到 destination 集合

  • Boolean move(K key, V value, K destKey);
setOperations.add("soureKey","source","acv","abc","ccc");
Boolean bool = setOperations.move("soureKey", "abc", "destinationKey");
System.out.println("soureKey move 移走了:"+setOperations.members("soureKey"));
System.out.println("destinationKey 收到了:"+setOperations.members("destinationKey"));

//获取Set集合元素个数

  • Long size(K key);
Long size = setOperations.size("settttthh");
System.out.println("settttthh size:"+size);

//判断o是不是集合元素

  • Boolean isMember(K key, Object o);
Boolean b = setOperations.isMember("settttthh", "1");
System.out.println("1 是不是 settttthh 的元素:"+b);

//计算集合的交集 //key对应的无序集合与otherKey对应的无序集合求交集

  • Set intersect(K key, K otherKey);
  • Set intersect(K key, Collection otherKeys);
  • Set intersect(Collection keys);
Set<Object> setInt = setOperations.intersect("settttthh","settttttt");
System.out.println("settttthh和settttttt的交集:"+setInt);

Set<Object> setInts = setOperations.intersect("settttthh", Lists.newArrayList("settttttt","soureKey"));
System.out.println("settttthh和settttttt and soureKey的交集:"+setInts);

//计算集合的交集并输出到一个新的集合

  • Long intersectAndStore(K key, K otherKey, K destKey);
  • Long intersectAndStore(K key, Collection otherKeys, K destKey);
  • Long intersectAndStore(Collection keys, K destKey);
Long inter1 = setOperations.intersectAndStore("settttthh","settttttt","newDestKey2");
System.out.println("交集输出到新key操作:"+inter1+"   newDestKey2 MEMBERS :"+setOperations.members("newDestKey2"));

Long inter2 = setOperations.intersectAndStore("settttthh",Lists.newArrayList("settttttt","soureKey"),"newDestKey");
System.out.println("多个key交集输出到新key操作:"+inter2+"   newDestKey MEMBERS :"+setOperations.members("newDestKey"));

//计算两个集合的差集

  • Set difference(K key, K otherKey);
  • Set difference(K key, Collection otherKeys);
  • Set difference(Collection keys);
Set<Object> setDiff = setOperations.difference("settttthh","settttttt");
System.out.println("settttthh和settttttt的差集:"+setDiff);

Set<Object> setDiffs = setOperations.difference("settttthh", Lists.newArrayList("settttttt","soureKey"));
System.out.println("settttthh和settttttt and soureKey的差集:"+setDiffs);

计算两个集合的差集并输出到新的集合

  • Long differenceAndStore(K key, K otherKey, K destKey);
  • Long differenceAndStore(K key, Collection otherKeys, K destKey);
  • Long differenceAndStore(Collection keys, K destKey);

  
Long diff1 = setOperations.differenceAndStore("settttthh","settttttt","newDestKey2");
System.out.println("差集输出到新key操作:"+diff1 +"   newDestKey2 MEMBERS :"+setOperations.members("newDestKey2"));

Long diff2 = setOperations.differenceAndStore("settttthh",Lists.newArrayList("settttttt","soureKey"),"newDestKey");
System.out.println("多个key差集输出到新key操作:"+diff2+"   newDestKey MEMBERS :"+setOperations.members("newDestKey"));

//计算集合的并集

  • Set union(K key, K otherKey);
  • Set union(K key, Collection otherKeys);
  • Set union(Collection keys);
Set<Object> setUnion = setOperations.union("settttthh","settttttt");
System.out.println("settttthh和settttttt的并集:"+setUnion);

Set<Object> setUnions = setOperations.union("settttthh", Lists.newArrayList("settttttt","soureKey"));
System.out.println("settttthh和settttttt and soureKey的并集:"+setUnions);

//计算多个集合的并集并输出到新的集合

  • Long unionAndStore(K key, K otherKey, K destKey);
  • Long unionAndStore(K key, Collection otherKeys, K destKey);
  • Long unionAndStore(Collection keys, K destKey);
Long union1 = setOperations.unionAndStore("settttthh","settttttt","newDestKey2");
System.out.println("并集输出到新key操作:"+union1+"   newDestKey2 MEMBERS :"+setOperations.members("newDestKey2"));

Long union2 = setOperations.unionAndStore("settttthh",Lists.newArrayList("settttttt","soureKey"),"newDestKey");
System.out.println("多个key并集输出到新key操作:"+union2+"   newDestKey MEMBERS :"+setOperations.members("newDestKey"));

使用Cursor在key的hash中迭代,相当于迭代器。

  • Cursor scan(K key, ScanOptions options);
Cursor<Object> curosr = redisTemplate.opsForSet().scan("settttttt", ScanOptions.NONE);
while(curosr.hasNext()){
   System.out.println(curosr.next());
}

Zset操作,添加有序集合元素

有序集合操作对象:

ZSetOperations zSetOperations = redisTemplate.opsForZSet();

  • public interface ZSetOperations<K,V> ZSetOperations提供了一系列方法对有序集合进行操作:

  • Boolean add(K key, V value, double score); 新增一个有序集合,存在的话为false,不存在的话为true

zSetOperations.add("zsetttttttt", "zzz", 1.0);
zSetOperations.add("zsetttttttt", "zza", 6.0);
zSetOperations.add("zsetttttttt", "zzb", 4.0);
zSetOperations.add("zsetttttttt", "zzc", 2.0);


ZSetOperations.TypedTuple<Object> objectTypedTuple1 = new DefaultTypedTuple<Object>("zset-5",9.6);
ZSetOperations.TypedTuple<Object> objectTypedTuple2 = new DefaultTypedTuple<Object>("zset-6",9.9);
Set<ZSetOperations.TypedTuple<Object>> tuples = new HashSet<ZSetOperations.TypedTuple<Object>>();
tuples.add(objectTypedTuple1);
tuples.add(objectTypedTuple2);
System.out.println(zSetOperations.add("zsetttttttt",tuples));
System.out.println(zSetOperations.range("zsetttttttt",0,-1));