源码笔记

源码

List源码

没有看不懂的源码,只有心不静的程序员。

List源码

An ordered collection (also known as a sequence). The user of this interface has precise control over
where in the list each element is inserted.  The user can access elements by their integer index
(position in the list), and search for elements in the list.

一个有序的集合(也被称为序列)。使用这个接口精确的控制list集合中每一个元素被插入。使用者可以访问元素通过它们的整数索引下标(或者将元素放置在list集合中),或者在list集合中查询元素。

Unlike sets, lists typically allow duplicate elements.  More formally, lists typically allow pairs of
elements e1 and e2 such that e1.equals(e2), and they typically allow multiple null elements if they
allow null elements at all. It is not inconceivable that someone might wish to implement a list that
prohibits duplicates, by throwing runtime exceptions when the user attempts to insert them, but we
expect this usage to be rare.

不像Set集合,list集合通常允许存放重复的元素。更正式地,list集合允许一对相等的元素e1和e2存在,如果它们允许所有的元素为null,它们通常允许存放多个的null元素。一些人可能希望实现一个禁止重复的list集合,当使用者尝试去插入它们时,通过抛出运行时异常,它不是难以想象的,但是我们要求的这个用法是少见的。

The List interface places additional stipulations, beyond those specified in the Collection
interface, on the contracts of the iterator, add, remove, equals, and hashCode methods. Declarations
for other inherited methods are also included here for convenience.

list接口添加了附加的规定,晚于那些在Collection接口中明确规定的迭代器、添加、移除、相等和哈希码方法。为了便利,还声明了对于包含其他继承的方法。

The List interface provides four methods for positional (indexed) access to list elements. Lists
(like Java arrays) are zero based. Note that these operations may execute in time proportional to
the index value for some implementations (the LinkedList class, for example). Thus, iterating over
the elements in a list is typically preferable to indexing through it if the caller does not know
the implementation.

list接口提供四个方法,通过位置去访问list集合中的元素。List(像Java数组)是底层集合。注意,这些操作的执行时间可能与某些实现的索引值成比例(例如,LinkedList类)。因此,如果调用者不知道这个实现类,在一个list集合中迭代每个元素通常优于通过索引的查找。

The List interface provides a special iterator, called a ListIterator, that allows
element insertion and replacement, and bidirectional access in addition to the normal
operations that the Iterator interface provides.  A method is provided to obtain a list
iterator that starts at a specified position in the list.

list接口提供了一个特殊的迭代器,叫做list迭代器,list迭代器允许元素插入和替换,但是除了不同寻常的双向存取操作外是迭代器接口提供。在list集合中指定具体的位置时,提供了一个存在list迭代器的方法。


List是一个接口继承了Collection接口,Collection接口继承了Iterable接口;同时List接口继承了Collection和Iterable接口的所有方法。
List接口中方法的详细使用:
size方法及用例:

//@return the number of elements in this list
//返回list集合中元素的个数
int size();
//-----------------------------------------------
public class CollectionDemo{
	public static void main(String[] args) {
        List list = new ArrayList();
        list.add("a");
        list.add("b");
        list.add("c");
        System.out.println(list.size());
    }
}

用例编译运行的结果:

list Length:3

isEmpty方法及用例:

//@return true if this list contains no elements
//如果list集合不包含任何元素返回true
boolean isEmpty();
//-----------------------------------------------
List list = new ArrayList();
System.out.println(list.isEmpty());
list.add("a");
list.add("b");
System.out.println(list.isEmpty());

用例编译运行的结果:

true
false

contains方法及用例:

//@return true if this list contains the specified element
//如果list集合包含指定的元素返回true
//@throws ClassCastException if the type of the specified element is incompatible with this list
//如果指定的元素类型与list集合中的类型不一致,则抛出类型转换异常。
//@throws NullPointerException if the specified element is null and this list does not permit null elements
//如果指定的元素为空或者list集合不允许空元素,则抛出空指针异常。
boolean contains(Object o);
//-----------------------------------------------
List list = new ArrayList<>();
list.add("a");
System.out.println(list.contains("b"));
list.add("b");
System.out.println(list.contains("b"));

用例编译运行的结果:

false
true

iterator方法及用例:

