ハッシュ地図詳細説明と例

ディレクトリ


Javaでアプリケーションと例のHashMap

HashMapの Javaの部分からは、それが基本マップJavaマッピング・インタフェースを提供し、1.2から設定されています。(キー、値)のデータを格納することによって、キーと値のペアです。私は、対応するキーの値を知って取得したいです。それはハッシュと呼ばれる技術を使用しているため、名前のHashMapを使用する理由、ハッシングは長い文字列が同じ短い文字列と文字列に変換することが可能です。給付の種類別セグメントの文字列より速く、インデックスマークです。また、時々 、キーと値のペアを格納するためのリンクリストを使用して、HashSetのHashMapを使用しています。

HashMapのいくつかの重要な属性:

  • HashMapのは、パッケージjava.utilでの一部です。
  • HashMapのは、子抽象クラスAbstractMapの統合、およびクラスAbstractMap Mapインタフェースは、非フェッチを提供します。
  • それはまた、配列をクローニングすることができ、インタフェースは、前述の説明でKおよびVは、キーと値を表す実装します
  • 重複キーは、HashMapを許可が、重複した値、すなわち、単一のキーはより多くの価値を持つことができないが、複数のキーが同じ値を含むことができることが可能になります。
  • 唯一の空のHashMapは、より多くのスペースを可能にするが、キーの値を可能に
  • このクラスは、それは順序が同じにとどまっている、それはハッシュテーブル似ていますが、同期していない保証はありません、マッピングの順序を保証するものではありません。

すべての画像

HashMapの内部構造

内部HashMapのクラスは、4つのフィールドが含まれているようにノードを表すことができ、前記ノードのアレイを含んでいます。

  1. int型のハッシュ
  2. Kキー
  3. V値
  4. ノード<K、V>次の

ノードは、オブジェクト自体への参照が含まれているので、これは、リンクされたリストです。
HashMapの:
すべての画像

HashMapのパフォーマンス

  1. 初期容量(初期容量)
  2. 負荷率(負荷率)
    • \(負荷率= \ {ハッシュテーブルのサイズに格納されたテーブル内の要素の数} cfrac {} \)
    • 例:16の内部容量は、負荷率が0.75である場合、テーブルは12個の要素を有する場合、次いで、バケット番号が自動的に増加するであろう。

すなわち、容器搬送能力の量、すなわちでインスタンスのHashMap一度初期容量負荷率は、ときに再ハッシュに停止する必要性を使用して測定します。再ハッシュのステップは、能力を向上させるために使用されます。HashMapの容量で2倍されます。負荷率はまた、これらの部品が再ハッシュ前に充填されることを可能にする尺度です。HashMapの生成増加のエントリ数と電流容量の負荷率が、容量が増加するとき、それは再ハッシュされました。初期容量は、再ハッシュを停止しないので、増加し続けているが、彼は慎重に彼のパフォーマンスを向上させるために選択しなければならないので、絶えず増加した初期容量で、それは、反復時間の複雑さを増大させた場合は初期容量を設定するとき、それが期待されなければなりませんアカウントへの値。負荷率は一般的に0.75に設定され、それが過ごした空間と時間のバランスが良いです。0-1間の負荷係数値

同期したHashMap

