Java HashMap, HashSet and PriorityQueue


  • 0
    A
    public class Twitter {
    
        private static class Tweet {
            private final int id;
            private final int timestamp;
    
            public Tweet(final int id, final int timestamp) {
                this.id = id;
                this.timestamp = timestamp;
            }
        }
    
        private Map<Integer, Set<Integer>> followingByUser;
    
        private Map<Integer, PriorityQueue<Tweet>> tweetsByUser;
    
        private AtomicInteger timestamp;
    
        /** Initialize your data structure here. */
        public Twitter() {
            followingByUser = new HashMap<>();
            tweetsByUser = new HashMap<>();
            timestamp = new AtomicInteger(0);
        }
    
        /** Compose a new tweet. */
        public void postTweet(final int userId, final int tweetId) {
            PriorityQueue<Tweet> tweets = tweetsByUser.getOrDefault(userId, buildTweetHeap());
            while (tweets.size() >= 10) {
                tweets.poll();
            }
    
            tweets.offer(new Tweet(tweetId, timestamp.getAndIncrement()));
            tweetsByUser.put(userId, tweets);
        }
    
        private PriorityQueue<Tweet> buildTweetHeap() {
            return new PriorityQueue<>(Comparator.comparingInt(tweet -> tweet.timestamp));
        }
    
        /** 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(final int userId) {
            Set<Integer> following = followingByUser.getOrDefault(userId, new HashSet<>());
    
            PriorityQueue<Tweet> ownTweets = tweetsByUser.getOrDefault(userId, buildTweetHeap());
            PriorityQueue<Tweet> tweets = new PriorityQueue<>(ownTweets);
            for (int followeeId : following) {
                if (tweetsByUser.containsKey(followeeId)) {
                    PriorityQueue<Tweet> followeeTweets = tweetsByUser.get(followeeId);
                    tweets.addAll(followeeTweets);
                }
            }
    
            if (tweets.isEmpty()) {
                return Collections.emptyList();
            }
    
            while (tweets.size() > 10) {
                tweets.poll();
            }
    
            List<Integer> result = new ArrayList<>(Collections.nCopies(tweets.size(), 0));
            for (int i = tweets.size()-1; i >= 0; i--) {
                result.set(i, tweets.poll().id);
            }
            return result;
        }
    
        /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
        public void follow(final int followerId, final int followeeId) {
            if (followerId == followeeId) {
                return;
            }
    
            Set<Integer> following = followingByUser.getOrDefault(followerId, new HashSet<>());
            if (!following.contains(followeeId)) {
                following.add(followeeId);
                followingByUser.put(followerId, following);
            }
        }
    
        /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
        public void unfollow(final int followerId, final int followeeId) {
            if (followerId == followeeId || !followingByUser.containsKey(followerId)) {
                return;
            }
    
            Set<Integer> following = followingByUser.get(followerId);
            following.remove(followeeId);
            if (!following.isEmpty()) {
                followingByUser.put(followerId, following);
            } else {
                followingByUser.remove(followerId);
            }
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.