面試題:Java中Collection與Collections的區別

來源:互聯網
上載者:User
1、java.util.Collection 是一個 集合介面。它提供了對集合對象進行基本操作的通用介面方法。Collection介面在Java 類庫中有很多具體的實現。Collection介面的意義是為各種具體的集合提供了最大化的統一操作方式。 Collection  
├List  
│├LinkedList  
│├ArrayList  
│└Vector  
│ └Stack  
└Set 2、java.util.Collections 是一個封裝類。它包含有各種有關集合操作的 靜態多態方法,用於實現對各種集合的搜尋、排序、安全執行緒化等操作。此類 不能執行個體化,就像一 個工具類,服務於Java的Collection架構。

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestCollections {
   
    public static void main(String args[]) {
        //注意List是實現Collection介面的
        List list = new ArrayList();
        double array[] = { 112, 111, 23, 456, 231 };
        for (int i = 0; i < array.length; i++) {
            list.add(new Double(array[i]));
        }
        Collections.sort(list);
        for (int i = 0; i < array.length; i++) {
            System.out.println(list.get(i));
        }
        // 結果:23.0 111.0 112.0 231.0 456.0
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.