それは同時にアクセスのHashMapの非同期複数のスレッドができています。複数のスレッドが同じクラスにアクセスすると、彼は外部で同期をとることが必要であるように、彼の構造を変更するには、少なくとも一つのスレッドされている場合。それが完了するまでに、パッケージのいくつかの同期マッピングによってオブジェクトです。そのようなオブジェクトが存在しない場合は、HashMapの同期を行うため、および偶発的な非同期アクセスを避けるために)(Collections.synchronizedMapにカプセル化することができます。
例としては、次のとおりです:

Map m = Collections.synchronizedMap(new HashMap(...));Map 

今、地図メートルの同期があります

あなたはイテレータクラスイテレータを作成した後、任意の構造の変更を(反復子のremoveメソッドを介してすべての道を除く)作る場合は迅速に障害が発生しています。イテレータが失敗した場合、それはConcurrentModificationExceptionをスローします。

HashMapのコンストラクタ

HashMapのは、各アクセス修飾子は公開され、4つのコンストラクタを提供します。

  1. HashMapの()デフォルトのコンストラクタはHashMapのインスタンス、16の初期容量、0.75の負荷率を生成します。
  2. HashMapの(INT初期容量):これは、HashMapのインスタンスを作成し、指定された初期容量と0.75の負荷率を持つインスタンス。
  3. HashMapの(INT初期容量、フロートloadFactor)は:これは、HashMapのインスタンス、指定された初期容量と指定された負荷率とのインスタンスを作成します。
  4. HashMapの(地図マップ):これは、指定されたマッピングのHashMapのインスタンスを作成するために同じマッピングを使用しています。

例:

    // Java program to illustrate 
    // Java.util.HashMap 
  
    import java.util.HashMap; 
    import java.util.Map; 
    
    public class GFG { 
        public static void main(String[] args) 
        { 
  
        HashMap<String, Integer> map 
            = new HashMap<>(); 
  
        print(map); 
        map.put("vishal", 10); 
        map.put("sachin", 30); 
        map.put("vaibhav", 20); 
  
        System.out.println("Size of map is:-"+ map.size()); 
  
        print(map); 
        if (map.containsKey("vishal")) { 
            Integer a = map.get("vishal"); 
            System.out.println("value for key"+ " \"vishal\" is:- "+ a); 
        } 
  
        map.clear(); 
        print(map); 
    } 
  
    public static void print(Map<String, Integer> map) 
    { 
        if (map.isEmpty()) { 
            System.out.println("map is empty"); 
        } 
  
        else { 
            System.out.println(map); 
        } 
    } 
} 

出力:

map is empty  
Size of map is:- 3  
{vaibhav=20, vishal=10, sachin=30}  
value for key "vishal" is:- 10  
map is empty

HashMapの時間の複雑さ

ハッシュ関数が適切に準備され、正しくgetおよびputを使用し、その後、バケット内の様々な要素の間で分散すれば、基本的な操作のHashMapの定数時間の複雑さを提供します。すべてのHashMap HashMapの容量を通って、キーの数に依存 - 値のペア。一般的に言えば、それはサイズと容量+に比例しています。バケットのHashMapの容量。だから、予約多数のバケツ無愛想のHashMapで始まりました。

HashMapのメソッド

)(明確な空1.:マップからマッピングをすべて削除します。

  • 構文Hash_Map.clear()
  • パラメータ:ボイド
  • 戻り値:なし戻り値
  • 例としては、次のとおりです:
 //将字符串映射成为整数键
 // Java code to illustrate the clear() method 
 import java.util.*; 

 public class Hash_Map_Demo { 
     public static void main(String[] args) 
     { 

         // Creating an empty HashMap 
         HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 
 
         // Mapping string values to int keys 
         hash_map.put(10, "pomelos"); 
         hash_map.put(15, "4"); 
         hash_map.put(20, "pomelos"); 
         hash_map.put(25, "Welcomes"); 
         hash_map.put(30, "You"); 
 
         // Displaying the HashMap 
         System.out.println("Initial Mappings are: " + hash_map); 
 
         // Clearing the hash map using clear() 
         hash_map.clear(); 
 
         // Displaying the final HashMap 
         System.out.println("Finally the maps look like this: " + hash_map); 
       } 
   } 

出力:

Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
Finally the maps look like this: {}
//将整数映射成为字符串
// Java code to illustrate the clear() method 
import java.util.*; 

public class Hash_Map_Demo { 
    public static void main(String[] args) 
    { 

        // Creating an empty HashMap 
        HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); 

        // Mapping int values to string keys 
        hash_map.put("pomelos", 10); 
        hash_map.put("4", 15); 
        hash_map.put("pomelos", 20); 
        hash_map.put("Welcomes", 25); 
        hash_map.put("You", 30); 

        // Displaying the HashMap 
        System.out.println("Initial Mappings are: " + hash_map); 

        // Clearing the hash map using clear() 
        hash_map.clear(); 

        // Displaying the final HashMap 
        System.out.println("Finally the maps look like this: " + hash_map); 
    } 
} 

出力:

Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25}
Finally the maps look like this: {}

