-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwitter.java
More file actions
174 lines (142 loc) · 3.98 KB
/
Twitter.java
File metadata and controls
174 lines (142 loc) · 3.98 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
class Tweet
{
int _TimeStamp, _tweetID;
public Tweet( int ts, int id )
{
_TimeStamp = ts;
_tweetID = id;
}
}
class TwitterIndividual
{
int _userID;
List<Tweet> _tweetList;
List<Integer> _followList;
public TwitterIndividual( int userID )
{
_userID = userID;
_tweetList = new LinkedList<>();
_followList = new ArrayList<>();
_followList.add( userID );
}
public TwitterIndividual( int userID, int timeStamp, int tweet )
{
this( userID );
_tweetList.add( new Tweet( timeStamp, tweet ) );
}
public void addFollow( int followeeID )
{
if ( !_followList.contains( followeeID ) )
_followList.add( followeeID );
}
public void deleteFollow( int followeeID )
{
if ( _followList.contains( followeeID ) )
_followList.remove( _followList.indexOf( followeeID ) );
}
public List<Integer> getFollowing()
{
return _followList;
}
public List<Tweet> getTweets()
{
return this._tweetList;
}
}
public class Twitter
{
List<TwitterIndividual> _twitterUser;
List<Integer> _userList;
int _timeStamp;
/** Initialize your data structure here. */
public Twitter()
{
_twitterUser = new ArrayList<>();
_userList = new ArrayList<>();
}
/** Compose a new tweet. */
public void postTweet( int userId, int tweetId )
{
if ( !_userList.contains( userId ) )
{
_userList.add( userId );
_twitterUser.add( new TwitterIndividual( userId, _timeStamp++, tweetId ) );
}
else
{
List<Tweet> tweets = getUser( userId ).getTweets();
if ( tweets.size() > 10 )
tweets.remove( 0 );
getUser( userId ).getTweets().add( new Tweet( _timeStamp++, tweetId ) );
}
}
TwitterIndividual getUser( int userID )
{
if ( _userList.contains( userID ) )
return _twitterUser.get( _userList.indexOf( userID ) );
return null;
}
/**
* 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 )
{
// List<List<Tweet>> newsList = new ArrayList<List<Tweet>>();
List<Tweet> newsList = new ArrayList<>();
List<Integer> newsListTweets = new ArrayList<>();
if ( getUser( userId ) == null )
return newsListTweets;
List<Integer> followList = getUser( userId )._followList;
for ( int i = 0; i < followList.size(); i++ )
newsList.addAll( getUser( followList.get( i ) )._tweetList );
Collections.sort( newsList, ( a, b ) -> ( b._TimeStamp - a._TimeStamp ) );
if ( newsList.size() > 10 )
newsList = newsList.subList( 0, 10 );
for ( int i = 0; i < newsList.size(); i++ )
newsListTweets.add( newsList.get( i )._tweetID );
return newsListTweets;
}
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
public void follow( int followerId, int followeeId )
{
if ( !_userList.contains( followerId ) )
{
_userList.add( followerId );
_twitterUser.add( new TwitterIndividual( followerId ) );
}
if ( !_userList.contains( followeeId ) )
{
_userList.add( followeeId );
_twitterUser.add( new TwitterIndividual( followeeId ) );
}
getUser( followerId ).addFollow( followeeId );
}
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
public void unfollow( int followerId, int followeeId )
{
if ( followerId == followeeId )
return;
if ( _userList.contains( followerId ) )
getUser( followerId ).deleteFollow( followeeId );
}
public static void main( String[] args )
{
Twitter twitter = new Twitter();
twitter.postTweet( 1, 5 );
twitter.unfollow( 1, 1 );
twitter.getNewsFeed( 1 );
}
}
/**
* Your Twitter object will be instantiated and called as such:
* Twitter obj = new Twitter();
* obj.postTweet(userId,tweetId);
* List<Integer> param_2 = obj.getNewsFeed(userId);
* obj.follow(followerId,followeeId);
* obj.unfollow(followerId,followeeId);
*/