//@return an iterator over the elements in this list in proper sequence
//返回一个用适当的顺序迭代list集合中每一个的元素
@NotNull Iterator<E> iterator();
//-----------------------------------------------
List list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
for (Iterator i = list.iterator(); i.hasNext(); ) {
    System.out.print(i.next());
}
//或
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
	System.out.println(iterator.next());
}

用例编译运行的结果:

List Elements:abc
			 :a/n b/n c/n

toArray方法及用例:

//@return an array containing all of the elements in this list in proper sequence
//用适当的顺序返回一个包含list集合中所有元素的数组
@NotNull Object[] toArray()
//-----------------------------------------------
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
Object[] objs = list.toArray();
for (Object o : objs) {
	System.out.println(o);
}

用例编译运行的结果:

a
b
c

带参数的toArray方法及用例:

//@param a the array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
//如果它是足够大的,list中的元素被存储进参数a数组中;否则,在运行时生成一个相同类型的新数组,被当做目标分配这个新数组。
//@return an array containing the elements of this list
//返回一个包含list集合元素的数组
//@throws ArrayStoreException if the runtime type of the specified array is not a supertype of the runtime type of every element in this list
//如果运行时指定的数组类型,在list集合中每一个元素不是父类型,则抛出数组存储异常
//@throws NullPointerException if the specified array is null
//如果指定的数组是空,则抛出空指针异常。
@NotNull<T> T[] toArray(@NotNull T[] a);
//-----------------------------------------------
List list = new ArrayList();
String[] strs = new String[3];
list.add("a");
list.add("b");
list.add("c");
list.toArray(strs);
for (String s : strs) {
    System.out.println(s);
}

用例编译运行的结果:

a
b
c

add方法及用例:

//@param e element to be appended to this list
//参数e元素被添加到list集合中
//@return true (as specified by {@link Collection#add})
//返回一个boolean值true
//@throws UnsupportedOperationException if the add operation is not supported by this list
//如果在list集合中进行添加操作是不支持,则抛出不支持操作异常。
//@throws ClassCastException if the class of the specified element prevents it from being added to this list
//如果指定的元素在添加到list类集合中时被阻止,则抛出类型转换异常。
//@throws NullPointerException if the specified element is null and this list does not permit null elements
//如果指定的元素是空值时,并且list集合不允许是null元素时,则抛出空指针异常。
//@throws IllegalArgumentException if some property of this element prevents it from being added to this list
//如果一些属性的元素被阻止添加到list集合中时,则抛出非法参数异常。
@Contract(value="_->true",mutates="this")boolean add(@Flow(targetIsContainer=true)E e);
//-----------------------------------------------
List list = new ArrayList();
list.add("a");
System.out.println(list.get(0));

用例编译运行的结果:

a

remove方法及用例:

//@param o element to be removed from this list, if present
//如果list集合中存在参数o元素,则被移除。
//@return true if this list contained the specified element
//如果list集合中包含这个指定的元素,则返回一个boolean值true
//@throws ClassCastException if the type of the specified element is incompatible with this list
//如果指定的元素类型与list集合中的类型不相符,则抛出类型转化异常。
//@throws NullPointerException if the specified element is null and this list does not permit null elements
//翻译同上,抛出空指针异常。
//@throws UnsupportedOperationException if the remove operation is not supported by this list
//如果移除list集合中的某个元素是不被支持的,则抛出不支持操作异常。
@Contract(mutates="this")boolean remove(Object o);
//-----------------------------------------------
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
boolean flag1 = list.remove("a");
boolean flag2 = list.remove("d");
System.out.println(flag1);
System.out.println(flag2);

用例编译运行的结果:

true
false

containsAll方法及用例:

//@param  c collection to be checked for containment in this list
//在list容器中参数c集合被检查
//@return true if this list contains all of the elements of the specified collection
//如果list集合中包含所有指定的集合元素,则返回一个boolean值true。
//@throws ClassCastException if the types of one or more elements in the specified collection are incompatible with this list
//如果在指定的集合中存在一个或者更多元素与list集合不相容,则抛出类型转换异常。
//@throws NullPointerException if the specified collection contains one or more null elements and this list does not permit null elements
//如果指定的集合包含一个或者更多个null值元素,并且list集合不允许存在null值元素时,则抛出空指针异常。
//@see #contains(Object)
//看contains方法
boolean containsAll(@NotNull Collection<?> c);
//-----------------------------------------------
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
List list1 = new ArrayList();
list1.add("b");
list1.add("c");
boolean flag1 = list.containsAll(list1);
list1.add("e");
boolean flag2 = list.containsAll(list1);
System.out.println(flag1 + "&" + flag2);

