> For the complete documentation index, see [llms.txt](https://blog.yushunchen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.yushunchen.com/algo/hash-table/friendship-service.md).

# 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
