Home > Java > javaTutorial > body text

About the example operation of HashMap in java

王林
Release: 2019-11-29 13:18:25
forward
2434 people have browsed it

About the example operation of HashMap in java

HashMap introduction:

1. Store data in (key, value) pairs.

2. Duplicate keys are not allowed, but duplicate values ​​are allowed.

3. Not synchronized (multiple threads can access at the same time)

Recommended related video tutorials: java online learning

Example demonstrations are as follows:

1. Add

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

2. Delete

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

3. Traverse

General performance:

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));
}
Copy after login

Use the iterator() method of the Collection class to traverse the collection (good performance)

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());
}
Copy after login

This article comes from the java entry program column to introduce you to some example operations about HashMap. Hope it helps everyone.

The above is the detailed content of About the example operation of HashMap in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template