import redis.clients.jedis.Jedis;
import java.util.List;import java.util.Set;/** * Created by user on 2015/12/8. */public class RedisTestForSet { private Jedis jedis = new Jedis("10.19.16.31",6379); public void test(){ //集合类型 //增加集合元素信息 jedis.sadd("set", "a", "b", "d", "f", "c"); //获取集合set中的值 Set<String> values= jedis.smembers("set"); System.out.println(values); //删除元素a jedis.srem("set","a"); values= jedis.smembers("set"); System.out.println(values); //元素的个数 Long l= jedis.scard("set"); System.out.println(l); jedis.sadd("set1", "a", "b", "13", "23", "c"); //获取差集 Set<String> set2=jedis.sdiff("set","set1"); System.out.println(set2); set2=jedis.sdiff("set1","set"); System.out.println(set2); //返回给定所有集合的差集并存储在 destination 中 jedis.sdiffstore("set3","set1","set"); values= jedis.smembers("set3"); System.out.println(values); //返回给定所有集合的交集 Set<String> set5 = jedis.sinter("set1","set"); System.out.println(set5); //返回给定所有集合的交集并存储在 destination 中 jedis.sinterstore("set5","set1","set"); values= jedis.smembers("set5"); System.out.println(values); //判断 member 元素是否是集合 key 的成员 boolean b = jedis.sismember("set","f"); System.out.println(b); /* SMOVE source destination member 将 member 元素从 source 集合移动到 destination 集合*/ jedis.smove("set", "set5", "f"); values= jedis.smembers("set5"); System.out.println(values); /*SPOP key 移除并返回集合中的一个随机元素*/ String random = jedis.spop("set"); System.out.println(random); /* SRANDMEMBER key [count] 返回集合中一个或多个随机数*/ List<String> resl= jedis.srandmember("set",2); System.out.println(resl); /* SUNION key1 [key2] 返回所有给定集合的并集*/ Set<String> ss= jedis.sunion("set","set1"); System.out.println(ss); /* SUNIONSTORE destination key1 [key2] 所有给定集合的并集存储在 destination 集合中*/ jedis.sunionstore("set6","set","set1"); values= jedis.smembers("set6"); System.out.println(values); } public static void main(String[] args){ RedisTestForSet redisTestForSet = new RedisTestForSet(); redisTestForSet.test(); }}