"1. "Hashmap,linkedhashmap,treemap Contrast
Common:
Hashmap,linkedhashmap,treemap are map;map primarily used to store key (key) value pairs, and the keys do not allow keys to repeat, but allow values to be duplicated.
Different points:
The key values stored in the 1.HashMap are random at the time of extraction and are our most commonly used map. It stores the data according to the hashcode value of the key, which can be accessed directly by the key, with fast access speed. HashMap is the best choice for inserting, deleting, and locating elements in a map.
2.TreeMap takes the sorted key-value pairs. But if you want to traverse the key in natural order or in a custom order, TreeMap is better.
3. Linkedhashmap is a subclass of HashMap, and if the order of output needs to be the same as the input, then the LINKEDHASHMAP can be implemented. (Application scenario: Shopping cart etc need order)
code example:
[Java]View PlainCopy
- Package com.alibaba.sample.petstore.web.store.module.screen;
- Import Java.util.HashMap;
- Import Java.util.Iterator;
- Import Java.util.LinkedHashMap;
- Import Java.util.Map;
- Import Java.util.Map.Entry;
- Import Java.util.TreeMap;
- Import Javax.servlet.http.HttpServletResponse;
- Import org.springframework.beans.factory.annotation.Autowired;
- Public class Viewcart {
- @Autowired
- private HttpServletResponse response;
- public Void execute () throws Exception {
- This.usehashmap ();
- This.usetreemap ();
- This.uselikedhashmap ();
- }
- public void Usehashmap () throws Exception {
- Response.getwriter (). println ("------unordered (random output)------");
- map<string, string> map = new hashmap<string, string> ();
- Map.put ("1", "Level 1");
- Map.put ("2", "Level 2");
- Map.put ("3", "Level 3");
- Map.put ("A", "Level A");
- Map.put ("B", "Level B");
- Map.put ("C", "Level C");
- Iterator<entry<string, string>> it = Map.entryset (). Iterator ();
- While (It.hasnext ()) {
- entry<string, string> e = It.next ();
- Response.getwriter (). println ("Key:" + e.getkey () + "; Value: "+ e.getvalue ());
- }
- }
- //Ordered (default sort, cannot be specified)
- public void Usetreemap () throws Exception {
- Response.getwriter (). println ("------ordered (but by default shun charge, cannot be specified)------");
- map<string, string> map = new treemap<string, string> ();
- Map.put ("1", "Level 1");
- Map.put ("2", "Level 2");
- Map.put ("3", "Level 3");
- Map.put ("A", "Level A");
- Map.put ("B", "Level B");
- Map.put ("C", "Level C");
- Iterator<entry<string, string>> it = Map.entryset (). Iterator ();
- While (It.hasnext ()) {
- entry<string, string> e = It.next ();
- Response.getwriter (). println ("Key:" + e.getkey () + "; Value: "+ e.getvalue ());
- }
- }
- public void Uselikedhashmap () throws Exception {
- Response.getwriter (). println ("------ordered (output according to input order)------");
- map<string, string> map = new linkedhashmap<string, string> ();
- Map.put ("1", "Level 1");
- Map.put ("2", "Level 2");
- Map.put ("3", "Level 3");
- Map.put ("A", "Level A");
- Map.put ("B", "Level B");
- Map.put ("C", "Level C");
- Iterator<entry<string, string>> it = Map.entryset (). Iterator ();
- While (It.hasnext ()) {
- entry<string, string> e = It.next ();
- Response.getwriter (). println ("Key:" + e.getkey () + "; Value: "+ e.getvalue ());
- }
- }
- }
return Result:
[HTML]View PlainCopy
- ------unordered (random output)------
- Key:3; Value:level 3
- Key:2; Value:level 2
- Key:1; Value:level 1
- Key:b; Value:level b
- KEY:C; Value:level C
- key:a; Value:level A
- ------ordered (but by default shun charge, cannot be specified)------
- Key:1; Value:level 1
- Key:2; Value:level 2
- Key:3; Value:level 3
- key:a; Value:level A
- Key:b; Value:level b
- KEY:C; Value:level C
- ------ordered (output according to the input order)------
- Key:1; Value:level 1
- Key:2; Value:level 2
- Key:3; Value:level 3
- key:a; Value:level A
- Key:b; Value:level b
- KEY:C; Value:level C
4. Summary: The PHP array is implemented with HashMap, so learning PHP for HashMap is easy to understand, linkedhashmap with the PHP array implementation of the function is very similar.
"2. 】
"Java" "collection"