Getting java.util.ConcurrentModificationException, help needed


  • 0
    V

    Hi everyone, below is my solution for the design twitter problem. It always give me the ConcurrentModificationException error when testing with the longest sample. Can someone kindly help me point out where I did wrong that could lead to the ConcurrentModificationException error? Many thanks!

    public class Twitter {
        private int _timestamp;
        private Map<Integer, User> hash;
        
        // The class for each twitter user
        private class User {
            Set<Integer> followers;
            PriorityQueue<Tweet> feeds = new PriorityQueue<>();
            User() {
                followers = new HashSet<>();
                feeds = new PriorityQueue<>();
            }
        }
        
        private class Tweet implements Comparable<Tweet>{
            int id;
            int userId;
            int timestamp;
            Tweet(int tweetId, int userId) {
                id = tweetId;
                this.userId = userId;
                this.timestamp = _timestamp++;
            }
            public int compareTo(Tweet t) {
                return this.timestamp > t.timestamp ? -1 : (this.timestamp < t.timestamp ? 1 : 0);
            }
        }
    
        /** Initialize your data structure here. */
        public Twitter() {
            hash = new HashMap<>();
            _timestamp = 0;
        }
        
        /** Compose a new tweet. */
        public void postTweet(int userId, int tweetId) {
            User user = hash.containsKey(userId) ? hash.get(userId) : new User();
            Tweet tweet = new Tweet(tweetId, userId);
            user.feeds.offer(tweet);
            
            hash.put(userId, user);
            for (int id : user.followers) {
                if (hash.containsKey(id)) {
                    User follower = hash.get(id);
                    follower.feeds.offer(tweet);
                }
            }
        }
        
        /** 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<Integer> ret = new ArrayList<>();
            if (!hash.containsKey(userId)) return ret;
            User user = hash.get(userId);
            int size = Math.min(10, user.feeds.size());
            List<Tweet> temp = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                Tweet tweet = user.feeds.poll();
                ret.add(tweet.id);
                temp.add(tweet);
            }
            for (Tweet tweet : temp) user.feeds.offer(tweet);
            return ret;
        }
        
        /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
        public void follow(int followerId, int followeeId) {
            User follower = hash.containsKey(followerId) ? hash.get(followerId) : new User();
            User followee = hash.containsKey(followeeId) ? hash.get(followeeId) : new User();
            followee.followers.add(followerId);
            for (Tweet tweet : followee.feeds) {
                follower.feeds.offer(tweet);
            }
            hash.put(followeeId, followee);
            hash.put(followerId, follower);
        }
        
        /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
        public void unfollow(int followerId, int followeeId) {
            if (!hash.containsKey(followerId) || !hash.containsKey(followeeId)) return;
            User followee = hash.get(followeeId);
            User follower = hash.get(followerId);
            followee.followers.remove(followerId);
            List<Tweet> temp = follower.feeds.stream().filter(tweet -> tweet.userId != followeeId).collect(Collectors.toList());
            follower.feeds = new PriorityQueue<>(temp);
        }
    }
    

  • 0
    C

    it is caused by modifying and iterating a collection at the same time.


Log in to reply
 

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