2.ブールのcontainsKey(key_element)指定されたキーのマッピングが存在するか否かを問い合わせます

  • 構文Hash_Map.containsKey(key_element)
  • パラメータ:あなたは、クエリの要素をマッピングするマップでのみkey_elementパラメータ点。
  • 戻り値:戻り値のみトゥーレと偽
  • 例としては、次のとおりです:
 //将字符串映射为整数
 // Java code to illustrate the containsKey() method 
 import java.util.*; 
 
 public class Hash_Map_Demo { 
     public static void main(String[] args) 
     { 

     // Creating an empty HashMap 
     HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 

     // Mapping string values to int keys 
     hash_map.put(10, "pomelos"); 
     hash_map.put(15, "4"); 
     hash_map.put(20, "pomelos"); 
     hash_map.put(25, "Welcomes"); 
     hash_map.put(30, "You"); 

     // Displaying the HashMap 
     System.out.println("Initial Mappings are: " + hash_map); 

     // Checking for the key_element '20' 
     System.out.println("Is the key '20' present? " +  
     hash_map.containsKey(20)); 

     // Checking for the key_element '5' 
     System.out.println("Is the key '5' present? " +  
     hash_map.containsKey(5)); 
     } 
 } 

出力:

Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
 Is the key '20' present? true
 Is the key '5' present? false
//将整数映射成为字符串
// Java code to illustrate the containsKey() method 
 import java.util.*; 
 
 public class Hash_Map_Demo { 
     public static void main(String[] args) 
     { 

     // Creating an empty HashMap 
     HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); 

     // Mapping int values to string keys 
     hash_map.put("pomelos", 10); 
     hash_map.put("4", 15); 
     hash_map.put("pomelos", 20); 
     hash_map.put("Welcomes", 25); 
     hash_map.put("You", 30); 

     // Displaying the HashMap 
     System.out.println("Initial Mappings are: " + hash_map); 

     // Checking for the key_element 'Welcomes' 
     System.out.println("Is the key 'Welcomes' present? " +  
     hash_map.containsKey("Welcomes")); 

     // Checking for the key_element 'World' 
     System.out.println("Is the key 'World' present? " +  
     hash_map.containsKey("World")); 
     } 
 } 

出力:
Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25} Is the key 'Welcomes' present? true Is the key 'World' present? false

3.ブールのcontainsValue(Object値):すべてのマッピングを削除する特定のキーの値

  • 语法:Hash_Map.containsValue(オブジェクトの値)
  • パラメータ:このメソッドは、オブジェクト・タイプの一つのパラメータの値をとり、参照がマッピング内の任意のキーの値をチェックすることによって、マッピングされるべきです。
  • 戻り値:マッピング値が検出された場合、この方法は、真のブール値を返し、残りのケースはfalseです。
  • 時間の複雑さ:O(n)は、
  • 例としては、次のとおりです:
// 将字符串映射为整数
// Java code to illustrate the containsValue() method 
 import java.util.*; 
 
 public class Hash_Map_Demo { 
     public static void main(String[] args) 
     { 

     // Creating an empty HashMap 
     HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 

     // Mapping string values to int keys 
     hash_map.put(10, "pomelos"); 
     hash_map.put(15, "4"); 
     hash_map.put(20, "pomelos"); 
     hash_map.put(25, "Welcomes"); 
     hash_map.put(30, "You"); 

     // Displaying the HashMap 
     System.out.println("Initial Mappings are: " + hash_map); 

     // Checking for the Value 'pomelos' 
     System.out.println("Is the value 'pomelos' present? " +  
     hash_map.containsValue("pomelos")); 

     // Checking for the Value 'World' 
     System.out.println("Is the value 'World' present? " +  
     hash_map.containsValue("World")); 
     } 
 } 

出力:

Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
 Is the value 'pomelos' present? true
 Is the value 'World' present? false
// 经整数映射为字符串
// Java code to illustrate the containsValue() method 
 import java.util.*; 
 
 public class Hash_Map_Demo { 
     public static void main(String[] args) 
     { 

     // Creating an empty HashMap 
     HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); 

     // Mapping int values to string keys 
     hash_map.put("pomelos", 10); 
     hash_map.put("4", 15); 
     hash_map.put("pomelos", 20); 
     hash_map.put("Welcomes", 25); 
     hash_map.put("You", 30); 

     // Displaying the HashMap 
     System.out.println("Initial Mappings are: " + hash_map); 

     // Checking for the Value '10' 
     System.out.println("Is the value '10' present? " + 
     hash_map.containsValue(10)); 

     // Checking for the Value '30' 
     System.out.println("Is the value '30' present? " + 
     hash_map.containsValue(30)); 

     // Checking for the Value '40' 
     System.out.println("Is the value '40' present? " +  
     hash_map.containsValue(40)); 
     } 
 } 

