题目描述：

Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user's news feed. Your design should support the following methods:

1. postTweet(userId, tweetId): Compose a new tweet.
2. getNewsFeed(userId): Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent.
3. follow(followerId, followeeId): Follower follows a followee.
4. unfollow(followerId, followeeId): Follower unfollows a followee.

题目分析：

这道题我在实现的时候花了比较长的时间，主要有两点没有注意到：

注意到以上两点，这个题就不难解决。

具体代码：

```  1 public class Twitter {
2     private static int order=0;
3     private Map<Integer,Set<Message>> messages;
4     private Map<Integer,Set<Integer>> followers;
5     /** Initialize your data structure here. */
7         messages = new HashMap<Integer,Set<Message>>();
8         followers = new HashMap<Integer,Set<Integer>>();
9     }
10
11     /** Compose a new tweet. */
12     public void postTweet(int userId, int tweetId) {
13         Message m = new Message(userId,tweetId,order++);
14         Set<Message> set=messages.getOrDefault(userId, new HashSet<Message>());
16         messages.put(userId, set);
17     }
18
19     /** Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. */
20     public List<Integer> getNewsFeed(int userId) {
21         List<Message> sets = new ArrayList<Message>();
22         //userId发布的消息
23         Set<Message> set = messages.getOrDefault(userId, new HashSet<Message>());
25         //找出他所关注的人发布的消息
27         if(follow!=null){
28             for(int i:follow){
29                 set=messages.getOrDefault(i, new HashSet<Message>());
31             }
32         }
33         //对找出的消息进行排序，并找出最近翻出的10条返回
34         List<Integer> result=new ArrayList<Integer>();
35         Compare c =new Compare();
36         sets.sort(c);
37         for(int i=0;i<sets.size()&&i<10;i++){
38             Message m=sets.get(i);
40         }
41         return result;
42     }
43
44     /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
45     public void follow(int followerId, int followeeId) {
46         if(followeeId==followerId)
47             return;
48         Set<Integer> set = followers.getOrDefault(followerId, new HashSet<Integer>());
50         followers.put(followerId, set);
51
52     }
53
54     /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
55     public void unfollow(int followerId, int followeeId) {
56         if(followeeId==followerId)
57             return;
58         if(!followers.containsKey(followerId)){
59              ;
60          }
61          else{
62              Set<Integer> set = followers.get(followerId);
63              set.remove(followeeId);
64              //如果userid的用户已经没有关注的人，讲他的记录从map中删除掉
65              if(set.size()==0){
66                  followers.remove(followerId);
67              }
68              else{
69              followers.put(followerId, set);
70              }
71          }
72     }
73 }
75 class Message{
76     int userId;
78     int order;
79     public Message(int userId, int twitterId,int order) {
80         super();
81         this.userId = userId;
83         this.order=order;
84     }
85 }
86 //对消息进行排序的比较器
87 class Compare implements Comparator<Message>{
88
89     @Override
90     public int compare(Message m1, Message m2) {
91         // TODO Auto-generated method stub
92         if(m1.order>m2.order)
93             return -1;
94         else if(m1.order<m2.order)
95             return 1;
96         else
97             return 0;
98     }
99
100 }```
• 相关阅读: