0
我正在閱讀使用JavaMail API的電子郵件並將每個消息(以及發件人,發送日期等)添加到LinkedHashMap中。接下來,我將LinkedHashMap添加到LinkedHashMaps的ArrayList。然而,奇怪的是整個Hashmapis被最後一個條目覆蓋。無法將元素添加到Java中的LinkedHashMap
我有以下代碼:
private ArrayList<LinkedHashMap<String, String>> readAllMsgsFromOutlook() throws FileNotFoundException, IOException, MessagingException
{
LinkedHashMap<String, String> msgsLinkedHashMap = new LinkedHashMap<String, String>();
ArrayList<LinkedHashMap<String, String>> msgsLinkedHashMapAsArrayList = new ArrayList<LinkedHashMap<String, String>>();
//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 < 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();
}
此時被添加的內容似乎是正確的。例如,如果我有24條消息,內容是「msg1」,「msg2」,「msg3」等,則系統會打印出正確的內容。
但是,當我在下面的右括號之後遍歷HashMap時,我所得到的只是第24個元素。換句話說,系統將始終打印「msg24」即使在列表中的第23個要素下面的代碼:
for (int arrayListCounter = 0; arrayListCounter < msgsLinkedHashMapAsArrayList.size(); arrayListCounter++) {
LinkedHashMap<String, String> singleHashmapInArrayList = msgsLinkedHashMapAsArrayList.get(arrayListCounter);
String content = singleHashmapInArrayList.get("content");
System.out.println(">>> " + arrayListCounter + " : " + content);
//出於某種原因,內容始終等於msg24而不是MSG1,MSG2,消息3等等。這似乎是第24個元素將取代所有以前的元素。但爲什麼? }
return msgsLinkedHashMapAsArrayList;
}
我一直在試圖找出問題的幾個小時。
我在做什麼錯?