Guava-Collections2源码解析

构造器

private Collections2() {

}

私有构造器,也没有静态构造器,所以可以很明确它是一个纯工具类了。


 

功能方法

filter过滤方法

传入一个带过滤的容器,和一个实现过滤规则的函数类,返回一个带有过滤动作的容器

public static <E> Collection<E> filter(Collection<E> unfiltered, Predicate<? super E> predicate) {

    return unfiltered instanceof Collections2.FilteredCollection?((Collections2.FilteredCollection)unfiltered).createCombined(predicate):new Collections2.FilteredCollection((Collection)Preconditions.checkNotNull(unfiltered), (Predicate)Preconditions.checkNotNull(predicate));

}

如果集成了Collections2.FilteredCollection类,则直接转型到Collections2.FilteredCollection,然后返回这个类。如果不是Collections2.FilteredCollection,则new一个,将传入的容器和规则传入。

看一下Collections2.FilteredCollection类的源码

static class FilteredCollection<E> extends AbstractCollection<E> {

    //存储待处理的集合

    final Collection<E> unfiltered;

    //存储过滤规则

    final Predicate<? super E> predicate;

  

    FilteredCollection(Collection<E> unfiltered, Predicate<? super E> predicate) {

        this.unfiltered = unfiltered;

        this.predicate = predicate;

    }

    //根据新的过滤规则和原来的过滤规则合并创建一个新的容器

    Collections2.FilteredCollection<E> createCombined(Predicate<? super E> newPredicate) {

        return new Collections2.FilteredCollection(this.unfiltered, Predicates.and(this.predicate, newPredicate));

    }

    //添加元素方法

    public boolean add(E element) {

        //根据过滤规则进行测试是否符合,如果符合便进行添加

        Preconditions.checkArgument(this.predicate.apply(element));

        return this.unfiltered.add(element);

    }

    //添加一个容器中所有元素

    public boolean addAll(Collection<? extends E> collection) {

        Iterator i$ = collection.iterator();

        //遍历容器,并对每一个容器进行塞选操作

        while(i$.hasNext()) {

            Object element = i$.next();

            Preconditions.checkArgument(this.predicate.apply(element));

        }

        //如果都满足就添加

        return this.unfiltered.addAll(collection);

    }

  

    public Iterator<E> iterator() {

        return Iterators.filter(this.unfiltered.iterator(), this.predicate);

    }

  

    public int size() {

        return Iterators.size(this.iterator());

    }

}


转型方法

传入一个转型的类,在传入一个转型规则

public static <F, T> Collection<T> transform(Collection<F> fromCollection, Function<? super F, T> function) {

    return new Collections2.TransformedCollection(fromCollection, function);

}

看以上代码,可以看到,直接创建了一个Collections2.TransformedCollection类。

static class TransformedCollection<F, T> extends AbstractCollection<T> {

    //需要转型的容器

    final Collection<F> fromCollection;

    //转型规则

    final Function<? super F, ? extends T> function;

    TransformedCollection(Collection<F> fromCollection, Function<? super F, ? extends T> function) {

        this.fromCollection = (Collection)Preconditions.checkNotNull(fromCollection);

        this.function = (Function)Preconditions.checkNotNull(function);

    }

    public void clear() {

        this.fromCollection.clear();

    }

    public boolean isEmpty() {

        return this.fromCollection.isEmpty();

    }

    //根据转型规则进行迭代

    public Iterator<T> iterator() {

        return Iterators.transform(this.fromCollection.iterator(), this.function);

    }

    public int size() {

        return this.fromCollection.size();

    }

}


有序排列方法

有序排列有两种方法:

//第一种是调用了第二种

public static <E extends Comparable<? super E>> Collection<List<E>> orderedPermutations(Iterable<E> elements) {

    return orderedPermutations(elements, Ordering.natural());

}

//第二种直接创建了Collections2.OrderedPermutationCollection类

public static <E> Collection<List<E>> orderedPermutations(Iterable<E> elements, Comparator<? super E> comparator) {

    return new Collections2.OrderedPermutationCollection(elements, comparator);

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值