Location>code7788 >text

hsahmap in java

Popularity:168 ℃/2025-02-01 12:12:48

HsahMap

HashMap is one of the most commonly used sets in Java.
The storage of hashmap is no orderly
HashMap stores key values ​​(Key-Value), the key Key is unique, and values ​​can be repeated.
The bottom layer of the hashmap is the array and linked list

Common ways of HashMap

Add method:
 1. PUT (K Key, V Value): Put the key value into HashMap.  If you add new content, return Null.  If the key already exists, the corresponding value of the update will be updated, and the value before the update will be returned
 2. PutiFabSENT (K Key, V Value) without this key, add up.  With this key, you will not be added to it

 Method:
 1. Replace (K Key, V Value) modify the data, and the key is Zhang San, updated to 19.  If you do n’t have this key, do n’t replace it (do n’t understand anything)

 How to get:
 1. get (Object Key): Get the corresponding value according to the key.  If the key does not exist, return NULL.  
 2. KeySet (): The return type is set, and the return value is the set of all keys in HashMap.
 3. Values ​​(): The return type is the collection, and the return value is the set of all values ​​in Hashmap.
 4. EntrySet (): Returns the set of all key values ​​pairs in HashMap.  Returns the set type.
 5, size (): Returns the number pair of key values ​​in HashMap.  The return type is int

 Delete method:
 1. Remove (Object Key): Delete the corresponding key value pair according to the key.  Remove the non -existing key name, returns null
 2, Remove (Object Key, Object Value): Delete the corresponding key name and corresponding value.  The type returned is Boolean
 3. Clear (): Clear HashMap and remove all key values ​​pairs.

 Check the element:
 1. Containskey (Object Key): The type returned is Boolean, determining whether the HashMap contains the specified key.
 2. Containsvalue (V Value) check whether it contains a value value.
 3, Isempty (): Check whether the hashmap is empty.  The Boolean value is returned.

 Clone method:
 1. Lone () to copy a hashmap object. The return value is an object, so it is necessary to use forced conversion, which is a shallow copy.
 Summary: The next method of learning a certain type is also like this.  It will be much better.

Added data put

