collect.Lists类

 1、reverse  反转

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

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

    public ImmutableList.Builder<E> addAll(Iterable<? extends E> 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<E> extends ImmutableList<E> {
    private final transient ImmutableList<E> forwardList;

    ReverseImmutableList(ImmutableList<E> 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<T> extends AbstractList<List<T>> {
    final List<T> list;   //需要切分的list
    final int size;  //切分的大小

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

    public List<T> 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();
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值