# Friendship Service

{% embed url="<https://www.lintcode.com/problem/560/>" %}

## Solution 1 (Java)

```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

* Notice here we used `TreeSet` to make sure the get methods return **sorted** and **unique** results.
* In line 16 and 27, we created an ArrayList from a TreeSet ([Initialization using another Collection](https://www.geeksforgeeks.org/initialize-an-arraylist-in-java/)).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.yushunchen.com/algo/hash-table/friendship-service.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
