Java 集合 List

List集合表明一個元素有序、可重複的集合,集合中每一個元素都有其對應的順序索引。List集合能夠經過索引來訪問指定位置的集合元素。List集合默認按元素的添加順序設置元素的索引。java

Java8改進的List接口和ListIterator接口

普通方法

List是有序集合,所以List集合裏增長了一些根據索引來操做集合元素的方法數組

  • void add(int index, Object element):將元素element插入到List集合的index處安全

  • boolean addAll(int index, Collection c):將集合c所包含的全部元素都插入到List集合的index處工具

  • Object get(int index):返回集合index索引處的元素性能

  • int indexOf(Object o):返回對象o在List集合中第一次出現的位置索引線程

  • int lastIndexOf(Object o):返回對象o在List集合中最後一次出現的位置索引code

  • Object remove(int index):刪除並返回index索引處的元素對象

  • Object set(int index, Object element):將index索引處的元素替換成element對象,返回被替換的舊元素排序

  • List subList(int fromIndex, int toIndex):返回從索引fromIndex(包含)到索引toIndex(不包含)處全部集合元素組成的子集合繼承

import java.util.*;

public class ListTest3 
{
    public static void main(String[] args) 
    {
        List teams = new ArrayList();
        //向teams集合中添加三個元素
        teams.add(new String("克利夫蘭騎士"));
        teams.add(new String("金州勇士"));
        teams.add(new String("俄克拉荷馬雷霆"));
        System.out.println(teams);
        //將新字符串對象插入在第二個位置
        teams.add(1, new String("費城76人"));        
        for (int i = 0 ; i < teams.size() ; i++ )
        {
            System.out.println(teams.get(i));
        }
        //刪除第3個元素
        teams.remove(2);
        System.out.println(teams);
        //判斷指定元素在List集合中的位置:輸出1,代表位於第二位
        System.out.println(teams.indexOf(new String("費城76人")));        ①
        //將第二個元素替換成新的字符串對象
        teams.set(1, new String("洛杉磯快船"));
        System.out.println(teams);
        //將teams集合的第二個元素(包括)
        //到第三個元素(不包括)截取成子集合
        System.out.println(teams.subList(1, 2));
    }
}
[克利夫蘭騎士, 金州勇士, 俄克拉荷馬雷霆]
克利夫蘭騎士
費城76人
金州勇士
俄克拉荷馬雷霆
[克利夫蘭騎士, 費城76人, 俄克拉荷馬雷霆]
1
[克利夫蘭騎士, 洛杉磯快船, 俄克拉荷馬雷霆]
[洛杉磯快船]

從代碼①處可知,List判斷兩個對象相等的標準只要equals()方法返回true便可

默認方法

  • void replaceAll(UnaryOperator operator):根據operator指定的計算規則從新設置List集合的全部元素

  • void sort(Comparator c):根據Comparator參數對List集合的元素排序

import java.util.*;

public class ListTest3
{
    public static void main(String[] args)
    {
        List teams = new ArrayList();
        // 向books集合中添加4個元素
        teams.add(new String("克利夫蘭騎士"));
        teams.add(new String("金州勇士"));
        teams.add(new String("俄克拉荷馬雷霆"));
        teams.add(new String("費城76人"));
        // 使用目標類型爲Comparator的Lambda表達式對List集合排序
        teams.sort((o1, o2)->((String)o1).length() - ((String)o2).length());
        System.out.println(teams);
        // 使用目標類型爲UnaryOperator的Lambda表達式來替換集合中全部元素
        // 該Lambda表達式控制使用每一個字符串的長度做爲新的集合元素
        teams.replaceAll(ele->((String)ele).length());
        System.out.println(teams); // 輸出[4, 5, 6, 7]
    }
}

ListIterator方法

ListIterator()方法返回一個ListIterator對象,ListIterator接口繼承了Iterator接口,提供了專門操做List的方法,ListIterator接口在Iterator接口基礎上增長了以下方法

  • boolean hasPrevious():返回該迭代器相關的集合是否還有上一個元素

  • Object previous():返回該迭代器的上一個元素

  • void add(Object o):在指定位置插入一個元素

ListIterator與Iterator對比

  • ListIterator增長了向前迭代的功能,Iterator只能向後迭代

  • ListIterator能夠經過add()方法向List集合中添加元素,Iterator只能刪除元素

import java.util.*;
public class ListIteratorTest
{
    public static void main(String[] args)
    {
        String[] teams = {
            "金州勇士", "俄克拉荷馬雷霆",
            "克利夫蘭騎士"
        };
        List teamList = new ArrayList();
        for (int i = 0; i < teams.length ; i++ )
        {
            teamList.add(teams[i]);
        }
        ListIterator lit = teamList.listIterator();
        while (lit.hasNext())
        {
            System.out.println(lit.next());
            lit.add("-------分隔符-------");
        }
        System.out.println("=======下面開始反向迭代=======");
        while(lit.hasPrevious())
        {
            System.out.println(lit.previous());
        }
    }
}
金州勇士
俄克拉荷馬雷霆
克利夫蘭騎士
=======下面開始反向迭代=======
-------分隔符-------
克利夫蘭騎士
-------分隔符-------
俄克拉荷馬雷霆
-------分隔符-------
金州勇士

ArrayList和Vector實現類

ArrayList和Vector都是基於數組實現的List類,因此ArrayList和Vector類封裝了一個動態的、容許再分配的Object[]數組。initialCapacity參數用來設置該數組的長度,若是向ArrayList和Vector添加大量元素時,可以使用ensureCapacity(int minCapacity)方法一次性增長initialCapacity。減小重分配次數,提供性能

建立空的ArrayList和Vector集合時不指定initialCapacity參數,則Object[]數組的長度默認爲10

從新分配Object[]數組的方法

  • void ensureCapacity(int minCapacity):將ArrayList和Vector集合的長度增長大於或大於minCapacity值

  • void trimToSize():調整ArrayList和Vector集合的Object[]數組長度爲當前元素的個數。調用該方法可減小ArrayList和Vector集合對象佔用的存儲空間

Vector的系列方法名長,具備不少缺點,一般儘可能少用Vector實現類

ArrayList和Vector的區別

  • ArrayList是線程不安全,當多個線程訪問同一個ArrayList集合時,若是有超過一個線程修改了ArrayList集合,則程序必須手動保證該集合的同步性

  • Vector是線程安全,無須程序保證該集合的特別想,也由於線程安全,Vector的性能比ArrayList的性能低

  • Collections工具類能夠將一個ArrayList變成線程安全,所以依然不推薦Vector實現類

固定長度的List

操做數組的工具類:Arrays,該工具類提供了asList(Object.. a)方法,能夠把一個數組或者指定個數的對象轉換成一個List集合,這個List集合既不是ArrayList實現類的實例,也不是Vector實現類的實例,而是Arrays的內部類ArrayList的實例

Arrays.ArrayList是一個固定長度的List集合,程序只能遍歷訪問該集合裏的元素,不可增長、刪除該集合裏的元素

import java.util.*;

public class FixedSizeList
{
    public static void main(String[] args)
    {
        List fixedList = Arrays.asList("克利夫蘭騎士", "金州勇士");
        // 獲取fixedList的實現類,將輸出Arrays$ArrayList
        System.out.println(fixedList.getClass());
        // 使用方法引用遍歷集合元素
        fixedList.forEach(System.out::println);
        // 試圖增長、刪除元素都會引起UnsupportedOperationException異常
        fixedList.add("俄克拉荷馬雷霆");
        fixedList.remove("金州勇士");
    }
}
相關文章
相關標籤/搜索