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

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

•Long unionAndStore(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().unionAndStore("setTest",strlist,"unionAndStoreTest2"));
      System.out.println("unionAndStoreTest2:" + template.opsForSet().members("unionAndStoreTest2"));
结果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
setTest3:[xxx, ccc, aaa]
5
unionAndStoreTest2:[ddd, xxx, bbb, aaa, ccc]

•Set<V> difference(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().difference("setTest","setTest2"));
结果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
[bbb, ddd]

•Set<V> difference(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().difference("setTest",strlist));
结果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
setTest3:[xxx, ccc, aaa]
[bbb, ddd]

•Long differenceAndStore(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().differenceAndStore("setTest","setTest2","differenceAndStore1"));
      System.out.println("differenceAndStore1:" + template.opsForSet().members("differenceAndStore1"));
结果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
2
differenceAndStore1:[bbb, ddd]

•Long differenceAndStore(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().differenceAndStore("setTest",strlist,"differenceAndStore2"));
      System.out.println("differenceAndStore2:" + template.opsForSet().members("differenceAndStore2"));
结果:setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
setTest3:[xxx, ccc, aaa]
2
differenceAndStore2:[bbb, ddd]

•Set<V> members(K key);
返回集合中的所有成员

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

•V randomMember(K key);
随机获取key无序集合中的一个元素

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

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