关于java中的HashMap的实例 *** 作

关于java中的HashMap的实例 *** 作,第1张

关于java中的HashMap的实例 *** 作

HashMap简介:

1、以(键,值)对存储数据。

2、不允许有重复的键,但允许有重复的值。

3、不同步(多个线程可以同时访问)

相关视频教程推荐:java在线学习

实例演示如下:

1、添加

HashMap<String, String> hash_map = new HashMap<String, String>(); 
 
hash_map.put( "名字" , "anny" );
hash_map.put( "性别" , "女" );
hash_map.put( "年龄" , "20" );

2、删除

HashMap<String, String> hash_map = new HashMap<String, String>(); 
 
hash_map.remove( "名字" );

3、遍历

一般性能:

HashMap<String, String> hash_map = new HashMap<String, String>(); 
 
hash_map.put( "名字" , "anny" );
hash_map.put( "性别" , "女" );
hash_map.put( "年龄" , "20" );

for(String key:hash_map.keySet())
{
  System.out.println("Key: "+key+" Value: "+hash_map.get(key));
}

使用 Collection 类的 iterator() 方法来遍历集合(性能好)

HashMap<String, String> hash_map = new HashMap<String, String>(); 
 
hash_map.put( "名字" , "anny" );
hash_map.put( "性别" , "女" );
hash_map.put( "年龄" , "20" );

Collection cl = hash_map.values();
Iterator itr = cl.iterator();
while (itr.hasNext()) {
    System.out.println(itr.next());
}

本文来自java入门程序栏目,为大家介绍关于HashMap的一些实例 *** 作,希望可以帮助到大家。

以上就是关于java中的HashMap的实例 *** 作的详细内容,

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

原文地址: https://outofmemory.cn/langs/689170.html

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

发表评论

登录后才能评论

评论列表(0条)

保存