# introduce

- Intersection: an element that appears at least twice in all sets
- Union: combine the elements of multiple sets and remove the duplicate elements
- Difference set: an element that exists in the first set and does not exist in other sets

# Related commands

command | explain |
---|---|

sinter key [key ...] | Gets the intersection of all sets |

sinterstore desitination key [key ...] | Get the intersection of all sets and overwrite the value of the destruction set |

sunion key [key ...] | Get all elements in all collections and de duplicate |

sunionstore destination key [key ...] | Get all elements in all sets, de duplicate and overwrite the value of the destination set |

sdiff key [key ...] | Gets the elements in one collection that are not in other collections |

sdiffstore destination key [key ...] | Get the elements in a collection that are not in other collections, and overwrite the value of the destination collection |

# intersection

## sinter key [key ...]

127.0.0.1:6379> sadd key1 1 2 3 4 (integer) 4 127.0.0.1:6379> sadd key2 1 5 2 8 (integer) 4 127.0.0.1:6379> sinter key1 key2 1) "1" 2) "2"

It can be seen from the above that 1 and 2 have appeared at least twice in all elements of key1 and key2, so the intersection of key1 and key2 is 1 and 2.

## sinterstore desitination key [key ...]

127.0.0.1:6379> sadd key1 1 2 3 4 (integer) 4 127.0.0.1:6379> sadd key2 1 5 2 8 (integer) 4 127.0.0.1:6379> sadd target 8 (integer) 1 127.0.0.1:6379> sinterstore target key1 key2 (integer) 2 127.0.0.1:6379> SMEMBERS target 1) "1" 2) "2"

It is learned from the above that after using this command, the return value is 2, indicating that there are 2 intersections, and the existing elements of target will be deleted first, and then the intersection will be written to target.

127.0.0.1:6379> sadd key1 1 2 3 4 (integer) 4 127.0.0.1:6379> sadd key2 1 5 2 8 (integer) 4 127.0.0.1:6379> get target (nil) 127.0.0.1:6379> sinterstore target key1 key2 (integer) 2 127.0.0.1:6379> SMEMBERS target 1) "1" 2) "2"

It is learned from the above that if the target does not exist, the key will be created first, and then the intersection will be written to the key.

127.0.0.1:6379> sadd key1 1 2 3 4 (integer) 4 127.0.0.1:6379> sadd key2 1 5 2 8 (integer) 4 127.0.0.1:6379> set target value OK 127.0.0.1:6379> sinterstore target key1 key2 (integer) 2 127.0.0.1:6379> SMEMBERS target 1) "1" 2) "2"

It is learned from the above that if the key type of the target is not set, the type of the key will be changed to set after using this command, and then the intersection will be written to the key.

Summary:

- If there is a value in the definition, the elements inside will be deleted first, and finally the elements of the intersection will be written, and the number of intersections will be returned.
- If the destination does not exist, the key will be created first, and finally the elements of the intersection will be written, and the number of intersections will be returned.
- If the type of destination is not set, it will first change the type to set, and finally write the elements of the intersection and return the number of intersections.

# Union

## sunion key [key ...]

127.0.0.1:6379> sadd key1 1 2 3 4 (integer) 4 127.0.0.1:6379> sadd key2 1 5 2 8 (integer) 4 127.0.0.1:6379> sunion key1 key2 1) "1" 2) "2" 3) "3" 4) "4" 5) "5" 6) "8"

From the above, we know that union is to obtain all elements in all parameter sets and remove duplicates.

## sunionstore destination key [key ...]

It is similar to the sinterstore destruction key [key...] command, except that it obtains Union.

# Difference set

## sdiff key key[key ...]

127.0.0.1:6379> sadd key1 1 2 3 4 (integer) 4 127.0.0.1:6379> sadd key2 1 5 2 8 (integer) 4 127.0.0.1:6379> sadd key4 3 8 (integer) 2 127.0.0.1:6379> sdiff key1 key2 key3 1) "4"

From the above, we know that the difference set is the element existing in the key at the first position, and the element that does not exist in other positions is called the difference set.

## sdiffstore destination key key[key ...]

It is similar to the sinterstore destruction key [key...] command, except that it obtains the difference set.

# SpringBoot mode

Prerequisite operation: introduce Redis related dependencies, and then inject RedisTemplet

## intersection

### 1) , get the intersection of two sets

Set<V> intersect(K key, K otherKey);

### 2) . get the intersection of key set and multiple sets

Set<V> intersect(K key, Collection<K> otherKeys);

### 3) The intersection of the key set and the otherKey set is stored in the destKey set

Long intersectAndStore(K key, Collection<K> otherKeys, K destKey);

### 4. The intersection of the key set and multiple sets is stored in the destKey set

Long intersectAndStore(K var1, Collection<K> var2, K var3);

## Union

### 1) , get the union of two sets

Set<V> union(K key, K otherKey);

### 2) . get the union of key set and multiple sets

Set<V> union(K key, Collection<K> otherKey);

### 3) The union of, key set and otherKey set is stored in destKey

Long unionAndStore(K key, K otherKey, K destKey);

### 4) The union of key sets and multiple sets is stored in destKey

Long unionAndStore(K key, Collection<K> var2, K destKey);

## Difference set

### 1) , get the difference set of two sets

Set<V> difference(K key, K otherKey); Return value:key-otherKey Difference set of

### 2) . obtain the difference set between the key set and multiple sets

Set<V> difference(K key, Collection<K> otherKeys);

### 3) The difference between the key set and the otherKey set is stored in destKey

Long differenceAndStore(K key, K otherKey, K destKey);

### 4) The difference set between the key set and multiple sets is stored in destKey

Long differenceAndStore(K key, Collection<K> otherKeys, K destKey);