出力:

 Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25}
 Is the value '10' present? false
 Is the value '30' present? true
 Is the value '40' present? false

4.オブジェクトのクローン():上述したハッシュマップの浅いコピーを返すために使用されています

  • 構文Hash_Map.clone()
  • パラメータ:ボイド
  • 戻り値:このメソッドは、HashMapの1つのコピーのみを返します。
  • 例としては、次のとおりです:
  // 将字符串映射为数字
  // Java code to illustrate the clone() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 

      // Creating an empty HashMap 
      HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 

      // Mapping string values to int keys 
      hash_map.put(10, "pomelos"); 
      hash_map.put(15, "4"); 
      hash_map.put(20, "pomelos"); 
      hash_map.put(25, "Welcomes"); 
      hash_map.put(30, "You"); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Displaying the cloned HashMap using clone() 
      System.out.println("The cloned map look like this: " + hash_map.clone()); 
      } 
  } 

出力:

  Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
  The cloned map look like this: {25=Welcomes, 10=pomelos, 20=pomelos, 30=You, 15=4}
  // 将整数映射为字符串
  // Java code to illustrate the clone() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 

      // Creating an empty HashMap 
      HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); 

      // Mapping int values to string keys 
      hash_map.put("pomelos", 10); 
      hash_map.put("4", 15); 
      hash_map.put("pomelos", 20); 
      hash_map.put("Welcomes", 25); 
      hash_map.put("You", 30); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Displaying the cloned HashMap using clone() 
      System.out.println("The cloned map look like this: " + hash_map.clone()); 
      } 
  } 

出力:

  Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25}
  The cloned map look like this: {pomelos=20, 4=15, You=30, Welcomes=25}

5.ブールのisEmpty():ハッシュマップビューのコレクションを返します。

  • 構文Hash_Map.isEmpty()
  • パラメータ:ボイド
  • 戻り値:マップが空であるか、または任意のマッピングが含まれていない場合、この方法は、真のブール値を返しますが、それ以外の場合はfalseです。
  • 例としては、次のとおりです:
  // 将整数映射成为字符串
  // Java code to illustrate the isEmpty() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 

      // Creating an empty HashMap 
      HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); 

      // Mapping int values to string keys 
      hash_map.put("pomelos", 10); 
      hash_map.put("4", 15); 
      hash_map.put("pomelos", 20); 
      hash_map.put("Welcomes", 25); 
      hash_map.put("You", 30); 

      // Displaying the HashMap 
      System.out.println("The Mappings are: " + hash_map); 

      // Checking for the emptiness of Map 
      System.out.println("Is the map empty? " + hash_map.isEmpty()); 
      } 
  } 

出力:

  The Mappings are: {4=15, pomelos=20, You=30, Welcomes=25}
  Is the map empty? false     
  // 对于空hashMap
  // Java code to illustrate the isEmpty() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 

      // Creating an empty HashMap 
      HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); 

      // Displaying the HashMap 
      System.out.println("The Mappings are: " + hash_map); 

      // Checking for the emptiness of Map 
      System.out.println("Is the map empty? " + hash_map.isEmpty()); 
      } 
  } 

出力:

  The Mappings are: {}
  Is the map empty? true

6.設定のentrySetは():ハッシュマップのSetビューを返します。

  • 構文hash_map.entrySet()
  • パラメータ:ボイド
  • 戻り値:このメソッドは、ハッシュマップは、要素の同じセットを持って返します。
  • 例:
  // 字符串映射成整数
  // Java code to illustrate the entrySet() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 

      // Creating an empty HashMap 
      HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 

      // Mapping string values to int keys 
      hash_map.put(10, "pomelos"); 
      hash_map.put(15, "4"); 
      hash_map.put(20, "pomelos"); 
      hash_map.put(25, "Welcomes"); 
      hash_map.put(30, "You"); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Using entrySet() to get the set view 
      System.out.println("The set is: " + hash_map.entrySet()); 
      } 
  } 

出力:

  Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
  The set is: [20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4]
// 讲整数映射成为字符串
// Java code to illustrate the entrySet() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 

      // Creating an empty HashMap 
      HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); 

      // Mapping int values to string keys 
      hash_map.put("pomelos", 10); 
      hash_map.put("4", 15); 
      hash_map.put("pomelos", 20); 
      hash_map.put("Welcomes", 25); 
      hash_map.put("You", 30); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Using entrySet() to get the set view 
      System.out.println("The set is: " + hash_map.entrySet()); 
      } 
  } 

