title: Simulating DELETE cascades with WeakHashMaps
body: <p>I'm developing a service that monitors computers. Computers can be added to or removed from monitoring by a web GUI. I keep reported data basically in various maps like <code>Map&lt;Computer, Temperature&gt;</code>. Now that the collected data grows and the data structures become more sophisticated (including computers referencing each other) I need a concept for what happens when removing computers from monitoring. Basically I need to delete all data reported by the removed computer. The most <a href="http://en.wikipedia.org/wiki/KISS_principle" rel="noreferrer">KISS</a>-like approach would be removing the data manually from memory, like</p>
  
  <pre><code>public void onRemove(Computer computer) {
      temperatures.remove(computer);
      // ...
  }
  </code></pre>
  
  <p>This method had to be changed whenever I add features :-( I know Java has a <a href="http://docs.oracle.com/javase/8/docs/api/java/util/WeakHashMap.html" rel="noreferrer"><code>WeakHashMap</code></a>, so I could store reported data like so:</p>
  
  <pre><code> Map&lt;Computer, Temperature&gt; temperatures = new WeakHashMap&lt;&gt;();
  </code></pre>
  
  <p>I could call <a href="http://docs.oracle.com/javase/8/docs/api/java/lang/System.html#gc--" rel="noreferrer"><code>System.gc()</code></a> whenever a computer is removed from monitoring in order have all associated data eagerly removed from these maps.</p>
  
  <p>While the first approach seems a bit like primitive MyISAM tables, the second one resembles <a href="http://dev.mysql.com/doc/refman/5.7/en/innodb-foreign-key-constraints.html" rel="noreferrer">DELETE cascades in InnoDB tables</a>. But still it feels a bit uncomfortable and is probably the wrong approach. Could you point out advantages or disadvantages of <code>WeakHashMaps</code> or propose other solutions to this problem?</p>