This content has been marked as final. Show 1 reply
You're getting the exception because the set that's returned is unmodifiable. (Even if it weren't, you'd get a ConcurrentModificationException, as you can't modify a Collection while you're iterating through it.)
I'm still not convinced you need to worry about doing this at all. You say the "user navigates to another scene". If you mean this literally (i.e., you're changing the scene that's in the stage), then you're calling stage.setScene(someNewScene). At that point, unless you're doing something really weird with your code, there should be no live references to the previous scene, and consequently no live references to the table and all the nodes that are part of the scene graph descending from it. Thus these will be eligible for garbage collection and the garbage collector will get them at some (unspecified) point in the future.
Even if you're not literally setting the scene, you are presumably removing some part of the scene graph and replacing it with something else. Again, unless you're deliberately hanging on to references to the part of the scene graph that's removed, everything you're worried about will be eligible for garbage collection.
Do you have some specific reason to convince yourself you need to manually manage memory here? There are certainly cases in Java when you need to do this, but they are (or should be) fairly few and far between.