出力:

  Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25}
  The set is: [4=15, pomelos=20, You=30, Welcomes=25]

7.オブジェクトのget(オブジェクトキー):特定のキーから取得または取得するために使用される値マップ

  • 構文hash_map.keySet()
  • パラメータ:パラメータなし
  • 戻り値:このメソッドは、ハッシュマップ結合のコレクションを返します。
  • 例としては、次のとおりです:
// 将字符串映射为整数值
// Java code to illustrate the keySet() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 
  
      // Creating an empty HashMap 
      HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 

      // Mapping string values to int keys 
      hash_map.put(10, "pomelos"); 
      hash_map.put(15, "4"); 
      hash_map.put(20, "pomelos"); 
      hash_map.put(25, "Welcomes"); 
      hash_map.put(30, "You"); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Using keySet() to get the set view of keys 
      System.out.println("The set is: " + hash_map.keySet()); 
      } 
  } 

出力:

  Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
  The set is: [20, 25, 10, 30, 15]
// 将整数映射成为字符串
// Java code to illustrate the keySet() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 

      // Creating an empty HashMap 
      HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); 

      // Mapping int values to string keys 
      hash_map.put("pomelos", 10); 
      hash_map.put("4", 15); 
      hash_map.put("pomelos", 20); 
      hash_map.put("Welcomes", 25); 
      hash_map.put("You", 30); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Using keySet() to get the set view of keys 
      System.out.println("The set is: " + hash_map.keySet()); 
      } 
  } 

出力:

  Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25}
  The set is: [4, pomelos, You, Welcomes]

8.セットketSet():キーは、ビューのセットを返すために使用され

* 语法 hash_map.keySet()
* 参数: 无参数
* 返回值:该方法返回一个具有散列映射键的集合。
* 示例如下:
// 将字符串映射为整数
// Java code to illustrate the keySet() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 

      // Creating an empty HashMap 
      HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 

      // Mapping string values to int keys 
      hash_map.put(10, "pomelos"); 
      hash_map.put(15, "4"); 
      hash_map.put(20, "pomelos"); 
      hash_map.put(25, "Welcomes"); 
      hash_map.put(30, "You"); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Using keySet() to get the set view of keys 
      System.out.println("The set is: " + hash_map.keySet()); 
  } 
} 

出力:

    Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
    The set is: [20, 25, 10, 30, 15]
//将整数映射为字符串
// Java code to illustrate the keySet() method 
import java.util.*; 
  
public class Hash_Map_Demo { 
    public static void main(String[] args) 
    { 
  
        // Creating an empty HashMap 
        HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); 
  
        // Mapping int values to string keys 
        hash_map.put("pomelos", 10); 
        hash_map.put("4", 15); 
        hash_map.put("pomelos", 20); 
        hash_map.put("Welcomes", 25); 
        hash_map.put("You", 30); 
  
        // Displaying the HashMap 
        System.out.println("Initial Mappings are: " + hash_map); 
  
        // Using keySet() to get the set view of keys 
        System.out.println("The set is: " + hash_map.keySet()); 
    } 
} 

出力:

    Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25}
    The set is: [4, pomelos, You, Welcomes]

9. int型のサイズ():マップのサイズを返すために使用されます

* 语法: Hash_Map.size()
* 参数: 无需参数
* 返回值: 该方法返回映射的大小,这也表示映射中存在的键值对的数量。
* 示例如下
//将字符串映射成为整数
// Java code to illustrate the size() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 

      // Creating an empty HashMap 
      HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 

      // Mapping string values to int keys 
      hash_map.put(10, "pomelos"); 
      hash_map.put(15, "4"); 
      hash_map.put(20, "pomelos"); 
      hash_map.put(25, "Welcomes"); 
      hash_map.put(30, "You"); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Displaying the size of the map 
      System.out.println("The size of the map is " + hash_map.size()); 
      } 
  } 

出力:

  Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
  The size of the map is 5
  // 将整数映射成为字符串
  // Java code to illustrate the size() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 

      // Creating an empty HashMap 
      HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); 

      // Mapping int values to string keys 
      hash_map.put("pomelos", 10); 
      hash_map.put("4", 15); 
      hash_map.put("pomelos", 20); 
      hash_map.put("Welcomes", 25); 
      hash_map.put("You", 30); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Displaying the size of the map 
      System.out.println("The size of the map is " + hash_map.size()); 
      } 
  } 

