Import java.util.*;p Ublic class Birthdays {public static void Main (string[] args) { map<friends, string> HM = new hashmap<friends,string> (); Friends f1 = new Friends ("Charis"); Friends F2 = new Friends ("Darling"); Hm.put (F1, "Summer"); Hm.put (F2, "spring 2002"); System.out.println (Hm.get (F1)); Friends F3 = new Friends ("Darling"); System.out.println (Hm.get (F3)); } } class friends{string name; Friends (String N) {name = n;}}
Results:
Summer 2009null
2. Enum usage
public class Weathertest { static Weather W; public static void Main (string[] args) { System.out.println (w.rainy.count+ "" +w.sunny.count+ ""); } } Enum Weather{rainy, sunny;int count = 0; Weather () {System.out.print ("C"); count++;}}
Results
C C 1 1
public class Weathertest { static Weather W; public static void Main (string[] args) { System.out.println (w.rainy+ "" +w.sunny+ ""); } } Enum Weather{rainy (1), Sunny (2);p rivate int n;private Weather (int n) {this.n=n;} @Override public String toString () { return string.valueof (THIS.N);} }
Results
0 S
The traversal of the enumeration:
public class Weathertest { enum Weather{rainy (1), Sunny (2);p rivate int n;private Weather (int n) {this.n=n;} @ Override public String toString () { return string.valueof (THIS.N);} } public static void Main (string[] args) { weather[] allw = Weather.values (); for (Weather w:allw) System.out.print (w+ ""); } }
Results Ibid., 1 2
3. Numeric type problem
public class Dec26 {public static void Main (string[] args) {short a1=6; New Dec26 (). Go (A1); New Dec26 (). Go (new Integer (7)); } void go (short x) { System.out.print ("S"); } void Go (Long x) { System.out.print ("L"); } void Go (int x) { System.out.print ("i"); } void Go (number N) { System.out.print ("n"); } }
Results:
T
4. Switch, case
public class Humping {public static void Main (string[] args) { String r= "-"; Char[] c={' A ', ' B ', ' C ', ' Z '}; for (char c1:c) switch (C1) {case ' a ': r+= "a"; Case ' B ': r+= "B"; break; Default: r+= "X"; Case ' Z ': r+= "z"; } System.out.println (R); } }
Results:
-abbxzz
5. HashMap The key value is empty
HashMap: Applies to inserting, deleting, and locating elements in a map.
Treemap: For traversing keys (key) in natural order or in a custom order.
Both are non-thread safe, the former is not sorted, the latter is automatically sorted
Øhashmap the key value in the deposit is random when it is taken out, it stores the data according to the hashcode value of the key, can get its value directly according to the key, and has the fast access speed. HashMap is the best choice for inserting, deleting, and locating elements in a map.
The øtreemap takes out the sorted key-value pairs. Insertions and deletions need to maintain balance to sacrifice some efficiency. But if you want to traverse the key in natural order or in a custom order, TreeMap is better.
This test increases and finds the function, hashmap than treemap efficiency is higher.
Import java.util.*;p Ublic class Garage {public static void Main (string[] args) { map<string,string> hm= New Hashmap<string,string> (); String[] K={null, "2", "3", NULL, "5"}; String[] v={"A", "B", "C", "D", "E"}; for (int i=0;i<5;i++) { hm.put (k[i],v[i]); System.out.print (Hm.get (k[i]) + ""); } System.out.print (hm.size () + "" "+hm.values () +" \ n ");} }
Results
A b c d E 4 [C, D, B, E]
Change to TreeMap error, so the TreeMap key value does not allow null
The difference between null and "":
Import java.util.*;p Ublic class Garage {public static void Main (string[] args) { map<string,string> hm= New Hashmap<string,string> (); String[] K={null, "2", "3", "", "5"}; String[] v={"A", "B", "C", "D", "E"}; for (int i=0;i<5;i++) { hm.put (k[i],v[i]); System.out.print (Hm.get (k[i]) + ""); } System.out.print (hm.size () + "" "+hm.keyset () +" "+hm.values () +" \ n ");} }
Results
A b c D e 5 [3, NULL, 2, , 5] [C, A, B, D, E]
Java Syntax questions