今天在中软机试的过程中用到了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时第三种效率最高。