
关于java中遍历map具体有四种方式,请看下文详解。
1、这是最常见的并且在大多数情况下也是最可取的遍历方式,在键值都需要时使用。
Map<Integer, Integer>map = new HashMap<Integer, Integer>()
for (Map.Entry<Integer, Integer>entry : map.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue())
}
2、在for-each循环中遍历keys或values。
如果只需要map中的键或者值,你可以通过keySet或values来实现遍历,而不是用entrySet。
Map<Integer, Integer>map = new HashMap<Integer, Integer>()
for (Integer key : map.keySet()) {
System.out.println("Key = " + key)
}
for (Integer value : map.values()) {
System.out.println("Value = " + value)
}
该方法比entrySet遍历在性能上稍好(快了10%),而且代码更加干净。
3、使用Iterator遍历
使用泛型:
Map<Integer, Integer>map = new HashMap<Integer, Integer>()
Iterator<Map.Entry<Integer, Integer>>entries = map.entrySet().iterator()
while (entries.hasNext()) {
Map.Entry<Integer, Integer>entry = entries.next()
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue())
}
不使用泛型:
Map map = new HashMap()
Iterator entries = map.entrySet().iterator()
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next()
Integer key = (Integer)entry.getKey()
Integer value = (Integer)entry.getValue()
System.out.println("Key = " + key + ", Value = " + value)
}
4、通过键找值遍历(效率低)
Map<Integer, Integer>map = new HashMap<Integer, Integer>()
for (Integer key : map.keySet()) {
Integer value = map.get(key)
System.out.println("Key = " + key + ", Value = " + value)
}
假设Map中的键值对为1=>11,2=>22,3=>33,现用方法1来遍历Map代码和调试结果如下:
扩展资料:
1、HashMap的重要参数
HashMap 的实例有两个参数影响其性能:初始容量 和加载因子。容量是哈希表中桶的数量,初始容量只是哈希表在创建时的容量。
加载因子 是哈希表在其容量自动增加之前可以达到多满的一种尺度。当哈希表中的条目数超出了加载因子与当前容量的乘积时,则要对该哈希表进行 rehash *** 作(即重建内部数据结构),从而哈希表将具有大约两倍的桶数。
在Java编程语言中,加载因子默认值为0.75,默认哈希表元为101。
2、HashMap的同步机制
注意,此实现不是同步的。 如果多个线程同时访问一个哈希映射,而其中至少一个线程从结构上修改了该映射,则它必须保持外部同步。
(结构上的修改是指添加或删除一个或多个映射关系的任何 *** 作;以防止对映射进行意外的非同步访问,如下:
Map m = Collections.synchronizedMap(new HashMap(...))
参考资料:百度百科-Hashmap
ava中map的常用遍历的具体方法有:
一 、在for-each循环中使用entries来遍历。这是最常见的并且在大多数情况下也是最可取的遍历方式。在键值都需要时使用。
二、 在for-each循环中遍历keys或values。如果只需要map中的键或者值,你可以通过keySet或values来实现遍历,而不是用entrySet。
三、使用Iterator遍历。
四、通过键找值遍历(效率低)。
总结:如果仅需要键(keys)或值(values)使用方法二。如果你使用的语言版本低于java 5,或是打算在遍历时删除entries,必须使用方法三。否则使用方法一(键值都要)。
JDK 中
view plaincopy to clipboardprint? <FONT color=# ff>Map map = new HashMap()
Iterator it = map entrySet(erator()
while (it hasNext()) {
Map Entry entry = (Map Entry) it next()
Object key = entry getKey()
Object value = entry getValue()
}</FONT>
Map map = new HashMap()
Iterator it = map entrySet(erator()
while (it hasNext()) {
Map Entry entry = (Map Entry) it next()
Object key = entry getKey()
Object value = entry getValue()
}JDK 中 应用新特性For Each循环
view plaincopy to clipboardprint? Map m = new HashMap()
for(Object o : map keySet()){
map get(o)
}
Map m = new HashMap()
for(Object o : map keySet()){
map get(o)
}返回的 set 中的每个元素都是一个 Map Entry 类型
view plaincopy to clipboardprint? <FONT color=# ff>private Hashtable<String String>emails = new Hashtable<String String>()</FONT>
private Hashtable<String String>emails = new Hashtable<String String>() 另外 我们可以先把hashMap 转为集合Collection 再迭代输出 不过得到的对象
view plaincopy to clipboardprint? <FONT color=# ff>//方法一: 用entrySet()
Iterator it = emails entrySet(erator()
while(it hasNext()){
Map Entry m=(Map Entry)it next()
( email + m getKey() + : + m getValue())
}
// 方法二 jdk 支持 用entrySet()和For Each循环()
for (Map Entry<String String>m : emails entrySet()) {
( email + m getKey() + : + m getValue())
}
// 方法三 用keySet()
Iterator it = emails keySet(erator()
while (it hasNext()){
String key
key=(String)it next()
( email + key + : + emails get(key))
}
// 方法五 jdk 支持 用keySEt()和For Each循环
for(Object m: emails keySet()){
( email + m+ : + emails get(m))
} </FONT>
//方法一: 用entrySet()
Iterator it = emails entrySet(erator()
while(it hasNext()){
Map Entry m=(Map Entry)it next()
( email + m getKey() + : + m getValue())
}
// 方法二 jdk 支持 用entrySet()和For Each循环()
for (Map Entry<String String>m : emails entrySet()) {
( email + m getKey() + : + m getValue())
}
// 方法三 用keySet()
Iterator it = emails keySet(erator()
while (it hasNext()){
String key
key=(String)it next()
( email + key + : + emails get(key))
}
// 方法五 jdk 支持 用keySEt()和For Each循环
for(Object m: emails keySet()){
( email + m+ : + emails get(m))
}
Map aa = new HashMap() aa put( tmp new Object()) //追加 替换用同样的函数 aa remove( temp ) //删除 for (Iterator i = aa values(erator() i hasNext() ) { Object temp = i next() } //遍历 来个完整的 包含TreeSet的元素内部排序的
view plaincopy to clipboardprint? public static void main(String[] args) {
ArrayList<String>list = new ArrayList<String>()
HashMap<Object Object>hash = new HashMap<Object Object>()
TreeMap<Object Object>treeMap = new TreeMap<Object Object>()
list add( a )
list add( b )
list add( c )
hash put( )
hash put( )
hash put( )
hash put( )
hash put( )
hash put( )
treeMap put( )
treeMap put( )
treeMap put( )
treeMap put( )
treeMap put( )
treeMap put( )
//list遍历
for(String m: list){
System out println(m)
}
// hashmap entrySet() 遍历
for(Map Entry<Object Object>m: hash entrySet()){
System out println(m getKey()+ +m getValue())
}
//hashmap keySet() 遍历
for(Object m: hash keySet()){
System out println(m+ +hash get(m))
}
// treemap keySet()遍历
for(Object m: treeMap keySet()){
System out println(m+ +treeMap get(m))
}
lishixinzhi/Article/program/Java/hx/201311/25783
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)