在做项目的时候有邮件发送方面的业务.对邮件的收发的业务不是很清楚,这几天重点在恶搞JavaMail.邮件系统的开发在一般的项目中都会用到,可以看出这一块的知识点用到的频率很高,所以必须来掌握它.这是老大写的一个邮件接口实现类(项目中的真实代码):
package com.tq.platform.service.core.impl;
import java.util.Iterator;
import java.util.Map;
import javax.activation.DataSource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.MailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import com.tq.platform.service.core.MailManager;
/**
* 邮件接口实现类
*
* @author Vincent
*/
public class MailManagerImpl extends BaseManagerImpl implements MailManager {
private MailSender sender;
public void setSender(MailSender sender) {
this.sender = sender;
}
public void sendMessage(String from, String[] to, String subject,
String content) {
sendMessage(from, to, subject, content, null);
}
public void sendMessage(String from, String[] to, String subject,
String content, Map<String, DataSource> attachments) {
try {
MimeMessage message = ((JavaMailSenderImpl) sender).createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true,"UTF-8");
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
if (attachments != null) {
for (Iterator<String> iterator = attachments.keySet().iterator(); iterator.hasNext();) {
String fileName = iterator.next();
helper.addAttachment(fileName, attachments.get(fileName));
}
}
((JavaMailSenderImpl) sender).send(message);
}
catch (MessagingException e) {
e.printStackTrace();
}
}
}
今天在看这段代码的时候我发现了一个小小的问题那就是老大写的这个遍历:
for (Iterator<String> iterator = attachments.keySet().iterator(); iterator.hasNext();) {
String fileName = iterator.next();
helper.addAttachment(fileName, attachments.get(fileName));
}
其实这样是没问题的.
但我想用entrySet()是不是效率更高一点呢!!!
for (Iterator<Entry<String, DataSource>> iterator = attachments
.entrySet().iterator(); iterator.hasNext();) {
Entry<String, DataSource> fileName = iterator.next();
helper.addAttachment(fileName.toString(), attachments.get(fileName));
}
当然在数据并发量不大的系统上entrySet()的优势并不明显.
这里我也对entrySet(), keySet()的迭代时间做了一个简单的测试,其 entrySet()的遍历速度并不比keySet()的速度快,但在其应用开发中这些小细节一定要知道.
/**
* @Title: MapTest.java
* @Package: cn.tq.java.Test
* @Description: TODO(用一句话描述该文件做什么)
* @author: 何枫
* @date: 2011-9-8 上午11:14:27
* @version: V1.0
*/
package cn.tq.java.Test;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
/**
* @Title: MapTest.java
* @Package: cn.tq.java.Test
* @Description: TODO(用一句话描述该文件做什么)
* @author: 何枫
* @date: 2011-9-8 上午11:14:27
* @version: V1.0
*/
public class MapTest {
/**
* @Title: main
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param args 设定文件
* @return void 返回类型
* @throws 测试keySet
* ()与entrySet()的迭代时间 keySet():迭代后只能通过get()取key
* entrySet():迭代后可以e.getKey(),e.getValue()取key和value.返回的是Entry接口
* 其通过代码的形式验证keySet()的速度比entrySet()慢了很多
*/
public static void main(String[] args) {
Map<String, String> kmap = new HashMap<String, String>();
Map<String, String> smap = new HashMap<String, String>();
for (int k = 0; k < 100; k++) {
kmap.put("" + k, "kmap");
}
for (int s = 0; s < 100; s++) {
smap.put("" + s, "smap");
}
long ctimesStart = Calendar.getInstance().getTimeInMillis();
System.out.println(stimesStart + " " + ctimesStart + " "
+ dtimesStart);
System.out.println("初始化开始时间:" + stimesStart);
Iterator<String> ktor = kmap.keySet().iterator();
while (ktor.hasNext()) {
System.out.print(ktor.next() + "\t");
}
/*
* Iterator<Entry<String, String>> stor = smap.entrySet().iterator();
* while(stor.hasNext()){ Entry<String, String> s = stor.next();
* System.out.print(s.getKey()+"\t");
* //System.out.print(stor.next().getKey()+"\t"); }
*/
long ctimesEnd = Calendar.getInstance().getTimeInMillis();
System.out.print("\n");
System.out.print("初始化结束时间:" + ctimesEnd + "\n");
System.out.print((ctimesEnd - ctimesStart));
}
}