-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeed.java
More file actions
43 lines (35 loc) · 1.14 KB
/
Feed.java
File metadata and controls
43 lines (35 loc) · 1.14 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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
public class Feed {
Map<Integer, Tweet> tweetMap;
public Feed() {
tweetMap = new HashMap<>();
}
public void addToFeed(Tweet tweet) {
int tweetId = tweet.getTweetId();
try {
if (tweetMap.containsKey(tweetId)) {
throw new IllegalStateException(
"Duplicate TweetId found"
);
}
} catch(Exception e) {
System.out.println(e.getMessage());
}
tweetMap.put(tweetId, tweet);
}
public List<Tweet> getRevChronoTweetList() {
List<Tweet> tweetList = new ArrayList<>();
PriorityQueue<Map.Entry<Integer, Tweet>> maxHeap = new PriorityQueue<>((a, b) -> b.getKey() - a.getKey());
for(Map.Entry<Integer, Tweet> entry : tweetMap.entrySet()) {
maxHeap.offer(entry);
}
while(!maxHeap.isEmpty()) {
tweetList.add(maxHeap.poll().getValue());
}
return tweetList;
}
}