Map Collections Overview:
The key is mapped to an object worthy
A key mapping not including coincident
Each key can map up to a value
Map interface and various interface Collection
Map is a double row, collection of a single
Unique, collectionde sub-system Set Map is unique
Map data structure value for a set of valid keys, nothing to do with values, data structures Collection is a set of elements for effective
By API, we know the elements most important feature of Map collection is that it can store key-value pairs
Map feature set of
Maps keys to values when mapping does not include a duplicate key, each key can only be mapped to a value at most
note:
Map data structure set of valid only for nothing to do with the value of key
Collection of the data structure is set for an element effective
Map collection features overview:
1. Add function
V put(k key,v value);
a: add functionality
b: If the key is first stored, the direct memory element, return null
c: If the store is not the first time, it replaced the previous value with the value and returns the previous value
2. Delete function
void clear (); Removes all of the elements key
V remove (Object key) to delete the key according to the key element, and returns the value
3. To determine the function
bollean containskey (Object key): Analyzing key set contains the specified time
bollean containsvalue (Object value): determining whether the collection contains a specified value
bollean isEmpoty (); determining whether the set is empty
4. acquisition function
Set<Map Entry<k,v>> entrySet();
V get (Object key); get the value according to the key
Set <E> keyset (); obtaining the set of all keys in the set
Collection <V> value (); Gets all worth collection set
The length of the function
int size (); return key set of the number of
Map collection traversal
method one:
Find value based on key
Gets a collection of all keys
Traversal key set, each key acquired
Find value based on key
Code reflect the following:
Set<String> set =hm.keySet();
for(String key:set){
String value=hm.get(key);
System.out.println(key+"---"+value);
}
Second way:
Find the key and value of the object based on a key
Gets a collection of objects of all keys
Traversal key set of objects to get a key for each object
Traversal key to find the key and value objects
Code reflect the following:
Set<Map,Entry<String,String>> set=hm.entrySet();
for(Map.Entry<String,String> me : set){
String key=me.getKey();
String value=me.getValue();
System.out.println(key+"---"+value);
}
HashMap class overview
Key is a hash table structure, you can guarantee the uniqueness of the key
hashMap Case
HashMap<String,String>
HashMap<Intrger,String>
HashMap<String,Student>
HashMap<Student,String>
LinkedHashMap class overview
Hash table implementation of the Map interface and a list of links, with predictable iteration order
By a hash table to ensure key uniqueness
(Same storage order and withdrawn) from the key table ordered guaranteed bond
TreeMap class overview
TreeMap class overview
Key is red-black tree structure, you can guarantee the sort key and uniqueness
TreeMap Case
TreeMap<String ,String>
TreeMap<String ,String>
Nested loop through the collection of
Nested HashMap
HashMap(HashMap<String,HashMap<String,Integer>>)
HashMap nested ArrayList
HashMap嵌套HashMap
Nesting nested HashMap HashMap HashMap (three nested)
Exercise Case:
"Aababcabcdabcde" Get the number of each letter appears string request Results: "a (5) b (4) c (3) d (2) e (1)"
Ideas:
1. Define a string
2. Define a Map collection
3. Place the string into an array of characters
4. traversing a character array, each character to obtain
5. Map to take this character set to go to see the return value
It is null: put the character as a key, as the value 1
Not null: put the value ++, and then re-store the keys and values
6. Define a string buffer
7. traverse TreeMap set, obtaining a key pair for each splice element
8. The string buffer output is converted to a string
Interview questions:
And the Hashtable difference 1.HashMap
HashMap: thread-safe, high efficiency, allow null keys and null values
Hashtable: thread-safe, low efficiency, does not permit null keys and null values
2.List, Set, Map of the interface is inherited Map interface
List, Set is not inherited from the Map interface, they are inherited from the Collection interface
Map is a top level interface
Collections class overview
A set of tools for operations (both static method)
Collections member method
public static <T> void sort(List<T> list) 排序
public static <T> int binarySearch(List<?> List T key) 二分查找
public static <T> T max (Collection <?> coll) maximum
public static void reverse(List<?> list) 反转
public static void shuffle (List <?> list) random permutation
Interview questions:
Collection and Collections of the difference?
Collection: the interface is the top single collection, there are sub-interfaces and Set List
Collections: is a collection of tools for the operation, there is the collection and sorting of binary method to find
Collections.sort()
If you have both natural ordering and sorting comparator to the comparator-based ordering
to sum up:
Map (double set of columns)
1.Map data structure only for a set of keys effectively, regardless of the value
2. The key is stored in the form of the elements, unique keys, values can be repeated
HashMap
The underlying data structure is a hash table, thread-safe, high efficiency
Hash table depends on two methods: hashcode and equals ()
The order of execution:
First determines hashcode () values are identical
Is: continue to see its return value equals
It is true: Description elements repeat, do not add
It is false: it is added directly to the collection
No: directly added to the collection
Final: automatically generated hashcode () and equals () to
LinkedHashMap
Underlying data structure of linked lists and hash tables
Ensure an ordered list of elements
By a hash table to ensure that the only element
Hashtable
The underlying data structure is a hash table, thread-safe, low efficiency
Hash table method is dependent on two hashcode and equals ()
TreeMap
The underlying data structure is a red-black tree (balanced binary tree is a self)
How to ensure the uniqueness of elements?
According to return value to determine whether it is 0
How to ensure ordering elements of it
Two ways
Natural ordering (comparative element includes a)
Let class implement the Comparable interface element belongs
Sorting comparator (set includes comparative)
Let receiving a set of objects of a class that implements the Comparator
Which one to use low?
Look demand
Whether in the form of key-value pairs
Is: Map
Whether the key needs sorting
Is: TreeMap
否:HashMap
I do not know to use HashMap
否:Collection
Its common way to traverse the collection:
Collection:
add()
remove()
contains()
iterator()
size()
:( traversal, two)
Enhancements for
Iterator
List: get () 3 Zhong traversal: ordinary for enhanced for iterator
Set: two kinds of traversal: Enhanced for iterator
Map:
put()
remove()
contaibkey(),containvalue()
keySet()
get()
value()
entrySet()
size()
Traversal:
Find value based on key
The key to find the object key and value, respectively,