java代码实现hashmap的插入,删除一个对象,并将value值放入数组中,并获取数据显示出来
1个回答
推荐于2016-11-01
展开全部
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class TestHm {
public static void main(String atgs[]){
HashMap hm=new HashMap();
//添加内容
hm.put("Object key1", "Object value");
hm.put("Object key2", "Object value");
hm.put("Object key3", "Object value");
hm.put("Object key4", "Object value");
//删除Object key3
hm.remove("Object key3");
//创建数组,声明i作为数组下标
String s[]=new String[hm.size()];
int i=0;
//显示内容
Set set=hm.keySet();
Iterator it=set.iterator();
while(it.hasNext()){
String t=(String)it.next();
System.out.println(t);
s[i++]=t;
}
//测试数组是否添加值
System.out.println("===================");
for(int t=0;t<=2;t++)
System.out.println(s[t]);
}
}
import java.util.Iterator;
import java.util.Set;
public class TestHm {
public static void main(String atgs[]){
HashMap hm=new HashMap();
//添加内容
hm.put("Object key1", "Object value");
hm.put("Object key2", "Object value");
hm.put("Object key3", "Object value");
hm.put("Object key4", "Object value");
//删除Object key3
hm.remove("Object key3");
//创建数组,声明i作为数组下标
String s[]=new String[hm.size()];
int i=0;
//显示内容
Set set=hm.keySet();
Iterator it=set.iterator();
while(it.hasNext()){
String t=(String)it.next();
System.out.println(t);
s[i++]=t;
}
//测试数组是否添加值
System.out.println("===================");
for(int t=0;t<=2;t++)
System.out.println(s[t]);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询