java遍历map(java遍历map并删除)
本篇文章给大家谈谈java遍历map,以及java遍历map并删除对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java遍历Map的几种方法分析
- 2、Java中怎样遍历Map的所有的元素
- 3、java中map的常用遍历方法有哪些?
- 4、Java中便历Map的几种方法
- 5、java Map 怎么遍历
- 6、java如何遍历map的所有的元素
java遍历Map的几种方法分析
1.先初始化一稿陆个map
public class TestMap {
public static MapInteger, Integer map = new HashMapInteger, Integer();
}
2.keySet values
如果只需要map的key或者value,用map的keySet或values方法无疑是最方便的
// KeySet 获取key
public void testKeySet() {
for (Integer key : map.keySet()) {
System.out.println(key);
}
}
// values 获取value
public void testValues() {
for (Integer value : map.values()) {
System.out.println(value);
}
}
3.keySet get(key)
如果需要同时获取key和value,可以先获取key,然后再通过map的get(key)获取value
需要说明的是,该方法不是最优选择,一般不推荐使用
// keySet get(key) 获取key and value
public void testKeySetAndGetKey() {
for (Integer key : map.keySet()) {
System.out.println(key + ":" + map.get(key));
}
}
4.entrySet
通过对map entrySet的遍历,也可以同时拿到key和value,一般情况下,性能上要优于上一种,这一种也是最常用的遍历方法
// entrySet 获取key and value
public void testEntry() {
for (Map.EntryInteger, Integer entry : map.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}
5.Iterator
对于上仿敬如面的几种foreach都可以用Iterator代替,其实foreach在java5中才被支持,foreach的写法看起来更简洁
但Iterator也有其优势:在用foreach遍历map时,如果改变其大小,会报错,但如果只是删除元素,可以使用Iterator的remove方法删除元素
// Iterator entrySet 获取key and value
public void testIterator() {
IteratorMap.EntryInteger, Integer备启 it = map.entrySet().iterator();
while (it.hasNext()) {
Map.EntryInteger, Integer entry = it.next();
System.out.println(entry.getKey() + ":" + entry.getValue());
// it.remove(); 删除元素
}
}
[img]Java中怎样遍历Map的所有的元素
JDK 中
view plaincopy to clipboardprint? FONT color=# ffMap 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=# ffprivate HashtableString String emails = new HashtableString String();/FONT
private HashtableString String emails = new HashtableString 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 EntryString 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) {
ArrayListString list = new ArrayListString();
HashMapObject Object hash = new HashMapObject Object();
TreeMapObject Object treeMap = new TreeMapObject 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 EntryObject 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
java中map的常用遍历方法有哪些?
ava中map的常用遍历的具体方法有:
一 、在for-each循环中使用entries来遍历。这是最御蔽常见的并且在大多数情况下也是最镇睁州可取的遍历方式。在键值都需要时使用。
二、 在for-each循环中遍历keys或values。如果只需要map中的键或者值,你可以通过keySet或values来实现遍历,而不是用entrySet。
三、使用Iterator遍历。
四、通过键找值遍历(效率低)。
总结:如果仅需要键(keys)或值(values)使用方法二。如果你使用的语言版本低于java 5,或是打算在遍历时删除entries,必须使用方法三。否则使用方法一(键值都要早伍)。
Java中便历Map的几种方法
常见的Map遍历有下面四种方法:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
public class MapDemo {
public static void main(String[] args) {
// 准备好需要遍历的Map
HashMapString, Integer map = new HashMapString, Integer();
map.put("Tom", 85);
map.put("Jack", 97);
test1(map);
test2(map);
test3(map);
test4(map);
}
// 方法一: 迭代器方式
// 特点: 效率高,速度快,但是代码量多
public static void test1(HashMapString, Integer map) {
IteratorEntryString, Integer it = map.entrySet().iterator();
while (it.hasNext()) {
EntryString, Integer e = it.next();
System.out.println("name:" + e.getKey() + "\tscore:" + e.getValue());
}
}
// 方法二: map.entrySet() for循环
// 特点: 效率也较丛乎局高,速度较快,且写法比方法一简单
public static void test2(HashMapString, Integer map) {
for (EntryString, Integer e : map.entrySet()) {
System.out.println("name:" + e.getKey() + "\tscore:" + e.getValue());
}
}
// 方法3 map.keySet for循环
// 特点:效率较慢
public static void test3(HashMapString, Integer map) {
for (String key : map.keySet()) {
System.out.println("name:" + key + "\tscore:" + map.get(key));
}
}
// 方法四: forEach
// 特点 速度较慢,但是代码少,简洁; (需要Java8或以上版本的支持)
public static void test4(HashMapString, Integer map) {
map.forEach((k, v) - System.out.println("name:" + k + "\tscore:" + v));
}
}
四种方法渗让之间的效率比较
(test1≈test2)(test3≈test4)
推荐: 数据量特别大的时候 使用方法1 : 代码长,但是效率高
数据量较少的, 那么使用方法4: 代码简洁而优顷氏雅~
java Map 怎么遍历
关于java中遍历map具体有四种方式,请看下文详解。
1、这是最常见的并且茄枝缓在大多数情况下也是最可取的遍历方式,在键值都需要时使用。
MapInteger, Integer map = new HashMapInteger, Integer();
for (Map.EntryInteger, Integer entry : map.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
2、在for-each循环中遍历keys或values。
如果只需要map中的键或者值,你可以通过keySet或values来实现遍历,而不是用entrySet。
MapInteger, Integer map = new HashMapInteger, 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遍历
使用泛型:
MapInteger, Integer map = new HashMapInteger, Integer();
IteratorMap.EntryInteger, Integer entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.EntryInteger, 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、通过键找值遍历(效率低)
MapInteger, Integer map = new HashMapInteger, 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
java如何遍历map的所有的元素
package net.nie.test; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class HashMapTest { private static MapInteger, String map=new HashMapInteger,String(); /** 1.HashMap 类映射不保证顺序;某些映射可明确保证其顺序: TreeMap 类 * 2.在遍历Map过程中,不能用map.put(key,newVal),map.remove(key)来修改和删除元素, * 会引发 并发修改异常,可以通过迭代器的remove(): * 从迭代器指向的 collection 中移除当前迭代元素 * 来达到删除访问中的元素的目的。谨败 * */ public static void main(String[] args) { map.put(1,"one"); map.put(2,"two"); map.put(3,"three"); map.put(4,"four"); map.put(5,"five"); map.put(6,"six"); map.put(7,"seven"); map.put(8,"eight"); map.put(5,"five"); map.put(9,"nine"); map.put(10,"ten"); IteratorMap.EntryInteger, String激晌拿 it = map.entrySet().iterator(); while(it.hasNext()){ Map.Entry明搭Integer, String entry=it.next(); int key=entry.getKey(); if(key%2==1){ System.out.println("delete this: "+key+" = "+key); //map.put(key, "奇数"); //ConcurrentModificationException //map.remove(key); //ConcurrentModificationException it.remove(); //OK } } //遍历当前的map;这种新的for循环无法修改map内容,因为不通过迭代器。 System.out.println("-------nt最终的map的元素遍历:"); for(Map.EntryInteger, String entry:map.entrySet()){ int k=entry.getKey(); String v=entry.getValue(); System.out.println(k+" = "+v); } } }
关于java遍历map和java遍历map并删除的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。