出力:

  Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25}
  The size of the map is 4

10.オブジェクトのPUT(オブジェクトキー、オブジェクト値):特定のキーと値のペアをマッピングするためのマップに挿入されます。

  • 语法Hash_Map.put(キー、値)
  • パラメータ:このメソッドは、2つのパラメータがあり、オブジェクト型はHashMapのです。
    • キー:これは、マッピングのための地図の中に挿入する必要がある重要な要素を指します。
    • 値:これは、上記のキーはにマッピングされる値を参照します。
  • 戻り値:あなたは、従来のキーを渡した場合、以前の値が返されます。新しいピアを渡すと、NULLが返されます。
  • 例としては、次のとおりです:
// 当传递一个存在key
// Java code to illustrate the put() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 

      // Creating an empty HashMap 
      HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 

      // Mapping string values to int keys 
      hash_map.put(10, "pomelos"); 
      hash_map.put(15, "4"); 
      hash_map.put(20, "pomelos"); 
      hash_map.put(25, "Welcomes"); 
      hash_map.put(30, "You"); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Inserting existing key along with new value 
      String returned_value = (String)hash_map.put(20, "All"); 

      // Verifying the returned value 
      System.out.println("Returned value is: " + returned_value); 

      // Displayin the new map 
      System.out.println("New map is: " + hash_map); 
      } 
  } 

出力:

  Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
  Returned value is: pomelos
  New map is: {20=All, 25=Welcomes, 10=pomelos, 30=You, 15=4}
// 当传递一个新值
// Java code to illustrate the put() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 

      // Creating an empty HashMap 
      HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 

      // Mapping string values to int keys 
      hash_map.put(10, "pomelos"); 
      hash_map.put(15, "4"); 
      hash_map.put(20, "pomelos"); 
      hash_map.put(25, "Welcomes"); 
      hash_map.put(30, "You"); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Inserting existing key along with new value 
      String returned_value = (String)hash_map.put(50, "All"); 

      // Verifying the returned value 
      System.out.println("Returned value is: " + returned_value); 

      // Displayin the new map 
      System.out.println("New map is: " + hash_map); 
      } 
  } 

出力:

  Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
  Returned value is: null
  New map is: {50=All, 20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}

11.のputAll(地図M):別のマップにマップのすべての要素をコピーするために使用されます。

  • 语法new_hash_map.putAll(exist_hash_map)
  • パラメータ:このメソッドは、パラメータexist_hash_mapを受け入れ、引数は、我々はコピーする既存のマップを参照します。
  • 戻り値:なし戻り値
  • 例外:我々はマッピングがNULLでコピーしたい場合は、このメソッドはNullPointerExceptionがスローされます。
  • 例としては、次のとおりです:
      // 将字符串映射为整数
      // Java code to illustrate the putAll() method 
      import java.util.*; 
      
      public class Hash_Map_Demo { 
      public static void main(String[] args) { 
          
      // Creating an empty HashMap 
      HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 
  
      // Mapping string values to int keys  
      hash_map.put(10, "pomelos"); 
      hash_map.put(15, "4"); 
      hash_map.put(20, "pomelos"); 
      hash_map.put(25, "Welcomes"); 
      hash_map.put(30, "You"); 
  
      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 
  
      // Creating a new hash map and copying 
      HashMap<Integer, String> new_hash_map = new HashMap<Integer, String>(); 
      new_hash_map.putAll(hash_map); 
  
      // Displaying the final HashMap 
      System.out.println("The new map looks like this: " + new_hash_map); 
      } 
  } 

出力:

  Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
  The new map looks like this: {25=Welcomes, 10=pomelos, 20=pomelos, 30=You, 15=4}
// 将整数映射成为字符串
// Java code to illustrate the putAll() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 
  
      // Creating an empty HashMap 
      HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); 

      // Mapping int values to string keys 
      hash_map.put("pomelos", 10); 
      hash_map.put("4", 15); 
      hash_map.put("pomelos", 20); 
      hash_map.put("Welcomes", 25); 
      hash_map.put("You", 30); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Creating a new hash map and copying 
      HashMap<String, Integer> new_hash_map = new HashMap<String, Integer>(); 
      new_hash_map.putAll(hash_map); 

      // Displaying the final HashMap 
      System.out.println("The new map looks like this: " + new_hash_map); 
      } 
  } 

