Map 遍历的四种方法和性能测试

今天在中软机试的过程中用到了Map,但当想到如何使用map时候大脑一片空白,现在把list map 内容的方法做个归纳总结。

一、 利用Map. keyset方法。 在map中方法定义如下: 

Set<K> keySet(); 

例如:

 for (String stuKey : stuMap.keySet()) {
			  stuStr = stuKey; 
			  stu = stuMap.get(stuKey);
		 }

二、 利用Map.Keyset().iterator()方法。

  Iterator<Map.Entry<String,Student>> it = stuMap.entrySet().iterator(); 
while (it.hasNext()) {
			 Map.Entry<String,Student> entry = it.next();
			 stuStr = entry.getKey(); 
			 stu = entry.getValue();
		 }


三、结合 Map.Entry<String, Student> entry 和 entrySet 方法:

 for (Map.Entry<String, Student> entry : stuMap.entrySet()) {
			 stuStr = entry.getKey(); 
			 stu = entry.getValue();
		 }


四、只能遍历values 用到方法是Map.values.

for (Student student : stuMap.values()) {
			 stu = student;
		}


下面分析下四种方法性能

我首先创建了一个测试内为: student,如下:

public class Student {

	private int age;
	private String name;

	public Student(String name, int age) {
		this.age = age;
		this.name = name;
	}
}


主类: 

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class MapListor {

	private static Map<String, Student> stuMap = new HashMap<String, Student> ();
	
	public static void main(String[] args) {
		
		intMap(200);
		
		//method 1: Map.Keyset()
		 long endTime = 0;
		 String stuStr = ""; // key used to be set when listing map.
		 Student stu = null; // value used to be set when listing map.
 		 long startTime = System.currentTimeMillis();
		 for (String stuKey : stuMap.keySet()) {
			  stuStr = stuKey; 
			  stu = stuMap.get(stuKey);
		 }
		 endTime = System.currentTimeMillis();
		 System.out.println("Using the first method to list Map("+ stuMap.size()+") costs: " + (endTime - startTime) );
		 
		 //method 2: Map.Keyset().iterator()
		 startTime = System.currentTimeMillis();
		 Iterator<Map.Entry<String,Student>> it = stuMap.entrySet().iterator(); 
		 while (it.hasNext()) {
			 Map.Entry<String,Student> entry = it.next();
			 stuStr = entry.getKey(); 
			 stu = entry.getValue();
		 }
		 endTime = System.currentTimeMillis();
		 System.out.println("Using the second method to list Map("+ stuMap.size()+") costs: " + (endTime - startTime) );
		 
		 //method 3: Map.Entry<String, Student> entry and entrySet method.
		 startTime = System.currentTimeMillis();
		 for (Map.Entry<String, Student> entry : stuMap.entrySet()) {
			 stuStr = entry.getKey(); 
			 stu = entry.getValue();
		 }
		 endTime = System.currentTimeMillis();
		 System.out.println("Using the third method to list Map("+ stuMap.size()+") costs: " + (endTime - startTime) ); 
		 
		 //method 4: only list values.
		 for (Student student : stuMap.values()) {
			 stu = student;
		}
	}

	/**
	 * init the stuMap according to the input number of students.
	 * @param stuNumber
	 */
	private static void intMap(int stuNumber) {
		Student stu = null;
		for (int i = 0; i < stuNumber; i++) {
			stu = new Student("student" + i, (int)Math.random() * 100);
			stuMap.put(String.valueOf(i), stu);
		}
	}
}

创建200运行结果: 
Using the first method to list Map(200) costs: 0
Using the second method to list Map(200) costs: 0
Using the third method to list Map(200) costs: 0


创建200000条数据结果:

Using the first method to list Map(200000) costs: 20
Using the second method to list Map(200000) costs: 17
Using the third method to list Map(200000) costs: 15


由上面可以得出: 在遍历大数据的Map时第三种效率最高。



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值