1.Map interface
The collection class that implements the map interface stores objects by means of a "key-value" mapping.
The key-value mapping pair is uniquely identified by a key, and the " key " at the bottom of the map is stored with set (which guarantees its uniqueness and does not duplicate).
The object that maps the key to a value. A map cannot contain duplicate keys, and each key can be mapped to at most one value.
The implementation class for the map interface in the JDK API is commonly used: 1,hashmap 2, TreeMap 3,hashtable (not commonly used)
Note: both the key and the value can be null, but the key cannot be duplicated and the value can be repeated
Common methods in 2.MAP interfaces
Object put (object key, object value); Stores the specified key-value pairs in the map
Note: Due to the uniqueness of key, if you put the key value pair of the same key again, then the key value pair will overwrite the original
Object get (object key); Take out the mapped value by key (key)
Object remove (object key); Removes its key-value pair from the map, based on the specified key.
Boolean ContainsKey (Object key); Determines whether the map contains a key-value pair for the specified key
Boolean Containsvalue (Object value);//Determines whether the map contains a "key-value" pair for the specified value
Boolean isEmpty (); Determine if a map contains elements
int size (); Get length
void Clear (); Clears all "key-value" pairs in the map
Set KeySet ();//Returns the set set of keys contained in the map
Collection values (); Returns a collection of all values in the map.
3.HashMap
HashMap the " key " is hashed with set (HashSet), so it is very efficient to take "value" according to "key".
4.TreeMap
TreeMap internal to the " key " with treeset red and black tree structure to store, so put TreeMap "Key-value" pair of key
Must be "sorted" .
5.map.entry interface
Map.entry is an interface defined within the map that is designed to store Key-value content.
6.Collections Tool Class ( different from collection<e> )
The Java.util.Collections class is a tool class for manipulating collections and provides some static methods for implementing some common algorithms based on collections.
void sort (list list); All elements of the//list list must implement the comparable interface , sorted in ascending order according to the natural ordering of the elements
void Shuffle (list list);//The Elements in the list list () are randomly arranged.
void reverse (list list);//invert the elements in the list list.
void copy (list dest, list src);//src copied to Dest
void swap (list<?> list,int i,int j);//Exchange list the element at the specified position
int BinarySearch (List <? extends t> list,t key); Find the position of an element in the list, provided it is sorted
Element Max (collection< extends?> coll)//returns the largest element in a given Collection based on the natural order of the elements
List synchronizedlist (list list);//Returns the list of synchronizations supported by the specified list
Note: Collections: A tool class that operates on a collection, which is a static method.
Arrays: A tool class that operates on an array of static methods.
7.HashTable
The old version of the Hashtable, the operation is mostly the same as HashMap. But it guarantees the synchronization of threads .
It has a subclass properties (property set) that are more commonly used:
-
The properties class represents a persisted property set. Properties can be saved in a stream or loaded from a stream. Each key and its corresponding value in a property set is a string.
-
Does not recommend using the put and Putall methods of storing elements, should use the SetProperty (Stringkey, StringValue) method,
-
Because the stored . Similar values should also use GetProperty (stringkey)
-
Does not support generics operations
Properties example, the root directory to exist config.properties file import Java.io.ioexception;import Java.io.inputstream;import Java.util.properties;public class Propertiestest {public static void main (string[] args) {//method chain InputStream is = Thread.cu Rrentthread (). Getcontextclassloader (). getResourceAsStream ("config.properties"); Properties prop = new properties (); try {prop.load (is);} catch (IOException e) {e.printstacktrace ();} String name = Prop.getproperty ("name"); String pwd = Prop.getproperty ("pwd"); SYSTEM.OUT.PRINTLN (name + "," + pwd);}}
8.Map interface Output
1,set<k> KeySet: Deposits all the keys in the map into the set collection. Because set has iterators.
All the keys that can be iterated out are taken in accordance with the Get method. Gets the value corresponding to each key.
The extraction principle of the Map collection: The Map collection is turned into a set set. is removed by an iterator.
2,set<map.entry<k,v>> EntrySet: The mapping relationships in the map collection are stored in the set set,
And the data type of the relationship is: map.entry
Entry is actually a static internal interface in the map.
Why should it be defined in the interior?
Because only with the map collection, there is a key-value pair, there will be a key value of the mapping relationship.
A relationship belongs to an internal thing in the map collection.
And the object accesses the elements in the map collection directly.
For the map interface, it cannot be output directly using iterations, because each location in the map holds a pair of values (KeyValue),
In iterator, only one value can be found at a time. So if you want to use an iteration for output, follow these steps:
-
Changes an instance of map to a set interface object via the EntrySet () method
-
Instantiate the
-
Through the set interface instance for iterator through the iterator iteration output, where each content is a Map.entry object
-
is keyvalue separated by Map.entry. Getkey,getvalue
Map<integer,string> map = new hashmap<integer,string> (); //add Element map.put (1, "BBB"); map.put (2, "AAA"); map.put (2, "Eee");//If the key is the same, subsequent elements overwrite the previous element. // Both the key and the value can be null, but the key cannot be duplicated. The value can be repeated. //Traversal map &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//1: First get the key set of map, then get the value according to the key Set< Integer> set = map.keyset (); //with enhanced for loop for (Integer i:set) { system.out.println (i+ ":" +map.get (i)); } //traverse &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//2 with iterator: Gets the value collection for the map. But this cannot get the key. &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//3: Get map   with key value relationship; Key-value relationships are stored in the set collection. //map.entry is a mapping relationship that describes the map collection . Set<Map.Entry<Integer, String>> set2 = Map.entryset (); for (map.entry<integer, string> M:set2) { system.out.println ("key:" + M.getkey () + "--value:" +m.getvalue ()); }
Maps are mostly used for lookups , and the output is a small number of operations.
Summarize:
Map
|--hashtable: The underlying is a hash table data structure that cannot be stored as a null key null value. The collection is thread-synchronized. jdk1.0. Low efficiency.
|--hashmap: The underlying is a hash table data structure that allows NULL values and NULL keys, which are not synchronized. Replace Hashtable, jdk1.2. High efficiency.
|--treemap: The bottom layer is a two-fork tree data structure. Thread is out of sync. Can be used to sort keys in the map collection
Selection criteria:
Storage requirements
Unordered-set
Ordered-list
"Key-value" to-map
Efficiency of reading and changing
Hash*-both are the highest
array*-Read fast Change slow
linked*-Read slow change fast
java-set under "Five"