1. Overview of the TreeMap class:
The key is a red-black tree structure that guarantees the ordering and uniqueness of the keys .
2. TreeMap Case:
Treemap<string, string>
code example:
1 Packagecn.itcast_04;2 3 ImportJava.util.Set;4 ImportJava.util.TreeMap;5 6 /*7 * TreeMap: is based on the red-black tree map interface implementation. 8 * 9 * hashmap<string,string>Ten * Key: String One * Value: String A */ - Public classTreemapdemo { - Public Static voidMain (string[] args) { the //To create a collection object -treemap<string, String> TM =NewTreemap<string, string>(); - - //creating elements and adding elements +Tm.put ("Hello", "Hello"); -Tm.put ("World", "worlds"); +Tm.put ("Java", "Javanese"); ATm.put ("World", "2"); atTm.put ("Java ee", "java ee"); - - //iterating through the collection -Set<string> set =Tm.keyset (); - for(String key:set) { -String value =Tm.get (key); inSYSTEM.OUT.PRINTLN (key + "---" +value); - } to } +}
Run the effect as follows:
Java Basic Knowledge Enhancement Collection Framework note 59:map collection of TreeMap (treemap<string,string>) case