Java中Map集合简介说明
下文笔者讲述java中Map集合的简介说明,如下所示
遍历map方式性能总结
Map集合特性简介
Map中value可以重复,key不能重复 当插入一个数据时,此时key已存在,则会替换以前key处的value
Map集合的示例分享
import java.util.HashMap;
public class MapTest1{
public static void main(String[] args){
HashMap map = new HashMap();
map.put("a", "java265.com-1");
map.put("b", "java265.com-2");
map.put("c", "java265.com-3");
map.put("a", "java265.com-4");
System.out.println(map);
String value = (String)map.get("b");
System.out.println(value);
System.out.println("--------------");
String value2 = (String)map.get("d");
System.out.println(value2);
}
}
Map中entrySet()方法功能简介
entrySet()方法 :
返回map集合
keySet()方法:
返回key的集合
values()方法:
返回所有value值的Collection集合
例:
public class MapTest2{
public static void main(String[] args){
HashMap map = new HashMap();
String str = new String("java265.com");
map.put("a", str);
map.put("a", str);
System.out.println(map);
}
}
运用内部类Map.Entry来遍历Map
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " :" + entry.getValue());
}
例:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class MapTest3{
public static void main(String[] args){
HashMap map = new HashMap();
map.put("a", "java265.com-1");
map.put("b", "java265.com-2");
map.put("c", "java265.com-3");
map.put("d", "java265.com-4");
map.put("e", "java265.com-5");
Set set = map.keySet();
for(Iterator iter = set.iterator(); iter.hasNext();){
String key = (String)iter.next();
String value = (String)map.get(key);
System.out.println(key + "=" + value);
}
}
}
遍历map方式性能总结
1.增强for循环使用方便
但性能较差
不适合处理超大量级的数据
2.迭代器的遍历速度要比增强for循环快很多
是增强for循环的2倍左右
3.使用entrySet遍历的速度要比keySet快很多
是keySet的1.5倍左右
1.增强for循环使用方便
但性能较差
不适合处理超大量级的数据
2.迭代器的遍历速度要比增强for循环快很多
是增强for循环的2倍左右
3.使用entrySet遍历的速度要比keySet快很多
是keySet的1.5倍左右
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


