---
title: "HashMap遍历"
author: "chy0919"
date: "2017-03-08T11:33:00Z"
tags: ["HashMap遍历"]
categories: ["编程语言"]
slug: "hashmap"
draft: false
lastmod: "2017-03-08T11:33:00Z"
id: 2360881
url: "https://chy0919.iteye.com/blog/2360881"
---

在日常的工作中经常会遇到遍历HashMap的情况，这里总结两种遍历方式，供大家参考

第一种：使用Entry工具类

        Map map = new HashMap();

        Iterator iterator = map.entrySet().iterator();

        while (iterator.hasNext()) {

                Map.Entry entry = (Map.Entry) iterator.next();

                Object key = entry.getKey();

                Object val = entry.getValue();

        }

第二种：逐一遍历

        Map map = new HashMap();

        Iterator iterator = map.keySet().iterator();

        while (iterator.hasNext()) {

                Object key = iterator.next();

                Object val = map.get(key);

        }

        两种遍历方法相比，第一种方式的效率高一些，在实际的使用中建议使用第一种进行遍历。