Arrays類和Collections的運用


為了方便的對Array對象、Collection對象進行操作,Java中提供了Arrays類和Collections類對其進行操作

其中Arrays和Collections中所有的方法都為靜態的,以方便直接傳入對象引用,執行相應的功能。

十分常用的方法有:

Arrays.asList(數組對象)  //此靜態方法用於將Array轉化為List類型對象。常常用於List類型對象的初始化中。

Collection.addAll(Collection對象,數組對象或可變參數列表 ) //次靜態方法用於向一個已經存在的Collection中拼接數據

Example:

Collection<String>  appleStore= new ArrayList<String>(Arrays.asList("Sweet,Sour".split(",")));

為了對ArrayList<String>進行初始話,Oracle公司提供了三種重載的構造器:

1、ArrayList() 
          Constructs an empty list with an initial capacity of ten.

2、ArrayList(Collection c) 
          Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

3、ArrayList(int initialCapacity) 
          Constructs an empty list with the specified initial capacity.

此處使用了第二個構造器,即傳入一個List對象。 

此處"Sweet,Sour".split(",")首先將字符串對象轉化為了一個String數組,包含兩個對象。

而后調用Array.asList(數組對象),將其轉換為一個List對象。

!值得注意的是collection對象.addAll(list對象),注意此處是collection對象而非Collections類。

 

Example2:

Collection<String> appleStore=new Collection<String>();

String[] strArray="Sweet0,Sour0".split(",");

Collections.addAll(appleStore, strArray);

Collections.addAll(appleStore, Sweet1, Sour2);

此使用了靜態方法Collections.addAll, 可以發現其在初始化上更具有靈活性,第一個參數為Collection對象,第二個可以是一個數組或可變參數列表(實質是數組,只是java編譯器的可變參數列表功能,可以實現自動的將其轉換為數組)。

 

Collections.addAll()方法和collection.add()在處理過程上具有很大的差別,所以其效率上的差距也很大,適用於不同的場景,以下為國外論壇上的一些經典解釋。

Let's take a closer look at the two of them:

col.addAll(Arrays.asList(1,2,3,4,5)); //此處采用collection.addAll()

Here's what happens:

  1. varags + autoboxing creates Integer[]  //可變參數列表,並且采用Integer的自動包裝器
  2. Arrays.asList creates a List<Integer> backed by the array  //根據數組生成相應的List
  3. addAll iterates over a Collection<Integer> using Iterator<Integer> //通過迭代器訪問並添加每一個對象
// b)
Collections.addAll(col,1,2,3,4,5); //使用Collections.addAll()靜態方法

Here's what happens:

  1. varargs + autoboxing creates Integer[]  //可變參數列表,自動包裝器
  2. addAll iterates over an array (instead of an Iterable<Integer>)  //迭代訪問數組

We can see now that b) may be faster because:

Arrays.asList call is skipped, i.e. no intermediary List is created.   //采用Collections.addAll()可以不用建立過渡的List對象,而是直接進行遍歷添加

Since the elements are given in an array (thanks to varargs mechanism), iterating over them may be faster than using Iterator.

That said, unless profiling shows otherwise, the difference isn't likely to be "significant". Do not optimize prematurely. While Java Collection Framework classes may be slower than arrays, they perform more than adequately for most applications.

API links

Summary

  • If you're adding elements from an array, you can use Collections.addAll(col, arr) //如果通過一個數組來添加,用靜態方法Collections.addAll().
    • Remember that varargs are also done using arrays
  • If you're adding elements from a Collection, use col.addAll(otherCol) //如果是添加一個已經有了點List對象,就用方法collection.addAll().
    • Do NOT e.g. Collections.addAll(col, otherCol.toArray())
      • Such roundabout way is likely to be slower!
  • It's not that one is supremely faster than the other 
    • It's about skipping unnecessary steps given the current situation  //關鍵是根據實際的情形進行選擇,兩者都很重要!

 

 

 


免責聲明!

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



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