Buggy code:
public final class CommandManager implements ICommandManager { private SortedSet activeCommandIds = new TreeSet ( ) ; private SortedMap commandElementsById = new TreeMap ( ) ; private ICommandManagerEvent commandManagerEvent ; private List commandManagerListeners ; private SortedMap commandsById = new TreeMap ( ) ; private SortedSet definedCommandIds = new TreeSet ( ) ; private RegistryReader registryReader ; public CommandManager ( ) { } public void addCommandManagerListener ( ICommandManagerListener commandManagerListener ) throws IllegalArgumentException { } public SortedSet getActiveCommandIds ( ) { } public ICommand getCommand ( String commandId ) throws IllegalArgumentException { } public SortedSet getDefinedCommandIds ( ) { } public void removeCommandManagerListener ( ICommandManagerListener commandManagerListener ) throws IllegalArgumentException { } public void setActiveCommandIds ( SortedSet activeCommandIds ) throws IllegalArgumentException { } public void updateDefinedCommandIds ( ) { if ( ( registryReader ) == null ) registryReader = new RegistryReader ( Platform . getPluginRegistry ( ) ) ; registryReader . load ( ) ; SortedMap commandElementsById = CommandElement . sortedMapById ( registryReader . getCommandElements ( ) ) ; SortedSet commandElementAdditions = new TreeSet ( ) ; SortedSet commandElementChanges = new TreeSet ( ) ; SortedSet commandElementRemovals = new TreeSet ( ) ; Iterator iterator = commandElementsById . entrySet ( ) . iterator ( ) ; while ( iterator . hasNext ( ) ) { Map . Entry entry = ( ( Map . Entry ) ( iterator . next ( ) ) ) ; String id = ( ( String ) ( entry . getKey ( ) ) ) ; CommandElement commandElement = ( ( CommandElement ) ( entry . getValue ( ) ) ) ; if ( ! ( this . commandElementsById . containsKey ( id ) ) ) commandElementAdditions . add ( id ) ; else if ( ! ( Util . equals ( commandElement , this . commandElementsById . get ( id ) ) ) ) commandElementChanges . add ( id ) ; } iterator = this . commandElementsById . entrySet ( ) . iterator ( ) ; while ( iterator . hasNext ( ) ) { Map . Entry entry = ( ( Map . Entry ) ( iterator . next ( ) ) ) ; String id = ( ( String ) ( entry . getKey ( ) ) ) ; CommandElement commandElement = ( ( CommandElement ) ( entry . getValue ( ) ) ) ; if ( ! ( commandElementsById . containsKey ( id ) ) ) commandElementRemovals . add ( id ) ; } SortedSet commandChanges = new TreeSet ( ) ; commandChanges . addAll ( commandElementAdditions ) ; commandChanges . addAll ( commandElementChanges ) ; commandChanges . addAll ( commandElementRemovals ) ; if ( ! ( commandChanges . isEmpty ( ) ) ) { this . commandElementsById = commandElementsById ; <START_BUG> SortedSet definedCommandIds = ( ( SortedSet ) ( commandElementsById . keySet ( ) ) ) ; <END_BUG> if ( ! ( Util . equals ( definedCommandIds , this . definedCommandIds ) ) ) { this . definedCommandIds = definedCommandIds ; fireCommandManagerChanged ( ) ; } iterator = commandChanges . iterator ( ) ; while ( iterator . hasNext ( ) ) fireCommandChanged ( ( ( String ) ( iterator . next ( ) ) ) ) ; } } CommandElement getCommandElement ( String commandId ) { } private void fireCommandChanged ( String commandId ) { } private void fireCommandManagerChanged ( ) { } }

Fix:
SortedSet definedCommandIds = new TreeSet ( commandElementsById . keySet ( ) ) ;