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!