hashMap


1.HashMap紹介:
Costruucts an empty HashMap with the default initial capacity(16)and the default load factor(0.75)を作成します。
容量は、ハッシュ・テーブルの中のバケットの数であり、初期容量は、ハッシュ・テーブルの作成時の容量にすぎない。ロードファクタは、ハッシュ・テーブルであり、容量が自動的に増加する前に、マルチボリュームに達することができるスケールである。
HashMapの後継関係
package java.util;
import java.io.*;
public class HashMap<K,V>
    extends AbstractMap<K,V>
    implements Map<K,V>, Cloneable, Serializable{
}
 HashMapはAbstractMapに継承され、Map、Cloeable、java.io.Serializableインターフェースを実現しました。 
  • 同期を実現しません。スレッドが不安定です。
    2.HashMapの使い方:
  • HashMap構造関数:
    //       。
    HashMap()
    //   “    ”     
    HashMap(int capacity)
    //   “    ” “    ”     
    HashMap(int capacity, float loadFactor)
    //   “ Map”     
    HashMap(Map<? extends K, ? extends V> map) 
  • HashMapインターフェース:
    void                 clear()
    Object               clone()
    boolean              containsKey(Object key)
    boolean              containsValue(Object value)
    Set<Entry<K, V>>     entrySet()
    V                    get(Object key)
    boolean              isEmpty()
    Set<K>               keySet()
    V                    put(K key, V value)
    void                 putAll(Map<? extends K, ? extends V> map)
    V                    remove(Object key)
    int                  size()
    Collection<V>        values() 
  • 3.LeetCode関連テーマ:
    two-sum
    Gven an array of integers、find two numbers such that they add up to a specific target number.
    The function twoSum shound return indices of the two numbers such that the y add up to the target,where index 1 must be less than index 2.Please note that your returned answerss(both index 1 and index 2.over 2)
    You may asome that each input would have exactly one solution.
    Input: numbers={2,7,11,15}target=9 Output: index 1=1,index 2=2
     
    4.参考コード:
    public class TwoSum {
    	public int[] twoSum(int[] numbers, int target) {
    	Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    	for (int i = 0; i < numbers.length; map.put(numbers[i], ++i)) 
    		if (map.containsKey(target - numbers[i])) 
    			return new int[]{map.get(target - numbers[i]),i+1};
    		return new int[]{0,0};
    	}
    }