Redis之set類型操作


接口:

package com.net.test.redis.base.dao;
/**
 * @author***
 * @Time:2017年8月10日 下午2:32:12
 * @version 1.0
 * @description
 */
public interface IRedisDaoNoSotredSet {

    public void add(String key, String...values);
    
    public void diff(String key, String otherKey);
    
    public void diddStore(String destKey, String key, String otherKey);
    
    public void inter(String key, String otherKey);
    
    public void interStore(String destKey, String key, String otherKey);
    
    public void union(String key, String otherKey);
    
    public void unionStore(String destKey, String key, String otherKey);
    
    public void pop(String key);
    
    public void remove(String key,String...values);
    
    public void size(String key);

    public void smembers(String key);
    
}

 

實現類:

package com.net.test.redis.base.dao.imp;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Repository;

import com.net.test.redis.base.dao.IRedisDaoNoSotredSet;

/**
 * @author ***
 * @Time:2017年8月10日 下午2:53:33
 * @version 1.0
 * @description
 */
@Repository
public class RedisDaoNoSotredSetImp implements IRedisDaoNoSotredSet{

    @Autowired
    private RedisTemplate<String, String>  redis;
    
    /**
     * @description 添加元素
     * @param key
     * @param values
     */
    @Override
    public void add(String key, String...values) 
    {
        SetOperations<String, String> oper = redis.opsForSet();
        int value =    oper.add(key, values).intValue();
        System.out.println("是否添加成功 :" + value);
    }
    
    /**
     * @description 找出兩個集合不同的部分,並進行遍歷
     * @param key
     * @param otherKey
     */
    @Override
    public void diff(String key, String otherKey)
    {
        SetOperations<String, String> oper = redis.opsForSet();
        Set<String> set = oper.difference(key,otherKey);
        for(String str : set)
        {
            System.out.println("兩個set的不同元素 : " + str);
        }
    }
    
    /**
     * @description  找出兩個集合不同的部分,並存到destKey集合中
     * @param destKey
     * @param key
     * @param otherKey
     */
    @Override
    public void diddStore(String destKey, String key, String otherKey)
    {
        SetOperations<String, String> oper = redis.opsForSet();
        int value = oper.differenceAndStore(key, otherKey, destKey).intValue();
        System.out.println("返回值 : " + value);
    }
    
    /**
     * @description 找出兩個集合的相同部分,並進行遍歷
     * @param key
     * @param otherKey
     */
    @Override
     public void inter(String key, String otherKey)
    {
        SetOperations<String, String> oper = redis.opsForSet();
        Set<String> set = oper.intersect(key, otherKey);
        for(String str : set)
        {
            System.out.println("兩個set的相同元素 : " + str);
        }
    }
    
    /**
     * @description 找出兩個集合的相同部分,存到新集合destKey中
     * @param destKey
     * @param key
     * @param otherKey
     */
    @Override
    public void interStore(String destKey, String key, String otherKey)
    {
        SetOperations<String, String> oper = redis.opsForSet();
        int value = oper.intersectAndStore(key, otherKey, destKey).intValue();
        System.out.println("返回值 : " + value); 
    }
    
    /** 
     * @description  找出兩個集合的並集,並進行遍歷
     * @param key
     * @param otherKey
     */
    @Override
    public void union(String key, String otherKey) 
    {
        SetOperations<String, String> oper = redis.opsForSet();
        Set<String> set = oper.union(key, otherKey);
        for(String str : set)
        {
            System.out.println("兩個set的相同元素 : " + str);
        }
    }
    
    /**
     * @description 找出兩個集合的並集,並存放到新集合destKey中
     * @param destKey
     * @param key
     * @param otherKey
     */
    @Override
    public void unionStore(String destKey, String key, String otherKey) 
    {
        SetOperations<String, String> oper = redis.opsForSet();
        int value = oper.unionAndStore(key, otherKey, destKey).intValue();
        System.out.println("返回值 : " + value); 
    }

    /**
     * @description  無序集合,隨機移除一個元素
     * @param key
     */
    @Override
    public void pop(String key)
    {
        SetOperations<String, String> oper = redis.opsForSet();
        String value = oper.pop(key);
        System.out.println("隨機移除的value為 : " + value);
    }
    
    /**
     * @description 根據給定的元素,去移除
     * @param key
     * @param values
     */
    @Override
    public void remove(String key, String...values) 
    {
        SetOperations<String, String> oper = redis.opsForSet();
        int value = oper.remove(key, (Object[])values).intValue();
        System.out.println("是否成功移除選定元素 : " + value);
    }
    
    /**
     * @description 獲取集合長度
     * @param key
     */
    @Override
    public void size(String key) 
    {
        SetOperations<String, String> oper = redis.opsForSet();
        int value = oper.size(key).intValue();
        System.out.println("set 集合長度為  : " + value);
    }
    
    /**
     * @description    遍歷整個集合
     * @param key
     */
    @Override
    public void smembers(String key) 
    {    
        SetOperations<String, String> oper = redis.opsForSet();
        Set<String> set = oper.members(key);
        for(String str : set)
        {
            System.out.println("兩個set的相同元素 : " + str);
        }
    }
    
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM