Map的排序和List的几种遍历方式

package test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author [email protected]
 * Map<K, V>的排序
 * List的几种遍历方式
 *
 */
public class Test4 {

	/**
	 * 对于Map<K, V>的排序
	 */
	static void testMap() {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("key1", 5);
		map.put("key2", 2);
		map.put("key3", 6);
		map.put("key4", 9);
		map.put("key5", 8);
		map.put("key6", 12);
		List<Map.Entry<String, Integer>> list = 
			new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
		Comparator<Map.Entry<String, Integer>> comparator = 
			new Comparator<Map.Entry<String, Integer>>() {
			public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
				return o1.getValue()-o2.getValue();
			}
		};
		Collections.sort(list, comparator);
		for(Map.Entry<String, Integer> entry : list) {
			System.out.println(entry.getKey() + ":" + entry.getValue());
		}
	}
	
	/**
	 * 关于List的几种遍历方式
	 */
	static void testList() {
		List<Long> list = new ArrayList<Long>();
        for(Long i=0l;i<3100000l;i++){ 
            list.add(i); 
        }
        
        Long oneOk = oneMethod(list); 
        Long twoOk = twoMethod(list); 
        Long threeOk = threeMethod(list); 
        Long fourOk = fourMethod(list);
        
        System.out.println("One:" + oneOk); 
        System.out.println("Two:" + twoOk); 
        System.out.println("Three:" + threeOk); 
        System.out.println("four:" + fourOk);
	}

	public static Long oneMethod(List<Long> lists){
        Long timeStart = System.currentTimeMillis(); 
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<lists.size();i++) { 
            sb.append(lists.get(i));
        } 
        Long timeStop = System.currentTimeMillis();
        sb = null;
        return timeStop -timeStart ; 
    } 
    
    public static Long twoMethod(List<Long> lists){ 
        Long timeStart = System.currentTimeMillis(); 
        StringBuilder sb = new StringBuilder();
        for(Long string : lists) {
        	sb.append(string);
        } 
        Long timeStop = System.currentTimeMillis(); 
        sb = null;
        return timeStop -timeStart ; 
    } 
    
    public static Long threeMethod(List<Long> lists){      
        Long timeStart = System.currentTimeMillis(); 
        StringBuilder sb = new StringBuilder();
        Iterator<Long> it = lists.iterator(); 
        while (it.hasNext()) { 
            sb.append(it.next()); 
        } 
        Long timeStop = System.currentTimeMillis(); 
        sb = null;
        return timeStop -timeStart ; 
    }    
    
    public static Long fourMethod(List<Long> lists){     
        Long timeStart = System.currentTimeMillis(); 
        StringBuilder sb = new StringBuilder();
        for(Iterator<Long> i = lists.iterator(); i.hasNext();)    { 
            sb.append(i.next()); 
        } 
        Long timeStop = System.currentTimeMillis(); 
        sb = null;
        return timeStop -timeStart ; 
    }
    
    public static void main(String[] args) {
//    	testMap();
	    testList();
	}
	   
}

 

你可能感兴趣的:(Map排序,List遍历)