package part;
 // Hashmap under the package of Java-> Util
 Import;
 public class java01 {
     Public Static Void Main (String [] args) {{
         HashMap Map = New HashMap ();
         ("zhangsan", "18");
         ("Lisi", "17");
         // Output {lisi = 17, zhangsan = 18}
         (MAP);
     }
 }

Add new content and return NULL. If the key already exists, the corresponding value will be updated

package part;
 // Hashmap under the package of Java-> Util
 Import;
 public class java01 {
     Public Static Void Main (String [] args) {{
         HashMap Map = New HashMap ();
         ("zhangsan", "18");
         // If you add new content, return to NULL.
         ("Lisi", "17");
         // This time the key name and the first key name, repeat, will cover it.  Because the key name is unique.  If the key already exists, return the value before the update.  Output 18
         ("" zhangsan "," 25 ");
         // Output {lisi = 17, zhangsan = 25}
         (MAP);
     }
 }

HashMap stores key value pair, key key is the only one

package part;
 // Hashmap under the package of Java-> Util
 Import;
 public class java01 {
     Public Static Void Main (String [] args) {{
         HashMap Map = New HashMap ();
         ("zhangsan", "18");
         ("Lisi", "17");
         // This time the key name and the first key name, repeat, will cover it.  Because the key name is unique
         ("zhangsan", "25");
         // Output {lisi = 17, zhangsan = 25}
         (MAP);
     }
 }

PutiFabSENT has no new key without this key. With this key, you will not be added to it

package part;
 // Hashmap under the package of Java-> Util
 Import;
 public class java01 {
     Public Static Void Main (String [] args) {{
         HashMap Map = New HashMap ();
         ("zhangsan", "18");
         // Without this key, add up.  With this key, you will not be added to it
         ("zhangsan", "19");
         (MAP);
     }
 }

Modify data: Replace replace

package part;
 // Hashmap under the package of Java-> Util
 Import;
 public class java01 {
     Public Static Void Main (String [] args) {{
         HashMap Map = New HashMap ();
         ("zhangsan", "18");
         // The key is Zhang San, updated to 19.  If you do n’t have this key, do n’t replace it (do n’t understand anything)
         ("zhangsan", "199");
         // Nothing to do
         ("lisi", "19");
         (map); // {zhangsan = 199}
     }
 }

KeySet (): Return to the set of all keys in HashMap.

package part;
 // Hashmap under the package of Java-> Util
 Import;
 Import;
 public class java01 {
     Public Static Void Main (String [] args) {{
         HashMap Map = New HashMap ();
         ("zhangsan", "18");
         ("lisi", "19");
         // keyset (): Return to the set of all keys in HashMap, the return type is set
         Set set = ();
         (set); // [lisi, zhangsan]
     }
 }

Values ​​(): Returns the collection of all values ​​in HashMap

package part;
 // Hashmap under the package of Java-> Util
 Import;
 Import;
 Import;

 public class java01 {
     Public Static Void Main (String [] args) {{
         HashMap Map = New HashMap ();
         ("zhangsan", "18");
         ("lisi", "19");
         // (): Return all the sets of all values ​​in HashMap.  The type is: Collection
         Collection list = ();
         (list); // [19, 18]
     }
 }

Enhanced for cycle traversal

public class java01 {
     Public Static Void Main (String [] args) {{
         HashMap Map = New HashMap ();
         ("zhangsan", "18");
         ("lisi", "19");
         // Get the collection of all key names
         Set set = ();
         for (object key: set) {
             // Output 19 18
             ((key);
         }
     }
 }

containSkey (Object Key): Determine whether the HashMap contains the specified key, and return is a Boolean value

package part;
 // Hashmap under the package of Java-> Util
 Import;
 Import;
 Import;

 public class java01 {
     Public Static Void Main (String [] args) {{
         HashMap Map = New HashMap ();
         ("zhangsan", "18");
         ("lisi", "19");
         // Containskey (Object Key): Determine whether there is a specified key in HashMap.  Return is a Boolean value
         BoOLEAN BOOL = ("zhangsan");
         (BOOL); // True
     }
 }

ContainsValue (V Value) checks whether it contains a value value. If it contains returning true, it does not include returning false

package part;
 // Hashmap under the package of Java-> Util
 Import;
 Import;
 Import;

 public class java01 {
     Public Static Void Main (String [] args) {{
         HashMap Map = New HashMap ();
         ("zhangsan", "18");
         ("lisi", "19");
         // Containsvalue (V Value) check whether it contains a value value.
         (("19")); // true
     }
 }

EntrySet (): Returns the collection of all key values ​​in HashMap.

package part;
 // Hashmap under the package of Java-> Util
 Import;
 Import;
 Import;
 Import;

 public class java01 {
     Public Static Void Main (String [] args) {{
         // The key name is a string, and the key value is also a string. We use generics for constraints
         HashMap <string, String> Map = New HashMap ();
         ("zhangsan", "18");
         ("lisi", "19");
         // The following is an interface, which is written like this
         Set << String, String >> Keyitem = ();
         For (<string, String> Entry: Keyitem) {
             // Traversing each item
             // lisi 19
             // zhangsan 18
             (Entry);
         }
     }
 }

Remove (Object Key) delete

package part;
 Import;
 public class java01 {
     Public Static Void Main (String [] args) {{
         // The key name is a string, and the key value is also a string. We use generics for constraints
         HashMap <string, String> Map = New HashMap ();
         ("zhangsan", "18");
         ("lisi", "19");
         // Delete Zhang San's key name.  At this time, the map is only LISI.
         ("zhangsan");
         // Delete the key name is LISI, which is the value of 100, and the return value is a Boolean type
         BoOLEAN FLAG = ("Lisi", "100");
         (Flag); // False
         (MAP);
     }
 }

Clone shallow copy, the return type is the object, you need to be mandatory conversion

package part;
 Import;
 public class java01 {
     Public Static Void Main (String [] args) {{
         // The key name is a string, and the key value is also a string. We use generics for constraints
         HashMap <string, String> Map = New HashMap ();
         ("zhangsan", "18");
         ("lisi", "19");
         // Clone is a shallow copy, and it is an object that returns, so it needs to be mandatory.
         HashMap <string, String> Newmap = (HashMap <string, String>);
         // {zhangsan = 18, lisi = 19}
         (Newmap);
     }
 }

end

Ready to start learning Java.
On the fifth day of studying today, I will publish articles every day, and I will get rolled up.
Ask your friends to supervise me, Oli gives