title: How to properly use ObservableMap in Java
body: <p>I'm currently developing a chat application and I want, in my GUI controllers, to have Observables Collections to be able to manage messages that I receive.</p>
  
  <p>So, to be more specific I have this listener where I listen for new messages that comes from the server:</p>
  
  <p><strong>Listener for new Chats:</strong></p>
  
  <pre><code>class ChatCustomManagerListener {   
     private static HashMap &lt;Chat, ChatCustomMessageListener&gt; chatsTable;
  
     /*
     * This is the event that gets triggered when new chat is created
     */
     public void chatCreated (Chat newChat) {
         ChatCustomMessageListener listener = new ChatCustomMessageListener();
         newChat.addListener (listener);
         chatsTable.put (newChat, listener);
      }
  }
  </code></pre>
  
  <p><strong>Listener for new Messages:</strong></p>
  
  <pre><code>class ChatCustomMessageListener {
     private List &lt;Message&gt; messagesQueue;
  
     public ChatCustomMessageListener () {
        //QueuedLinkedList is my extension of LinkedList class
        //add() is made at the tail of List
        //get() returns the head of list
        messagesQueue = new QueuedLinkedList ();
      }
     /*
     * This is the event that gets triggered when a new message is received
     */
      public void processMessage (Chat chat, Message message) {
         messagesQueue.add (message);
      }
  }
  </code></pre>
  
  <p>Now, In my GUI Controllers, I want to define an <code>ObservableMap</code> to look for <code>ChatCustomManagerListener.chatsTable</code> changes and for each <code>Entry</code> inside chatsTable, I want to have an <code>ObservableList</code> for <code>ChatCustomMessageListener.messagesQueue</code> </p>
  
  <p>What I got so far, but not working is this:</p>
  
  <pre><code>    class ChatController {
         private ObservableMap  &lt;Chat, ChatCustomMessageListener&gt; chatsObserver;
         private LinkedList &lt;ObservableList &lt;Message&gt;&gt; messagesObservables;
         private LinkedList &lt;List &lt;Message&gt;&gt; messageData;
  
         private void initObservables () {
            chatsObserver = FXCollections.observableMap (ChatCustomManagerListener.getChats ());
  
          messageData   = new LinkedList &lt;&gt; ();
          messagesObservables = new LinkedList &lt;&gt; ();
  
          chatsObserver.forEach ((chat, messageListener) -&gt; {
              messageData.add (messageListener.getMessagesQueue());
          });
  
          messageData.forEach ((queueOfMessages) -&gt; {
              ObservableList &lt;Message&gt; observer = FXCollections.observableList (queueOfMessages);
              observer.addListener (new ListChangeListener &lt;Message&gt; () {
  
                  @Override
                  public void onChanged (javafx.collections.ListChangeListener.Change &lt;? extends Message&gt; c)                      
                      log.debug ("New message was received");
                  }
              });
              messagesObservables.add (observer);
          });
  
          chatsObserver.addListener (new MapChangeListener &lt;Chat, ChatCustomMessageListener&gt; () {
  
              @Override
              public void onChanged (
                      javafx.collections.MapChangeListener.Change &lt;? extends Chat, ? extends ChatCustomMessageListener&gt; change) {
                  //define what is done when a new chat is created
                  log.debug ("New  chat was created");
  
              }
  
          });
  }}
  </code></pre>
  
  <p>So what is the right way to define my Observables?</p>