Friendship Service

ID: 560; easy

Solution 1 (Java)

public class FriendshipService {

    private Map<Integer, Set<Integer>> followers;
    private Map<Integer, Set<Integer>> followings;
    public FriendshipService() {
        followers = new HashMap<>();
        followings = new HashMap<>();
    }

    /*
     * @param user_id: An integer
     * @return: all followers and sort by user_id
     */
    public List<Integer> getFollowers(int user_id) {
        if (followers.containsKey(user_id)) {
            return new ArrayList<Integer>(followers.get(user_id));
        }
        return new ArrayList<Integer>();
    }

    /*
     * @param user_id: An integer
     * @return: all followings and sort by user_id
     */
    public List<Integer> getFollowings(int user_id) {
        if (followings.containsKey(user_id)) {
            return new ArrayList<Integer>(followings.get(user_id));
        }
        return new ArrayList<Integer>();
    }

    /*
     * @param from_user_id: An integer
     * @param to_user_id: An integer
     * @return: nothing
     */
    public void follow(int to_user_id, int from_user_id) {
        followings.putIfAbsent(from_user_id, new TreeSet<Integer>());
        followings.get(from_user_id).add(to_user_id);

        followers.putIfAbsent(to_user_id, new TreeSet<Integer>());
        followers.get(to_user_id).add(from_user_id);
    }

    /*
     * @param from_user_id: An integer
     * @param to_user_id: An integer
     * @return: nothing
     */
    public void unfollow(int to_user_id, int from_user_id) {
        if (followings.containsKey(from_user_id))
            followings.get(from_user_id).remove(to_user_id);

        if (followers.containsKey(to_user_id))
            followers.get(to_user_id).remove(from_user_id);
    }
}

Notes

Last updated