hashcode and equals must be rewritten.
"Coercion"Do not remove/add the element in the Foreach loop. remove element Use iterator method, if concurrent operation, need to lock iterator object. Counter example: list
"Spec"When the collection is initialized, try to specify the collection initial value size. Description: ArrayList try to initialize using ArrayList (int initialcapacity).
"Spec"Use EntrySet to traverse the Map class collection KV inste
.
Call the keyset method to put back a set interface type
set
Mode two: Implementing the values method using the Map interface (for enhancements)
Similarly: The values method that invokes the map interface he put back a collection attempt, collection interface He also implemented the Iterable interface, so can iterate.
collection
Mode III: Implementation using the EntrySet method of the map interface (for enhancements)
MAP MT = new HashMap ();
Mode one
Set keys= mt.keyset ();
For (iterator It=keys.iterator (); It.hasnext ();) {
Object key=it.next ();
Object Value=mt.get (key);
}
Mode two
set
Simple point of said: Keyset () traversal is the keys into the set set, and then traverse the keys in the set, and then use the traversal of the key to obtain the corresponding value. Value= get (key);and EntrySet () is to save all the key value pa
res = "map parameter :";
Set entrySet = map. entrySet ();
For (Iterator it = entrySet. iterator (); it. hasNext ();){
Object next = it. next ();
Res + = next + ",";
}
Return res;
}
Public String getMessage (Test2 t2 ){
String res = "Test2 (bean) parameter :";
Res + = t2.p1 + ",";
Res + = t2.p2 + ",";
Return res;
}
Public String getMessage (Test3 t2 ){
String r
//
10. [recommended] specify the initial value of the set when the set is initialized.
Note: hashmap uses hashmap (INT initialcapacity) for initialization.
Positive example: initialcapacity = (number of elements to be stored/load factor) + 1. Note that the default load factor (loaderfactor) is 0.75. If you cannot determine the initial value, set it to 16 (that is, the default value ).
Inverse example: 1024 elements need to be placed in hashmap. Because the initial capacity size is not set, as
The HashMap store is a key-value pair, so its traversal with the list and set should be different in general.But Java cleverly handles HashMap's key-value pairs as a monolithic object (Java.util.Map.Entry), which optimizes the traversal of the hashmap, making it no different from the list and set.The first type:Java code
Map map = new HashMap ();
Iterator iter = Map.entryset (). Iterator ();
while (Iter.hasnext ()) {
Java.util.Map.Entry Entry = (map.entry) iter.next ();
ContainsKey (Key): Contains keyBoolean Containsvalue (value): Whether the value is included4, remove.int size (): Return lengthValue get (key): Gets the corresponding value by specifying the key. If NULL is returned, you can tell that the key does not exist. Of course there is a special case, that is, in the HashMap collection, the null key can be stored in the null value.Collection values (): Gets all the values in the Map collection.5, want to get all the elements in the map:Principle: There
"; // obtain the result set List
Students = qr. query (SQL, new BeanListHandler
(Student. class); System. out. println (students);} // a single query @ Testpublic void selectOne () throws SQLException {QueryRunner qr = new QueryRunner (C3P0Utils. getDataSource (); String SQL = "select * from student where id =? "; // Get a single result Student student = qr. query (SQL, new BeanHandler
(Student. class), 1); System. out. println (student );}
4. query data and put it i
The map is an interface in Java, and Map.entry is an internal interface to the map.Map provides a number of common methods, such as keyset (), EntrySet (), and so on.The KeySet () method return value is a collection of key values in map; The return value of EntrySet () also returns a set collection of type Map.entry. Map.entry is an internal interface to the map declaration, which is generic and defined as
encourage recursion. Because recursion usually means that it can be decomposed into tail-recursing optimized by individual users ). If the programming language you are using can support it, that would be even better. Even so, you should also note that the slight adjustment to the algorithm will make the tail recursion become normal recursion.
We hope the compiler can automatically detect this point, otherwise we will waste a lot of stack frameworks (frames) in order to use just a few local vari
the student to be added:"); 13 String name = in. next (); // accept the input name14 this. students. put (Id, new Student (Id, name); 15} else {16 System. out. println ("this Id has been occupied Yes! "); 17} 18 19}
Write another test function that prints the output, such:
1/* 2 * print Student Category 3*4 */5 public void PrintStu () {6 System. out. println ("Total" + this. students. size () + "Student:"); 7 // traverse keySet 8 for (String s: this. students. keySet () {9 Student st = students
the JDK follow it.Map's APIabstract void Clear () Abstract Boolean ContainsKey (Object key) Abstract Boolean Containsvalue (object Value) abstract setDescription :The map provides an interface for returning a keyset, a value set, or a key-value mapping relationship set, respectively.EntrySet () The set collection for the return key-value setKeySet () to return the set collection of keysetVALUES () collection collection of user return value setsBecause t
correspondencebetween the stored location of the record and its key words , so that each keyword key corresponds to a storage location F (key).The corresponding relationship f is called the hash function, also known as the hash function.Hashing is used to store records in a contiguous storage space, which is known as a hash table or hash table.
/** * An object that maps keys to values. A map cannot contain duplicate keys; * each key can map to at most one value. */
An object that
order is determined by comparable or comparator). TreeMap is characterized by the fact that the resulting result is sorted, and TreeMap is the only map with Submap method, which can return a subtree.
Weekhashmap
Weak key (week key) mapping, which allows you to release the object that the map points to, which is designed to solve some particular class of problems. If there is no reference outside the map to a key, the key can be reclaimed by the garbage collector.
method is out of order. The typically uses the following: map map = new HashMap (); Map.put ("Key1", "LISI1"); Map.put ("Key2", "Lisi2"); Map.put ("Key3", "Lisi3"); Map.put ("Key4", "Lisi4"); //Gets the set collection of all keys for the map collection first, keyset () Iterator it = Map.keyset (). Iterator ( ); //Get iterator while (It.hasnext ()) { Object key = It.next (); System.out.println (Map.get (key)); } Second type: EntrySet ()SetT
map cannot contain duplicate keys, each key can be mapped to at most one value. Therefore, the key-value set, the keyset is set, and the value set is collection.Map provides methods such as key-value pair, get value by key, delete key, get capacity size, and so on.2 Map.entryMap.entry is defined as follows:Interface entryMap.entry is an internal interface in the map, Map.entry is a key-value pair , and Map obtains the Map.entry key-value pair collection through the
the set returned by this method is that the data is emitted in a disorderly order.Typical usage is as follows:Map map = new HashMap ();Map.put ("Key1", "LISI1");Map.put ("Key2", "Lisi2");Map.put ("Key3", "Lisi3");Map.put ("Key4", "Lisi4");Gets the set set of all keys for the map collection first, keyset ()Iterator it = Map.keyset (). Iterator ();Get iteratorswhile (It.hasnext ()) {Object key = It.next ();System.out.println (Map.get (key));}Second type: Entr
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.