如何向Map中添加数据

如何向Map中添加数据,第1张

向Map中添加数据的步骤如下:

1.首先使用insert方式插入三组数据到map容器中,然后遍历打印容器中存放的数据。

2.从输出的结果看,三组数据成功存放到map容器。

3.接下来仍然使用insert方式,但是插入的是value_type的数据。

4.从输出结果看,采用插入value_type数据显示现象与前一种方式是相同。这样就解决了向Map中添加数据的问题了。

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

package net.nie.test import java.util.HashMap import java.util.Iterator import java.util.Map public class HashMapTest { private static Map<Integer, String>map=new HashMap<Integer,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") Iterator<Map.Entry<Integer, 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.Entry<Integer, String>entry:map.entrySet()){ int k=entry.getKey() String v=entry.getValue() System.out.println(k+" = "+v) } } }


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/7993159.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存