用例编译运行的结果:

true&false

addAll方法及用例:

//@param c collection containing elements to be added to this list
//参数c集合包含的元素被添加到list集合中。
//@return true if this list changed as a result of the call
//如果list集合结果改变了,则返回一个boolean值true。
//@throws UnsupportedOperationException if the addAll operation is not supported by this list
//如果在list集合中添加所有元素的操作不被支持,则会抛出不支持操作异常。
//@throws ClassCastException if the class of an element of the specified collection prevents it from being added to this list
//如果指定集合的元素的类被阻止添加到list集合中,则抛出类型转换异常。
//@throws NullPointerException if the specified collection contains one or more null elements and this list does not permit null elements, or if the specified collection is null
//如果指定集合中包含一个或更多的null元素,并且该list集合不允许存在null值,或者指定的集合是否是null,则抛出空指针异常。
//@throws IllegalArgumentException if some property of an element of the specified collection prevents it from being added to this list
//如果指定的集合存在一些属性元素被阻止添加到list集合中,则抛出非法数据异常。
//@see #add(Object)
//看add方法
@Contract(mutates="this")boolean addAll(@NotNull @Flow(sourceIsContainer=true,targetIsContainer=true)Collection<? extends E> c);
//-----------------------------------------------
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
List list1 = new ArrayList();
list1.add("g");
list1.add("k");
list.addAll(list1);
for (Object o : list) {
    System.out.print(o);
}

用例编译运行的结果:

abcgk

带两个参数的addAll方法及用例:

//@param index index at which to insert the first element from the specified collection
//从指定集合中插入第一个元素通过参数给出的索引值。
//@param c collection containing elements to be added to this list
//参数c集合包含的元素被添加到list集合中。
//@return true if this list changed as a result of the call
//如果list集合的结果被改变,则返回一个boolean值true。
//@throws UnsupportedOperationException if the addAll operation is not supported by this list
//如果添加所有元素到list集合中不被支持,则抛出不支持操作异常。
//@throws ClassCastException if the class of an element of the specified collection prevents it from being added to this list
//如果指定集合的元素类被阻止添加到list集合中,则抛出类型转换异常。
//@throws NullPointerException if the specified collection contains one or more null elements and this list does not permit null elements, or if the specified collection is null
//翻译同上,抛出空指针异常。
//@throws IllegalArgumentException if some property of an element of the specified collection prevents it from being added to this list
//翻译同上,抛出非法数据异常。
//@throws IndexOutOfBoundsException if the index is out of range
//如果索引超出范围,则抛出下标超出界限异常。
@Contract(mutates="this")boolean addAll(int index, @NotNull @Flow()Collection<? extends E> c);
//-----------------------------------------------
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
List list1 = new ArrayList();
list1.add("g");
list1.add("k");
list.addAll(2, list1);
for (Object o : list) {
    System.out.print(o);
}

用例编译运行的结果:

abgkc

removeAll方法及用例:

//@param c collection containing elements to be removed from this list
//参数c集合包含的所有元素从list集合中移除
//@return true if this list changed as a result of the call
//如果list集合结果改变了,则返回一个boolean值true。
//@throws UnsupportedOperationException if the removeAll operation is not supported by this list
//翻译同上,抛出不支持操作异常。
//@throws ClassCastException if the class of an element of this list is incompatible with the specified collection
//如果list集合元素的类与指定集合的类不相符,则抛出类转换异常。
//@throws NullPointerException if this list contains a null element and the specified collection does not permit null elements
//翻译同上,抛出空指针异常。
//@see #remove(Object)
//看remove方法
//@see #contains(Object)
//看contains方法
@Contract(mutates="this")boolean removeAll(@NotNull Collection<?> c);
//-----------------------------------------------
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
List list1 = new ArrayList();
list1.add("a");
list1.add("c");
list.removeAll(list1);
for (Object o : list) {
    System.out.print(o);
}

用例编译运行的结果:

b

retainAll方法及用例:

//@param c collection containing elements to be retained in this list
//参数c集合包含的元素被保留在list集合中
//@return true if this list changed as a result of the call
//如果list集合结果改变了,则返回一个boolean值true。
//@throws UnsupportedOperationException if the retainAll operation is not supported by this list
//如果保留操作对于list集合不被支持,则抛出不支持操作异常。
//@throws ClassCastException if the class of an element of this list is incompatible with the specified collection
//如果list集合元素的类与指定集合类是不符的,则抛出类转换异常。
//@throws NullPointerException if this list contains a null element and the specified collection does not permit null elements or if the specified collection is null
//如果list集合包含一个null元素,并且指定集合不允许存在null元素或者指定集合是null时,则抛出空指针异常。
//@see #remove(Object)
//@see #contains(Object)
@Contract(mutates="this")boolean retainAll(@NotNull Collection<?> c);
//-----------------------------------------------
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
List list1 = new ArrayList();
list1.add("a");
list1.add("k");
boolean flag = list.retainAll(list1);
System.out.println(flag);
for (Object o : list) {
    System.out.println(o);
}

用例编译运行的结果:

true
a

replaceAll方法及用例:

//@param operator the operator to apply to each element
//参数operator应用于每一个元素
//@throws UnsupportedOperationException if this list is unmodifiable.Implementations may throw this exception if an element cannot be replaced or if, in general, modification is not supported
//如果list集合是无法改变的。如果一个元素不能被替换,或者通常不支持修改,则实现时可能抛出不支持操作异常。
//@throws NullPointerException if the specified operator is null or if the operator result is a null value and this list does not permit null elements
//如果指定的参数operator是null或者参数operator的结果集为null值时,并且list集合不允许存在null元素,则抛出空指针异常。
//@since 1.8
@Contract(mutates="this")default void replaceAll(UnaryOperator<E> operator) {
        Objects.requireNonNull(operator);
        final ListIterator<E> li = this.listIterator();
        while (li.hasNext()) {
            li.set(operator.apply(li.next()));
        }
    }
//-----------------------------------------------
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
//其中,UnaryOperator<E>这个类型,中文翻译是一元运算符的意思。其继承Function这个接口。
list.replaceAll(t -> t);
for (Object s : list) {
    System.out.print(s);
}
//如果list的类型为Integer类型时,t -> t后面可以了加其他的一元运算符。例:+,*,/
System.out.println();
list.replaceAll(x -> x + "k,");
for (Object o : list) {
    System.out.print(o);
}

用例编译运行的结果:

abc
ak,bk,ck,

sort方法及用例:

//@param c the {@code Comparator} used to compare list elements.A {@code null} value indicates that the elements' {@linkplain Comparable natural ordering} should be used
//参数c被用作比较list集合中的元素。表示一个被使用的元素值。
//@throws ClassCastException if the list contains elements that are not mutually comparable	using the specified comparator
//如果list集合中包含的元素,不被指定的比较器相互比较,则抛出类转换异常。
//@throws UnsupportedOperationException if the list's list-iterator does not support the {@code set} operation
//如果list集合的list迭代器不支持操作,则抛出不支持操作异常。
//@throws IllegalArgumentException if the comparator is found to violate the {@link Comparator} contract
//如果比较器被发现了违反了规定,则抛出非法数据异常。
//@since 1.8
default void sort(Comparator<? super E> c) {
        Object[] a = this.toArray();
        Arrays.sort(a, (Comparator) c);
        ListIterator<E> i = this.listIterator();
        for (Object e : a) {
            i.next();
            i.set((E) e);
        }
    }
//-----------------------------------------------
List list = new ArrayList();
list.add("1");
list.add("a");
list.add("c");
list.add("2");
list.add("3");
list.add("d");
list.sort(new Comparator<String>() {
    @Override
    public int compare(String o1, String o2) {
        return o1.compareTo(o2);
    }
});
System.out.println(list);

用例编译运行的结果:

[1, 2, 3, a, c, d]

clear方法及用例:

//Removes all of the elements from this list (optional operation). The list will be empty after this call returns.
//从list集合中移除所有元素。此调用返回后,list集合将是空集合。
//@throws UnsupportedOperationException if the clear operation is not supported by this list
//如果list集合不被支持清楚操作,则抛出不支持操作异常。
void clear();
//-----------------------------------------------
List list = new ArrayList();
list.add("1");
list.add("a");
list.add("c");
list.add("2");
list.add("3");
list.add("d");
list.clear();
System.out.println(list);

