1.What is map
As the difine of map in Collins:"Map is a drawing of a particular area,showing its main features as they would appear if you looked at them from above".In programming language,the mean of map is similar with the defination in the dictionary,that is a value can be find from a map(Key).Return the the strict defination of map in Computer Science,map is a type of associative container,which supports high efficience look up,and the elements in a map are key-value pairs:The key serves as an index into the map,and the value represents the data associated with that index.In python,there also is a datastructure similar with "map",however,it's called dictionary,and both Java and C++ this datastructur is called map.The key points of map is as follows:
point 1:The elements in map is key-value pair,key is the index of each element in the map,and value is the contains of this element.
point 2:The key-value pair has a one-to-one relationship,one key corresponds to identify value,there is on other situations.in another words,one key can not correspond to multi values,on the contrary,several different values can not correspond to one key.
point 3:Map is one of the collections member,it has a high searching effience,because,once you make sure the key or the value of an element in the map,you can easily find a correspond value or key.And the practice implements of map would be introduced in the later part of this blog.
2.HashMap
Before I talk about Hashmap's internal implements,I suppose to show the inheritance structure of this Container.
Map-->AbstractMap-->HashMap
As you can see from the inheritance relationship of HashMap,It extends from AbstractMap,which implements the base interface of Map.Now,I suppose to learn more about this Container from the source code of hashmap,and figure out the implement method of this container.
//Interface Map
public interface Map<K,V>{
//Returns the number of key-value pair in Map.
int size();
//Returns true if the map is empty,otherwise return false.
boolean isEmpty();
//Finds out whether the map contains a specifid key or not.The reason that the
//arguments arguments is a Object class is that Object is the base class of all
//class in java.
boolean containKey(Object key);
//Finds out whether this map contains a specified value or not,return a boolean
boolean containValue(Object value);
//Gets the key corresponding value and return it,it's type is the template V.
V get(Object key);
//Deletes the key corresponding value and return it,it's type is the template V.
V remove(Object key);
//copies all of the elements in the specified map to this map.and the input args
//is a Map<? extends K,? extends V>,"?" is extends from K and V,so any subclass of
//K,V are legal.
void putAll(Map<? extends K,? extend V> m)
//Deletes all elements in this map and returns anything.
void clear();
//Returns a key set of this map.
Set<K> keySet();
//Returns a values collection of this map.
Collection<V> values();
}
3.The Difference Between HashMap and HashTable