Map.Entry 和 Map.entrySet() ,用类遍历hashMap,hashTable

最近再写一段很恶心的系统代码,上网搜索资料时,偶然见看到这篇文章不错,给了我很多的启发。先收藏一下,作为以后开发备用资料。希望看到的朋友也能多多学习学习!

发现HashMap 用到下面的格式 ,直接循环遍历整个hashMap  hashTable

返回的 set 中的每个元素都是一个 Map.Entry 类型。

private Hashtable<String, String> emails = new Hashtable<String, String>();

//      方法一: 用entrySet()
//  Iterator it = emails.entrySet().iterator();
//  while(it.hasNext()){
//   Map.Entry m=(Map.Entry)it.next();
//   logger.info("email-" + m.getKey() + ":" + m.getValue());
//  }
  
  // 方法二:直接再循环中
  for (Map.Entry<String, String> m : emails.entrySet()) {
   
   logger.info("email-" + m.getKey() + ":" + m.getValue());
  }
  
  // 方法三:用keySet()
  Iterator it = emails.keySet().iterator();
  while (it.hasNext()){
   String key;
   key=(String)it.next();
   logger.info("email-" + key + ":" + emails.get(key));
  }

 

另外 我们可以先把hashMap 转为集合Collection,再迭代输出,不过得到的对象

  Map   aa   =   new   HashMap();  
  aa.put("tmp1",   new   Object());     //追加     替换用同样的函数.  
  aa.remove("temp1");                       //删除  
  for   (Iterator   i   =   aa.values().iterator();   i.hasNext();   )   {  
          Object   temp   =   i.next();  
  }         //遍历  

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,HashMapHashtable都是基于键值对存储数据的,且都是线程不安全的。 下面是向HashMapHashtable中输入Person对象的代码: ```java import java.util.HashMap; import java.util.Hashtable; import java.util.Map; public class Main { public static void main(String[] args) { // 新建Person对象 Person person1 = new Person(1, "Alice", 18); Person person2 = new Person(2, "Bob", 20); Person person3 = new Person(3, "Charlie", 22); // 使用HashMap存储Person对象 Map<Integer, Person> hashMap = new HashMap<>(); hashMap.put(person1.getId(), person1); hashMap.put(person2.getId(), person2); hashMap.put(person3.getId(), person3); // 使用Hashtable存储Person对象 Map<Integer, Person> hashtable = new Hashtable<>(); hashtable.put(person1.getId(), person1); hashtable.put(person2.getId(), person2); hashtable.put(person3.getId(), person3); } } class Person { private int id; private String name; private int age; public Person(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public String getName() { return name; } public int getAge() { return age; } } ``` 接下来是遍历HashtableHashMap的代码: ```java import java.util.HashMap; import java.util.Hashtable; import java.util.Iterator; import java.util.Map; public class Main { public static void main(String[] args) { // 新建Person对象 Person person1 = new Person(1, "Alice", 18); Person person2 = new Person(2, "Bob", 20); Person person3 = new Person(3, "Charlie", 22); // 使用HashMap存储Person对象 Map<Integer, Person> hashMap = new HashMap<>(); hashMap.put(person1.getId(), person1); hashMap.put(person2.getId(), person2); hashMap.put(person3.getId(), person3); // 使用Hashtable存储Person对象 Map<Integer, Person> hashtable = new Hashtable<>(); hashtable.put(person1.getId(), person1); hashtable.put(person2.getId(), person2); hashtable.put(person3.getId(), person3); // 遍历HashMap long startHashMap = System.currentTimeMillis(); Iterator<Map.Entry<Integer, Person>> hashMapIterator = hashMap.entrySet().iterator(); while (hashMapIterator.hasNext()) { Map.Entry<Integer, Person> entry = hashMapIterator.next(); System.out.println("HashMap: key=" + entry.getKey() + ", value=" + entry.getValue()); } System.out.println("HashMap遍历时间:" + (System.currentTimeMillis() - startHashMap) + "ms"); // 遍历Hashtable long startHashtable = System.currentTimeMillis(); Iterator<Map.Entry<Integer, Person>> hashtableIterator = hashtable.entrySet().iterator(); while (hashtableIterator.hasNext()) { Map.Entry<Integer, Person> entry = hashtableIterator.next(); System.out.println("Hashtable: key=" + entry.getKey() + ", value=" + entry.getValue()); } System.out.println("Hashtable遍历时间:" + (System.currentTimeMillis() - startHashtable) + "ms"); } } class Person { private int id; private String name; private int age; public Person(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public String getName() { return name; } public int getAge() { return age; } } ``` 通过测试可以发现,HashMap遍历速度比Hashtable更快。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值