title: Unable to Add Elements to LinkedHashMap in Java
body: <p>I'm reading emails using JavaMail API and adding each message (along with sender, sent date etc) to a LinkedHashMap. Next I'm adding the LinkedHashMap to an ArrayList of LinkedHashMaps. However, the odd thing is that the entire Hashmapis being overwritten by the last entry.</p>
  
  <p>I have the following code:</p>
  
  <pre><code>private ArrayList&lt;LinkedHashMap&lt;String, String&gt;&gt; readAllMsgsFromOutlook() throws FileNotFoundException, IOException, MessagingException
      {
  
          LinkedHashMap&lt;String, String&gt; msgsLinkedHashMap = new LinkedHashMap&lt;String, String&gt;();
          ArrayList&lt;LinkedHashMap&lt;String, String&gt;&gt; msgsLinkedHashMapAsArrayList = new ArrayList&lt;LinkedHashMap&lt;String, String&gt;&gt;();
  
          //String host = "smtp-mail.outlook.com";
  
          String emailID = ..getEmail();
          String password = ..getPassword();
          Properties props = new Properties();
  
          props.setProperty("mail.store.protocol", "imaps");
  
  
          try {
              Session session = Session.getInstance(props, null);
  
              Store store = session.getStore("imaps");
              store.connect("imap-mail.outlook.com", emailID, password);
              Folder inboxFolder = store.getFolder("INBOX");
              UIDFolder UID = (UIDFolder)inboxFolder;
  
              Message[] msgs = null;
  
  
              Flags seen = new Flags(Flags.Flag.SEEN);
              FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
              msgs = inboxFolder.search(unseenFlagTerm);  
  
  
              System.out.println("Number of New Emails in Inbox: " + msgs.length + "\n");
  
  
  
              String senderAsString = "";
              String sentDateAsString = "";
              String subjectAsString = "";
              String contentAsString = "";
              String UIDAsString = "";
  
  
              for (int msgCounter = 0; msgCounter &lt; msgs.length; msgCounter++) 
  
              {
  
                  System.out.println("Email #: " + (msgCounter + 1) + "\n");
  
                  Message msg  = msgs[msgCounter];
                  long UIDAsLong = UID.getUID(msg);
  
                  Object content = msg.getContent();  
  
  
                  if (content instanceof String)  
                  {  
                      System.out.println("Email is instance of String: \n");
  
  
                      Address[] sender = msg.getFrom();
  
  
  
                      senderAsString = sender[0].toString();
                      System.out.println("SENDER: " + senderAsString);
                      sentDateAsString = msg.getSentDate().toString();
                      System.out.println("SENT_DATE: " + sentDateAsString);
  
                      subjectAsString = msg.getSubject().toString();
                      System.out.println("SUBJECT: " + subjectAsString);
                      contentAsString = (String)content; 
                      System.out.println("CONTENT: " + contentAsString);
  
                      UIDAsString = String.valueOf(UIDAsLong);
  
                      System.out.println("UID: " + UIDAsString);
  
  
                      System.out.println("\n");
  
  
  
                  }  
                  else if (content instanceof Multipart)  
                  {  
                      System.out.println("Email is instance of Multipart");
  
                      Multipart mp = (Multipart)content;  
                      BodyPart bp = mp.getBodyPart(0);
                      Address[] sender = msg.getFrom();
                      sentDateAsString = msg.getSentDate().toString();
                      subjectAsString = msg.getSubject();
                      contentAsString = bp.getContent().toString();
  
                      senderAsString = sender[0].toString();
                      System.out.println("SENDER: " + senderAsString);
  
  
                      System.out.println("SENT DATE: " + sentDateAsString);
  
  
                      System.out.println("SUBJECT: " + subjectAsString);
                      System.out.println("CONTENT: " + contentAsString);
                      UIDAsString = String.valueOf(UIDAsLong);
  
                      System.out.println("UID: " + UIDAsString);
  
                      System.out.println("\n");
  
  
                  }
  
  
  
                  msgsLinkedHashMap.put("sender", senderAsString);
                  msgsLinkedHashMap.put("sentDate", sentDateAsString);
                  msgsLinkedHashMap.put("subject", subjectAsString);
                  msgsLinkedHashMap.put("content", contentAsString);
                  msgsLinkedHashMap.put("UID", UIDAsString);
  
  
  
                  msgsLinkedHashMapAsArrayList.add(msgsLinkedHashMap);
  
  
  
          }
  
  
  
          } catch (Exception mex) {
              // TODO Auto-generated catch block
              mex.printStackTrace();
          }
  </code></pre>
  
  <p>At this point the content that is added seems to be correct. For example, if I had 24 messages and the content was "msg1", "msg2", "msg3" etc then this system prints out the correct content.</p>
  
  <p>However when I loop through the HashMap after the following closing brackets all I get is the 24th element. In other words, the system will always print "msg24" even for the first 23 elements in list in the following code:</p>
  
  <pre><code>        for (int arrayListCounter = 0; arrayListCounter &lt; msgsLinkedHashMapAsArrayList.size(); arrayListCounter++) {
  
              LinkedHashMap&lt;String, String&gt; singleHashmapInArrayList = msgsLinkedHashMapAsArrayList.get(arrayListCounter);
  
              String content = singleHashmapInArrayList.get("content");
  
              System.out.println("&gt;&gt;&gt; " + arrayListCounter + " : " + content);
  </code></pre>
  
  <p>// For some reason content is always equal to msg24 instead of msg1, msg2, msg3 etc. It seems like the 24th element is replacing all previous elements. But why?
              }</p>
  
  <pre><code>        return msgsLinkedHashMapAsArrayList;
  
      }
  </code></pre>
  
  <p>I've been trying to figure out the problem for hours. </p>
  
  <p>What am I doing wrong?</p>