-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTwitter.java
More file actions
110 lines (94 loc) · 3.48 KB
/
Twitter.java
File metadata and controls
110 lines (94 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package LeetCode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
/*
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:
postTweet(userId, tweetId): Compose a new tweet.
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.
follow(followerId, followeeId): Follower follows a followee.
unfollow(followerId, followeeId): Follower unfollows a followee.
*/
public class Twitter {
private Map<Integer, Set<Integer>> userFollowers;
private Map<Integer, Queue<Integer>> userTimeline;
/** Initialize your data structure here. */
public Twitter() {
userFollowers = new HashMap<>();
userTimeline = new HashMap<>();
}
/** Compose a new tweet. */
public void postTweet(int userId, int tweetId) {
Set<Integer> followers = userFollowers.get(userId);
if (followers != null) {
followers.stream().forEach(follower -> addToQueue(follower, tweetId));
}
addToQueue(userId, tweetId);
}
private void addToQueue(Integer follower, Integer tweetId) {
Queue<Integer> timeline = userTimeline.get(follower);
if (timeline == null) {
timeline = new LinkedList<>();
userTimeline.put(follower, timeline);
}
if (timeline.size() > 10) {
timeline.poll();
}
timeline.add(tweetId);
}
/**
* 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.
*/
public List<Integer> getNewsFeed(int userId) {
if (userTimeline.containsKey(userId)) {
Queue<Integer> feed = userTimeline.get(userId);
List<Integer> newsFeed = new ArrayList<>();
newsFeed.addAll(feed);
return newsFeed;
} else {
return Collections.emptyList();
}
}
/**
* Follower follows a followee. If the operation is invalid, it should be a
* no-op.
*/
public void follow(int followerId, int followeeId) {
if (userFollowers.containsKey(followerId)) {
userFollowers.get(followeeId).add(followerId);
} else {
Set<Integer> followers = new HashSet<>();
followers.add(followerId);
userFollowers.put(followeeId, followers);
}
}
/**
* Follower unfollows a followee. If the operation is invalid, it should be a
* no-op.
*/
public void unfollow(int followerId, int followeeId) {
if (userFollowers.containsKey(followeeId)) {
userFollowers.get(followeeId).remove(followerId);
}
}
public static void main(String[] args) {
Twitter inst = new Twitter();
inst.postTweet(1, 5);
inst.getNewsFeed(1);
inst.follow(1, 2);
inst.postTweet(2, 6);
inst.getNewsFeed(1);
inst.unfollow(1, 2);
inst.getNewsFeed(1);
}
}