Map<K, V>的排序和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 qxch0372@163.com
 * 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();
	}
	   
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值