Guava之Iterables使用示例
发布日期:2021-08-26 13:56:23 浏览次数:1 分类:技术文章

本文共 3528 字,大约阅读时间需要 11 分钟。

这是一个常量工具类。Iterables类包含了一系列的静态方法,来操作或返回Iterable对象。

public final class Iterables {  private Iterables() {}}

1.boolean removeAll(Iterable removeFrom,Collection elementsToRemove)

/** * Removes, from an iterable, every element that belongs to the provided * collection. * * 

This method calls Collection#removeAll if iterable is a * collection, and Iterators#removeAll otherwise. */@CanIgnoreReturnValuepublic static boolean removeAll(Iterable

removeFrom, Collection
elementsToRemove) { return (removeFrom instanceof Collection) ? ((Collection
) removeFrom).removeAll(checkNotNull(elementsToRemove)) : Iterators.removeAll(removeFrom.iterator(), elementsToRemove);}

实例:

public class Test {    public static void main(String[] args) {        List
list = Lists.newArrayList(); list.add("one"); list.add("two"); list.add("three"); List
list2 = Lists.newArrayList(); list2.add("two"); list2.add("four"); System.out.println(Iterables.removeAll(list, list2)); // true System.out.println(list.toString()); // [one, three] }}

2.boolean retainAll(Iterable removeFrom,Collection elementsToRetain)

/** * Removes, from an iterable, every element that does not belong to the * provided collection. * * 

This method calls Collection#retainAll if iterable is a * collection, and Iterators#retainAll otherwise. */@CanIgnoreReturnValuepublic static boolean retainAll(Iterable

removeFrom, Collection
elementsToRetain) { return (removeFrom instanceof Collection) ? ((Collection
) removeFrom).retainAll(checkNotNull(elementsToRetain)) : Iterators.retainAll(removeFrom.iterator(), elementsToRetain);}

实例:

public class Test {    public static void main(String[] args) {        List
list = Lists.newArrayList(); list.add("one"); list.add("two"); list.add("three"); List
list2 = Lists.newArrayList(); list2.add("two"); list2.add("three"); list2.add("four"); System.out.println(Iterables.retainAll(list, list2)); // true System.out.println(list.toString()); // [two, three] }}

3.boolean removeIf(Iterable removeFrom,Predicate predicate)

/** * Removes, from an iterable, every element that satisfies the provided * predicate. * * 

Removals may or may not happen immediately as each element is tested * against the predicate. The behavior of this method is not specified if * {

@code predicate} is dependent on {
@code removeFrom}. */@CanIgnoreReturnValuepublic static
boolean removeIf(Iterable
removeFrom, Predicate
predicate) { if (removeFrom instanceof RandomAccess && removeFrom instanceof List) { return removeIfFromRandomAccessList((List
) removeFrom, checkNotNull(predicate)); } return Iterators.removeIf(removeFrom.iterator(), predicate);}

实例:

public class Test {    public static void main(String[] args) {        List
list = Lists.newArrayList(); list.add("one"); list.add("three"); list.add("two"); list.add("two"); list.add("three"); Iterables.removeIf(list, new Predicate
() { // 移除集合中使得apply()方法返回为true的元素 @Override public boolean apply(String input) { return input.length() == 5; } }); // [one, two, two] System.out.println(list.toString()); }}

转载地址:https://blog.csdn.net/weixin_33888907/article/details/85976771 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:黄聪:初识Pjax:pjax是什么
下一篇:分布式和集群的区别

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年03月23日 12时14分50秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章