public void addUserToCache(User user) {
        users.compute(user.getId(), (key, value) -> {
            Optional.ofNullable(value)
                    .map(Reference::get)
                    .filter(oldUser -> oldUser != user)
                    .map(Cleanupable.class::cast)
                    .ifPresent(Cleanupable::cleanup);

            WeakReference<User> result = new WeakReference<>(user, usersCleanupQueue);
            userIdByRef.put(result, key);
            return result;
        });
    }
public void addUserToCache ( User user ) { users . compute ( user . getId ( ) , ( key , value ) -> { Optional . ofNullable ( value ) . map ( Reference :: get ) . filter ( oldUser -> oldUser != user ) . map ( Cleanupable . class :: cast ) . ifPresent ( Cleanupable :: cleanup ) ; WeakReference < User > result = new WeakReference < > ( user , usersCleanupQueue ) ; userIdByRef . put ( result , key ) ; return result ; } ) ; }
Adds the given user to the cache.

@param user The user to add.