Home  >  Article  >  Java  >  About the example operation of HashMap in java

About the example operation of HashMap in java

王林
王林forward
2019-11-29 13:18:252376browse

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" );

2. Delete

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

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));
}

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

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete