collect.Lists类

 1、reverse  反转

Lists.reverse(Arrays.asList(1, 2, 3))  结果是:3,2,1

那看下面的源码发现:reverse方法方法并不是真的把list reverse一下,

    public ImmutableList.Builder addAll(Iterable elements) {
        Preconditions.checkNotNull(elements);
        if (elements instanceof Collection) {
            Collection collection = (Collection)elements;
            this.getReadyToExpandTo(this.size + collection.size());
            if (collection instanceof ImmutableCollection) {
                ImmutableCollection immutableCollection = (ImmutableCollection)collection;
                this.size = immutableCollection.copyIntoArray(this.contents, this.size);
                return this;
            }
        }

        super.addAll(elements);
        return this;
    }

//反转

private static class ReverseImmutableList extends ImmutableList {
    private final transient ImmutableList forwardList;

    ReverseImmutableList(ImmutableList backingList) {
        this.forwardList = backingList;
    }

    private int reverseIndex(int index) {//反转的位置,比如  1,2,3    反转的下标为:3-1-0=2,那么反转之后就在2号下标所  处的位置上
        return this.size() - 1 - index;
    }

    private int reversePosition(int index) {
        return this.size() - index;
    }
 

2、partition方法

     Lists.partition(Arrays.asList(1, 2, 3,4,5),3)     返回结果:1,2,3             4,5

private static class Partition extends AbstractList> {
    final List list;   //需要切分的list
    final int size;  //切分的大小

    Partition(List list, int size) {
        this.list = list;
        this.size = size;
    }

    public List get(int index) {
        Preconditions.checkElementIndex(index, this.size());   //检查边界
        int start = index * this.size;        
        int end = Math.min(start + this.size, this.list.size());
        return this.list.subList(start, end); //sublist方法获得start和end之间的元素
    }

    public int size() {
        return IntMath.divide(this.list.size(), this.size, RoundingMode.CEILING);
    }//list的切割

    public boolean isEmpty() {
        return this.list.isEmpty();
    }
}

 

 

你可能感兴趣的:(JAVA)