用例编译运行的结果:

[]

equals方法及用例:

//@param o the object to be compared for equality with this list
//为父类型的参数o与list集合比较是否相等
//@return true if the specified object is equal to this list
//如果指定的父类参数与list集合是相等的,则返回boolean值true。
boolean equals(Object o);
//-----------------------------------------------
List list = new ArrayList();
list.add("1");
list.add("a");
list.add("c");
List list2 = new ArrayList();
list2.add("1");
list2.add("a");
list2.add("c");
boolean flag1 = list.equals(list2);
boolean flag2 = list.equals("a");
boolean flag3 = list.equals("b");
System.out.println(flag1);
System.out.println(flag2);
System.out.println(flag3);

用例编译运行的结果:

true
false
false

hashCode方法及用例:

//@return the hash code value for this list
//返回list集合中的哈希码值
//@see Object#equals(Object)
//@see #equals(Object)
int hashCode();
//-----------------------------------------------
List list = new ArrayList();
list.add("1");
list.add("a");
list.add("c");
int hashCode = list.hashCode();
System.out.println(hashCode);

用例编译运行的结果:

79986

get方法及用例:

//@param index index of the element to return
//返回索引下标中的元素。
//@return the element at the specified position in this list
//返回list集合中指定位置的元素
//@throws IndexOutOfBoundsException if the index is out of range
//如果下标索引超出范围,则抛出下标超出界限异常
@Flow(sourceIsContainer=true)E get(int index);
//-----------------------------------------------
List list = new ArrayList();
list.add("1");
list.add("a");
list.add("c");
System.out.println(list.get(1));

用例编译运行的结果:

a

set方法及用例:

//@param index index of the element to replace
//替换下标索引下的元素
//@param element element to be stored at the specified position
//元素被存放在指定位置
//@return the element previously at the specified position
//返回指定位置中的新元素(以前的元素被替换)
//@throws UnsupportedOperationException if the set operation is not supported by this list
//如果从list集合中设置操作不被支持,则抛出不支持操作异常
//@throws ClassCastException if the class of the specified element prevents it from being added to this list
//如果指定元素的类型阻止添加到list集合中,则抛出类转换异常
//@throws NullPointerException if the specified element is null and this list does not permit null elements
//翻译同上,抛出空指针异常
//@throws IllegalArgumentException if some property of the specified element prevents it from being added to this list
//如果一些指定元素的属性被阻止添加到list集合中,则抛出非法数据异常
//@throws IndexOutOfBoundsException if the index is out of range
//如果下标索引超出范围,则抛出下标超出界限异常
@Contract(mutates="this") @Flow(sourceIsContainer=true)E set(int index, @Flow(targetIsContainer=true)E element);
//-----------------------------------------------
List list = new ArrayList();
list.add("1");
list.add("a");
list.add("c");
//下标不能超出范围,否则报以上异常
list.set(2, "b");
System.out.println(list);

用例编译运行的结果:

[1, a, b]

add方法及用例:

//@param index index at which the specified element is to be inserted
//指定的元素被插入到下标索引下
//@param element element to be inserted
//参数element元素被插入
//@throws UnsupportedOperationException if the add operation is not supported by this list
//翻译同上,抛出不支持操作异常
//@throws ClassCastException if the class of the specified element prevents it from being added to this list
//翻译同上,抛出类型转换异常
//@throws NullPointerException if the specified element is null and this list does not permit null elements
//翻译同上,抛出空指针异常
//@throws IllegalArgumentException if some property of the specified element prevents it from being added to this list
//翻译同上,抛出非法数据异常
//@throws IndexOutOfBoundsException if the index is out of range
//翻译同上,下标超出界限异常
@Contract(mutates="this")void add(int index, @Flow(targetIsContainer=true)E element);
//-----------------------------------------------
List list = new ArrayList();
list.add("1");
list.add("a");
list.add("c");
list.add(3, "b");
System.out.println(list);

用例编译运行的结果:

[1, a, c, b]

remove方法及用例:

