Spring中使用RedisTemplate操作Redis(spring(7)

使用:template.opsForSet().move("setTest","aaa","setTest2");
      System.out.println(template.opsForSet().members("setTest"));
      System.out.println(template.opsForSet().members("setTest2"));
结果:[ccc]
[aaa]

•Long size(K key);
无序集合的大小长度

使用:System.out.println(template.opsForSet().size("setTest"));
结果:1

•Boolean isMember(K key, Object o);
判断 member 元素是否是集合 key 的成员

使用:System.out.println(template.opsForSet().isMember("setTest","ccc"));
      System.out.println(template.opsForSet().isMember("setTest","asd"));
结果:true
false

•Set<V> intersect(K key, K otherKey);
key对应的无序集合与otherKey对应的无序集合求交集

使用:System.out.println(template.opsForSet().members("setTest"));
      System.out.println(template.opsForSet().members("setTest2"));
      System.out.println(template.opsForSet().intersect("setTest","setTest2"));
结果:[aaa, ccc]
[aaa]
[aaa]

•Set<V> intersect(K key, Collection<K> otherKeys);
key对应的无序集合与多个otherKey对应的无序集合求交集

使用:System.out.println(template.opsForSet().members("setTest"));
        System.out.println(template.opsForSet().members("setTest2"));
        System.out.println(template.opsForSet().members("setTest3"));
        List<String> strlist = new ArrayList<String>();
        strlist.add("setTest2");
        strlist.add("setTest3");
        System.out.println(template.opsForSet().intersect("setTest",strlist));
结果:[aaa, ccc]
[aaa]
[ccc, aaa]
[aaa]

•Long intersectAndStore(K key, K otherKey, K destKey);
key无序集合与otherkey无序集合的交集存储到destKey无序集合中

使用:System.out.println("setTest:" + template.opsForSet().members("setTest"));
System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
System.out.println(template.opsForSet().intersectAndStore("setTest","setTest2","destKey1"));
System.out.println(template.opsForSet().members("destKey1"));
结果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
2
[aaa, ccc]

•Long intersectAndStore(K key, Collection<K> otherKeys, K destKey);
key对应的无序集合与多个otherKey对应的无序集合求交集存储到destKey无序集合中

使用:System.out.println("setTest:" + template.opsForSet().members("setTest"));
      System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
      System.out.println("setTest3:" + template.opsForSet().members("setTest3"));
      List<String> strlist = new ArrayList<String>();
      strlist.add("setTest2");
      strlist.add("setTest3");
      System.out.println(template.opsForSet().intersectAndStore("setTest",strlist,"destKey2"));
      System.out.println(template.opsForSet().members("destKey2"));
结果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
setTest3:[ccc, aaa]
2
[aaa, ccc]

•Set<V> union(K key, K otherKey);
key无序集合与otherKey无序集合的并集

使用:System.out.println("setTest:" + template.opsForSet().members("setTest"));
      System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
      System.out.println(template.opsForSet().union("setTest","setTest2"));
结果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
[ccc, aaa, ddd, bbb]

•Set<V> union(K key, Collection<K> otherKeys);
key无序集合与多个otherKey无序集合的并集

使用:System.out.println("setTest:" + template.opsForSet().members("setTest"));
      System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
      System.out.println("setTest3:" + template.opsForSet().members("setTest3"));
      List<String> strlist = new ArrayList<String>();
      strlist.add("setTest2");
      strlist.add("setTest3");
      System.out.println(template.opsForSet().union("setTest",strlist));
结果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
setTest3:[xxx, ccc, aaa]
[ddd, xxx, bbb, aaa, ccc]

•Long unionAndStore(K key, K otherKey, K destKey);
key无序集合与otherkey无序集合的并集存储到destKey无序集合中

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/8f96c330d6318f32beb9e4557005b934.html