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)
Common methods in 2.MAP interfaces
Object put (object key, object value); Stores the specified key-value pairs in the map
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
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
List synchronizedlist (list list);//Returns the list of synchronizations supported by the specified list
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 persistent set of attributes. 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.
- Storing element methods such as put and Putall are not recommended, and the SetProperty (Stringkey, StringValue) method should be used.
- Because the stored "key-value" pairs are all strings. Similar values should also use GetProperty (Stringkey)
- Generic operations are not supported
//Propertiesexample, the root directory where the config.properties file is to exist
Importjava.io.IOException;ImportJava.io.InputStream;Importjava.util.Properties; Public classPropertiestest { Public Static voidMain (string[] args) {//method ChainInputStream is =Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream ("Config.properties"); Properties prop=NewProperties ();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
For the map interface, it cannot be output directly using iterations , because each location in the map holds a pair of values (Key?value),
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 through the EntrySet () method
- Instantiating a iterator through a set interface instance
- With iterator iteration output, each content is a Map.entry object
- Separation of Key?value by Map.entry. Getkey,getvalue
Maps are mostly used for lookups , and the output is a small number of operations.
Summarize:
Selection criteria:
? Storage requirements
? unordered-set
? ordered-list
?“ Key-value "to-map
The efficiency of reading and changing
? Hash*-both are the highest
? array*-Read fast Change slow
? linked*-Read slow change fast
java-Collection Map