List
实现了List接口的方法:
AbstractList, AbstractSequentialList, ArrayList, AttributeList, CopyOnWriteArrayList, LinkedList, RoleList, RoleUnresolvedList, Stack, Vector
解析
可以看到 List 继承了 Collection 接口.比起Collection而言List多了很多方法,同时还重写了Spliterator方法.以及默认实现了部分方法.
下面先看一看已经默认实现了的方法
replaceAll
可以看到,replaceAll的实现方式是:
- 传入一个UnaryOperator
- 调用this.listIterator();
- 如果 迭代器 有下一个元素
- 将下一个元素的值赋值为UnaryOperator.apply的结果
- 如果 没有
使用demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| public class Main {
public static void main(String[] args){
List list = new ArrayList<Integer>(); for(int i = 0; i < 10 ; i++){ list.add(i); }
for(int i =0 ; i < list.size();i++){ System.out.print(list.get(i)); }
System.out.println();
list.replaceAll(new UnaryOperator<Integer>() { @Override public Integer apply(Integer integer) { return integer <= 4 ? integer : 4; } });
for(int i =0 ; i < list.size();i++){ System.out.print(list.get(i)); }
System.out.println(); }
}
|
输出结果
用lambda表达式
1
| list.replaceAll(o -> (Integer) o > 4? 4:o);
|
结果一样
sort
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| default void sort(Comparator<? extends 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 ); } }
``` java public interface List<E> extends Collection<E> { int size();
boolean isEmpty();
boolean contains(Object o);
Iterator<E> iterator();
Object[] toArray();
<T> T[] toArray(T[] a);
boolean add(E e);
boolean remove(Object o);
boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c);
boolean addAll(int index, Collection<? extends E> c);
boolean removeAll(Collection<?> c);
boolean retainAll(Collection<?> c);
default void replaceAll(UnaryOperator<E> operator){ Objects.requireNonNull(operator);
final ListIterator<E> li = this.listIterator(); while( li.hasNext()){ li.set(operator.apply(li.next())); } }
default void sort(Comparator<? extends 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 ); } }
void clear();
boolean equals(Object o);
int hashCode();
E get(int index);
E set(int index, E element);
void add(int index, E element);
E remove(int index);
int indexOf(Object o);
int lastIndexOf(Object o);
ListIterator<E> listIterator();
ListIterator<E> listIterator(int index);
List<E> subList(int fromIndex, int toIndex);
@Override default Spliterator<E> Spliterator(){ return Spliterators.spliterator(this,Spliterator.ORDERED); } }
|
- 先把List转换成数组
- 然后通过Arrays的静态方法传入数组和Comparator进行排序
- 获取迭代器,调用迭代器的next(),set()方法按序赋值
### Arrays
是一个工具类,包含了控制数组的各个方法