package JavaSenior;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
/**
* HashMap比SortedMap快,非排序的时候用HashMap
* @author Administrator
*/
public class Java_Sortedmap {
public static void main(String[] args) {
HashMap<String , String> map = new HashMap<String ,String>();
map.put("10", "hello10");
map.put("20", "hello20");
map.put("30", "hello30");
//遍历map
for(Entry<String,String> entry: map.entrySet()){
System.out.println("排序之前:"+entry.getKey()+"-->"+entry.getValue());
}
System.out.println("======================================================");
SortedMap<String, String> sortMap = new TreeMap<String,String>(map);
Set<Entry<String,String>> entry1 =sortMap.entrySet();
Iterator<Entry<String,String>> it = entry1.iterator();
while(it.hasNext()){
Entry<String, String> entry3 = it.next();
System.out.println("排序之后:"+entry3.getKey()+"-->"+entry3.getValue());
}
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
/**
* HashMap比SortedMap快,非排序的时候用HashMap
* @author Administrator
*/
public class Java_Sortedmap {
public static void main(String[] args) {
HashMap<String , String> map = new HashMap<String ,String>();
map.put("10", "hello10");
map.put("20", "hello20");
map.put("30", "hello30");
//遍历map
for(Entry<String,String> entry: map.entrySet()){
System.out.println("排序之前:"+entry.getKey()+"-->"+entry.getValue());
}
System.out.println("======================================================");
SortedMap<String, String> sortMap = new TreeMap<String,String>(map);
Set<Entry<String,String>> entry1 =sortMap.entrySet();
Iterator<Entry<String,String>> it = entry1.iterator();
while(it.hasNext()){
Entry<String, String> entry3 = it.next();
System.out.println("排序之后:"+entry3.getKey()+"-->"+entry3.getValue());
}
}
}