Collection接口和Collections類的簡單區別和講解


  這里僅僅進行一些簡單的比較,如果你想要更加詳細的信息話,請自己百度。

  1.Collection:

  是集合類的上層接口。本身是一個Interface,里面包含了一些集合的基本操作。

  Collection接口時Set接口和List接口的父接口

  里面的常用操作有如下內容:

 

   2.Collections 

      Collections是一個集合框架的幫助類,里面包含一些對集合的排序,搜索以及序列化的操作。

      最根本的是Collections是一個類哦。

      下面是Collections類中的常用操作:

      

     

      為了更好的理解collections類的作用,下面貼一段對Map中的元素進行自定義排序的代碼:

     

package com.yonyou.test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;





/**
 * 測試類
 * @author 小浩
 * @創建日期 2015-4-18
 */
public class Test{
	public static void main(String[] args) throws IOException{
		new Test().test();
   }


/**
 * 對Map元素進行排序操作
 */
private void test() {
 Map<String,Integer> map=new HashMap<String,Integer>();
 map.put("張三",7);
 map.put("李四",1);
 map.put("王五",9);
 map.put("趙六",8);
 
 ArrayList<Entry<String,Integer>> list=new ArrayList<Entry<String,Integer>>(map.entrySet());
 Collections.sort(list, new Comparator<Entry<String,Integer>>() {

	@Override
	public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
		return o1.getValue()-o2.getValue();
	}
});
 
 //map按照指定格式排序后的結果
 for(Entry<String,Integer> entry:list)
 {
	 System.out.println(entry.getKey()+"=》"+entry.getValue());
 }
 
}
}

  

      好吧,就先到這里吧~

     

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM