1. 程式人生 > >五種資料結構的單元test---需複合封裝的JedisUtil使用

五種資料結構的單元test---需複合封裝的JedisUtil使用

1、String

@Test
    @Test
    public void DemoString(){
        Jedis jedis = JedisUtil.getJedis();
        //儲存資料
        jedis.set("name","ninka");
        String a = jedis.get("name");
        System.out.println(a);
        //釋放資源
        JedisUtil.releaseResource(jedis);
    }

2、hash

@Test
public void demoHash(){ Jedis jedis = JedisUtil.getJedis(); jedis.hset("hash1","name","ninka"); jedis.hset("hash1","age","24"); System.out.println(jedis.hgetAll("hash1")); //年齡增加兩歲 jedis.hincrBy("hash1","age",2); System.out.println("增加年齡後:"+jedis.hgetAll("hash1"
)); jedis.hdel("hash1","age"); System.out.println("刪除年齡後:"+jedis.hgetAll("hash1")); JedisUtil.releaseResource(jedis); }

3、List

@Test
    public void demoList(){
        Jedis jedis = JedisUtil.getJedis();
        //list中左邊依次新增成員
        jedis.lpush("person","ninka1"
); jedis.lpush("person","zs2"); jedis.lpush("person","ls3"); jedis.lpush("person","ww4"); //lrange範圍輸出,0到-1是指全部list List<String> personList1 = jedis.lrange("person",0,-1); for (String person : personList1){ System.out.println(person); } //刪除list jedis.del("person"); //list中右邊依次新增成員 jedis.rpush("person","ninka1"); jedis.rpush("person","zs2"); jedis.rpush("person","ls3"); jedis.rpush("person","ww4"); //lrange範圍輸出,0到-1是指全部list List<String> personList2 = jedis.lrange("person",0,-1); for (String person : personList2){ System.out.println(person); } JedisUtil.releaseResource(jedis); }

4、Set

@Test
    public void demoSet(){
        Jedis jedis = JedisUtil.getJedis();
        jedis.sadd("myset1","ninka","a","b"," c","d");
        jedis.sadd("myset2","ninka", "a", "b", "d");
        System.out.println("myset1=" + jedis.smembers("myset1"));
        System.out.println("myset2=" + jedis.smembers("myset2"));
        //交集運算
        String inter = "";
        for (String str:jedis.sinter("myset1","myset2")){
            inter = inter + str +" ";
        }
        System.out.println("交集為:"+inter);
        //並集運算
        String union = "";
        for (String str:jedis.sunion("myset1","myset2")){
            union = union + str +" ";
        }
        System.out.println("並集為:"+union);
        //差集運算
        String diffSet="";
        for (String str:jedis.sdiff("myset1","myset2")){
            diffSet= diffSet + str + " ";
        }
        System.out.println("差集是:"+diffSet);
        JedisUtil.releaseResource(jedis);
    }

5、SortedSet

@Test
    public void demoSortedSet(){//有序的set
        Jedis jedis = JedisUtil.getJedis();
        jedis.zadd("mySortedSet1",100.0,"ninka");
        jedis.zadd("mySortedSet1",80,"zs");
        jedis.zadd("mySortedSet1",70,"ls");
        jedis.zadd("mySortedSet1",60,"ww");
        //檢視成員數量,引數一定要從小到大,否則代表的不是範圍
        System.out.println("成員數量為:"+jedis.zcard("mySortedSet1"));
        System.out.println("70~100的成員數量為:"+jedis.zcount("mySortedSet1",70,100));
        String sort = "";
        String str = "";
        for (Tuple tuple :jedis.zrangeWithScores("mySortedSet1",0,-1)){
            str = tuple.getElement()+","+String.valueOf(tuple.getScore());
            sort = sort+str+"  ";
        }
        System.out.println("所有成員的名和分數:["+sort+"]");
        //刪除ww
        jedis.zrem("mySortedSet1","ww");
        System.out.println("刪除ww之後的成員數量為:"+jedis.zcard("mySortedSet1"));
        //加分
        jedis.zincrby("mySortedSet1",1,"ls");
        String sort1 = "";
        String str1 = "";
        for (Tuple tuple :jedis.zrangeWithScores("mySortedSet1",0,-1)){
            str1 = tuple.getElement()+","+String.valueOf(tuple.getScore());
            sort1 = sort1+str1+"  ";
        }
        System.out.println("給ls加分後所有成員的名和分數:["+sort1+"]");
    }