//@param index the index of the element to be removed
//下标索引下的元素被移除
//@return the element previously at the specified position
//返回指定位置以前在的(移除的)元素
//@throws UnsupportedOperationException if the remove operation is not supported by this list
//翻译同上,抛出不支持操作异常
//@throws IndexOutOfBoundsException if the index is out of range
//翻译同上,下标超出界限异常
@Flow(targetIsContainer=true) @Contract(mutates="this")E remove(int index);
//-----------------------------------------------
List list = new ArrayList();
list.add("1");
list.add("a");
list.add("c");
list.remove(0);
System.out.println(list);

用例编译运行的结果:

[a, c]

indexOf方法及用例:

//@param o element to search for
//搜索参数o元素
//@return the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element
//返回list集合中首次发现的指定元素位置的下标索引值,如果为-1,表示list集合中不包含这个元素
//@throws ClassCastException if the type of the specified element is incompatible with this list
//如果指定的元素类型与list集合中元素的类型不一致,则抛出类型转换异常
//@throws NullPointerException if the specified element is null and this list does not permit null elements
//翻译同上,抛出空指针异常
int indexOf(Object o);
//-----------------------------------------------
List list = new ArrayList();
list.add("1");
list.add("a");
list.add("c");
System.out.println(list.indexOf("a"));

用例编译运行的结果:

1

lastIndexof方法及用例:

//@param o element to search for
//搜索参数o元素
//@return the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element
//返回list集合中最后一次发现指定元素位置的索引下标值,如果为-1则表示list集合不包含该元素
//@throws ClassCastException if the type of the specified element is incompatible with this list
//翻译同上,抛出类型转化异常
//@throws NullPointerException if the specified element is null and this list does not permit null elements
//翻译同上,抛出空指针异常
int lastIndexOf(Object o);
//-----------------------------------------------
List list = new ArrayList();
list.add("1");
list.add("a");
list.add("1");
System.out.println(list.lastIndexOf("1"));

用例编译运行的结果:

2

listIterator方法及用例:

//@return a list iterator over the elements in this list (in proper sequence)
//返回在list集合中的元素列出迭代器
@NotNull ListIterator<E> listIterator();
//-----------------------------------------------
List list = new ArrayList();
list.add("1");
list.add("a");
list.add("b");
ListIterator listIterator = list.listIterator();
while (listIterator.hasNext()) {
    System.out.println(listIterator.next());
}

用例编译运行的结果:

1
a
b

带参数的listIterator方法及用例:

//@param index index of the first element to be returned from the list iterator (by a call to {@link ListIterator#next next})
//从list迭代器中返回参数index索引位置的第一个元素以后的所有元素(按适当顺序)。
//@return a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list
//在list元素集合中,返回一个list迭代器
//@throws IndexOutOfBoundsException if the index is out of range
//翻译同上,抛出下标超出界限异常
@NotNull ListIterator<E> listIterator(int index);
//-----------------------------------------------
List list = new ArrayList();
list.add("1");
list.add("a");
list.add("b");
ListIterator listIterator = list.listIterator(1);
while (listIterator.hasNext()) {
    System.out.println(listIterator.next());
}

用例编译运行的结果:

a
b

subList方法及用例:

//@param fromIndex low endpoint (inclusive) of the subList
//以参数fromIndex值为开始值(包含)截取list集合
//@param toIndex high endpoint (exclusive) of the subList
//以参数toIndex值为结束值(不包含)截取list集合
//@return a view of the specified range within this list
//返回一个list集合中指定范围的子list集合
//@throws IndexOutOfBoundsException for an illegal endpoint index value
//对于一个非法的端点索引值,会抛出下标超出界限异常
@NotNull @Flow(sourceIsContainer=true,targetIsContainer=true)List<E> subList(int fromIndex, int toIndex);
//-----------------------------------------------
List list = new ArrayList();
list.add("1");
list.add("a");
list.add("b");
//前闭后开
List list2 = list.subList(1, 2);
System.out.println(list2);

用例编译运行的结果:

[a]

spliterator方法及用例:

//@return a {@code Spliterator} over the elements in this list
//在list元素集合中,返回一个Spliterator类的值
//@since 1.8
@Override
default Spliterator<E> spliterator() {
	return Spliterators.spliterator(this, Spliterator.ORDERED);
}
//-----------------------------------------------
List list = new ArrayList();
list.add("1");
list.add("a");
list.add("b");
System.out.println(list.spliterator().ORDERED);

用例编译运行的结果:

16

小白一枚,英文更是菜鸟级别,不对的地方欢迎大佬指正…

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值