Detailed explanation of examples of HashMap traversal and use in Java
Several traversal methods of map:
Map< String, String> map = new HashMap<>(); map.put("aa", "@sohu.com"); map.put("bb","@163.com"); map.put("cc", "@sina.com"); System.out.println("普通的遍历方法,通过Map.keySet遍历key和value");//普通使用,二次取值 for (String key : map.keySet()) { System.out.println("key= "+key+" and value= "+map.get(key)); } System.out.println("通过Map.entrySet使用iterator遍历key和value:"); Iterator<Map.Entry<String, String>> it = map.entrySet().iterator(); while(it.hasNext()){ Map.Entry<String, String> entry = it.next(); System.out.println("key= "+entry.getKey()+" and value= "+entry.getValue()); } System.out.println("通过Map.entrySet遍历key和value"); //推荐这种,特别是容量大的时候 for(Map.Entry<String, String> entry : map.entrySet()){ System.out.println("key= "+entry.getKey()+" and value= "+entry.getValue()); } System.out.println(“通过Map.values()遍历所有的value,但不能遍历key”); for(String v : map.values()){ System.out.println("value = "+v); }
The connection and difference between HashMap and Hashtable
The implementation principles are the same, the functions are the same, the bottom layer They are all hash table structures, query speed is fast, and can be interoperated in many cases. Early versions are generally safe.
HashMap and Hashtable both implement the Map interface, but before deciding which one to use, you must first understand the difference between them. The main differences are: thread safety, synchronization, and speed.
HashMap is almost equivalent to Hashtable, except that HashMap is non-synchronized and can accept null (HashMap can accept null keys and values, while Hashtable Otherwise).
HashMap is non-synchronized, while Hashtable is synchronized, which means that Hashtable is thread-safe and multiple threads can share a Hashtable; and if there is no correct synchronization, multiple threads are HashMap cannot be shared. Java 5 provides ConcurrentHashMap, which is a replacement for HashTable and has better scalability than HashTable.
Another difference is that HashMap's iterator (Iterator) is a fail-fast iterator, while Hashtable's enumerator iterator is not fail-fast. So when other threads change the structure of the HashMap (add or remove elements), a ConcurrentModificationException will be thrown, but the remove() method of the iterator itself will not throw a ConcurrentModificationException when removing elements. But this is not a guaranteed behavior, it depends on the JVM. This is also the difference between Enumeration and Iterator.
Because Hashtable is thread-safe and synchronized, it is slower than HashMap in a single-threaded environment. If you don't need synchronization and only need a single thread, then using HashMap will perform better than Hashtable.
HashMap cannot guarantee that the order of elements in the Map will remain unchanged over time.
Characteristics of hashmap
HashMap is a subclass of the map interface and is an object that maps keys to values, where keys and values are Object, not thread-safe
hashMap uses a hash table to store map keys
key is unordered and unique, one can be null
value is unordered and not unique, there can be pairs of null
linkedHashMap Use a hash table to store the keys in the map, and use a linked doubly linked list to manage the order
What we use most is HashMap. To insert, delete and locate elements in the Map, HashMap is the best s Choice. If the order of output needs to be the same as the order of input, then LinkedHashMap can be used. It can also be arranged in the order of reading.
HashMap is the most commonly used Map, which is based on the key The hashCode value stores data, and its value can be obtained directly according to the key, which has fast access speed. HashMap only allows the key of one record to be NULL, and allows the value of multiple records to be NULL. HashMap does not support thread synchronization, that is, multiple threads can write HashMap at the same time at any time, which may cause data inconsistency.
If synchronization is required, you can use the synchronizedMap method of Collections to make HashMap synchronized. LinkedHashMap saves the insertion order of records. When using Iterator to traverse LinkedHashMap, the record obtained first must be inserted first.
HashMap can be synchronized through the following statement:
Map m = Collections.synchronizeMap(hashMap);
Comparison of the efficiency of several commonly used collections
The above is the detailed content of Detailed explanation of examples of HashMap traversal and use in Java. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.