use HashMap TreeMap LikedHashMap

//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 static void main(String[] args)  
    {
        try{
            new ViewCart().execute();
        }
        catch(Exception ex)
        {
           System.out.println(ex.toString());  
        }
    }


    public void execute() throws Exception {  
        this.useHashMap();      
        this.useTreeMap();  
        this.useLikedHashMap();   
    }  
      
    public void useHashMap() throws Exception {      
        System.out.println("------无序(随机输出)------");  
        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();       
            System.out.println("Key: " + e.getKey() + ";   Value: "       + e.getValue());      
        }  
    }  
      
    // 有序(默认排序,不能指定)   
    public void useTreeMap() throws Exception {      
        System.out.println("------有序(但是按默认顺充,不能指定)------");      
        Map<String, String> map = new TreeMap<String, String>();   
        map.put("3", "Level 3");
        map.put("c", "Level c");
        map.put("1", "Level 1");      
      
        map.put("a", "Level a");      
        map.put("b", "Level b");  
        map.put("2", "Level 2");      
      
        Iterator<Entry<String, String>> it = map.entrySet().iterator();      
        while (it.hasNext()) {       
            Entry<String, String> e = it.next();       
            System.out.println("Key: " + e.getKey() + ";   Value: "       + e.getValue());      
        }  
    }  
      
    public void useLikedHashMap() throws Exception {      
        System.out.println("------有序(根据输入的顺序输出)------");      
        Map<String, String> map = new LinkedHashMap<String, String>();    
        map.put("3", "Level 3");      
        map.put("a", "Level a"); 
        map.put("2", "Level 2");
        map.put("b", "Level b");        
        map.put("1", "Level 1");
        map.put("c", "Level c");  
        Iterator<Entry<String, String>> it = map.entrySet().iterator();      
        while (it.hasNext()) {       
            Entry<String, String> e = it.next();       
            System.out.println("Key: " + e.getKey() + ";   Value: "       + e.getValue());      
        }  
    }  
} 

 

 

---------- java ----------
------无序(随机输出)------
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
------有序(但是按默认顺充,不能指定)------
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
------有序(根据输入的顺序输出)------
Key: 3;   Value: Level 3
Key: a;   Value: Level a
Key: 2;   Value: Level 2
Key: b;   Value: Level b
Key: 1;   Value: Level 1
Key: c;   Value: Level c

Output completed (0 sec consumed) - Normal Termination

 

posted @ 2017-05-08 20:05  sky20080101  阅读(100)  评论(0)    收藏  举报