Storing Social Relationships
For example, users' friends/followers/followings can be stored in a set, so the operation of seeking common friends between two people only needs to use the sinter intersect command; union and subtraction operations can also be performed.
import java.util.Set;
import redis.clients.jedis.Jedis;
public class CommonFriends {
public static void main(String[] args) {
String host = "127.0.0.1";
int port = 10011;
Jedis jedis = new Jedis(host, port);
//my friends
jedis.sadd("myfriends", "John");
jedis.sadd("myfriends", "Emliy");
jedis.sadd("myfriends", "Ben");
jedis.sadd("myfriends", "Steven");
System.out.println("my friends are: ");
Set<String> myList = jedis.smembers("myfriends");
for (String item:myList) {
System.out.print(item+" ");
}
//your friends
jedis.sadd("yourfriends", "Mark");
jedis.sadd("yourfriends", "Tim");
jedis.sadd("yourfriends", "Willim");
jedis.sadd("yourfriends", "Ben");
jedis.sadd("yourfriends", "Steven");
System.out.println("\n");
System.out.println("your friends are: ");
Set<String> yourList = jedis.smembers("yourfriends");
for (String item:yourList) {
System.out.print(item+" ");
}
//our common friends
System.out.println("\n");
System.out.println("our common friends are: ");
Set<String> commonList = jedis.sinter("myfriends","yourfriends");
for (String item:commonList) {
System.out.print(item+" ");
}
jedis.close();
}
}
Output:
my friends are:
Steven Emliy John Ben
your friends are:
Steven Mark Tim Ben Willim
our common friends are:
Steven Ben