出力:

  Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25}
  The new map looks like this: {pomelos=20, 4=15, You=30, Welcomes=25}

12.オブジェクトの削除(オブジェクトキー):これは、任意のマップを削除する特定のキーの値です。

  • 语法Hash_Map.remove(オブジェクトキー)
  • パラメータ:このメソッドは、マップのマップからそれを削除するには、パラメータのキーを使用しています。
  • 戻り値:キーが存在する場合は、この方法は、そうでない場合はNULLを返し、あらかじめ指定されたキーにマッピングされた値を返します。
  • 例としては、次のとおりです:
  // 当传递一个已存在key
  // Java code to illustrate the remove() method 
      import java.util.*; 
      
      public class Hash_Map_Demo { 
      public static void main(String[] args) { 
              
      // Creating an empty HashMap 
      HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 
  
      // Mapping string values to int keys  
      hash_map.put(10, "pomelos"); 
      hash_map.put(15, "4"); 
      hash_map.put(20, "pomelos"); 
      hash_map.put(25, "Welcomes"); 
      hash_map.put(30, "You"); 
  
      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map);  
  
      // Removing the existing key mapping 
      String returned_value = (String)hash_map.remove(20); 
  
      // Verifying the returned value 
      System.out.println("Returned value is: "+ returned_value); 
  
      // Displayin the new map 
      System.out.println("New map is: "+ hash_map); 
      } 
  } 

出力:

  Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
  Returned value is: pomelos
  New map is: {25=Welcomes, 10=pomelos, 30=You, 15=4}
// 当传递一个新key
// Java code to illustrate the remove() method 
  import java.util.*; 
      
  public class Hash_Map_Demo { 
  public static void main(String[] args) { 
          
    // Creating an empty HashMap 
    HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 
  
    // Mapping string values to int keys  
    hash_map.put(10, "pomelos"); 
    hash_map.put(15, "4"); 
    hash_map.put(20, "pomelos"); 
    hash_map.put(25, "Welcomes"); 
    hash_map.put(30, "You"); 
 
    // Displaying the HashMap 
    System.out.println("Initial Mappings are: " + hash_map);  
 
    // Removing the new key mapping 
    String returned_value = (String)hash_map.remove(50); 
 
    // Verifying the returned value 
    System.out.println("Returned value is: "+ returned_value); 
 
    // Displayin the new map 
    System.out.println("New map is: "+ hash_map); 
 } 
} 

出力:

  Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 3`0=You, 15=4}
  Returned value is: null
  New map is: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}

13.コレクション値():ハッシュマップのビュー内の値のセットを返すために使用されます。

  • 構文Hash_Map.values()
  • パラメータ:ボイド
  • 戻り値:このメソッドは、マップされたすべての値を含むビューのコレクションを返します。
  • 例としては、次のとおりです:
  // 将字符串映射为整数
  // Java code to illustrate the values() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 
  
          // Creating an empty HashMap 
          HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 
  
          // Mapping string values to int keys 
          hash_map.put(10, "pomelos"); 
          hash_map.put(15, "4"); 
          hash_map.put(20, "pomelos"); 
          hash_map.put(25, "Welcomes"); 
          hash_map.put(30, "You"); 
  
          // Displaying the HashMap 
          System.out.println("Initial Mappings are: " + hash_map); 
  
          // Using values() to get the set view of values 
          System.out.println("The collection is: " + hash_map.values()); 
      } 
  }   

出力:

  Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
  The collection is: [pomelos, Welcomes, pomelos, You, 4]
// 将整数映射成字符串
// Java code to illustrate the values() method 
import java.util.*; 

public class Hash_Map_Demo { 
  public static void main(String[] args) 
  { 

      // Creating an empty HashMap 
      HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); 

      // Mapping int values to string keys 
      hash_map.put("pomelos", 10); 
      hash_map.put("4", 15); 
      hash_map.put("pomelos", 20); 
      hash_map.put("Welcomes", 25); 
      hash_map.put("You", 30); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Using values() to get the set view of values 
      System.out.println("The collection is: " + hash_map.values()); 
  } 
} 

出力:

  Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25}
  The collection is: [15, 20, 30, 25]

参考文献:

https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html
https://www.geeksforgeeks.org/hashset-in-java/

おすすめ

転載: www.cnblogs.com/Pomelos/p/11959647.html