这是一个常量工具类。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.
*
* <p>This method calls Collection#removeAll if iterable is a
* collection, and Iterators#removeAll otherwise.
*/
@CanIgnoreReturnValue
public 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<String> list = Lists.newArrayList();
list.add("one");
list.add("two");
list.add("three");
List<String> 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.
*
* <p>This method calls Collection#retainAll if iterable is a
* collection, and Iterators#retainAll otherwise.
*/
@CanIgnoreReturnValue
public 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<String> list = Lists.newArrayList();
list.add("one");
list.add("two");
list.add("three");
List<String> 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.
*
* <p>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}.
*/
@CanIgnoreReturnValue
public static <T> boolean removeIf(Iterable<T> removeFrom, Predicate<? super T> predicate) {
if (removeFrom instanceof RandomAccess && removeFrom instanceof List) {
return removeIfFromRandomAccessList((List<T>) removeFrom, checkNotNull(predicate));
}
return Iterators.removeIf(removeFrom.iterator(), predicate);
}

实例:

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

Guava之Iterables使用示例的更多相关文章

  1. Guava之FluentIterable使用示例

    FluentIterable 是guava集合类中常用的一个类,主要用于过滤.转换集合中的数据:FluentIterable是一个抽象类,实现了Iterable接口,大多数方法都返回FluentIte ...

  2. Guava之ImmutableMap使用示例

    ImmutableMap 的作用就是:可以让java代码也能够创建一个对象常量映射,来保存一些常量映射的键值对. 分析以下情景,来具体讨论这个的好处. 假设现在有需求如下:根据数据库存的某个key字段 ...

  3. Guava RateLimiter限流器使用示例

    Guava中的RateLimiter可以限制单进程中某个方法的速率,本文主要介绍如何使用,实现原理请参考文档:推荐:超详细的Guava RateLimiter限流原理解析和推荐:RateLimiter ...

  4. Guava Cache 使用笔记

    https://www.cnblogs.com/parryyang/p/5777019.html https://www.cnblogs.com/shoren/p/guava_cache.html J ...

  5. Spring cloud微服务安全实战-3-3 API安全机制之流控

    首先要保证你的服务是可用的,其中一个重要的手段就是流控.就是流量控制.比如我的系统每秒只能处理500个请求,那么多余的请求就拒绝掉.这样我的系统不会被压死 实际的开发中,所要面对的流控场景实际是非常复 ...

  6. Guava并发:ListenableFuture与RateLimiter示例

    ListenableFuture顾名思义就是可以监听的Future,它是对java原生Future的扩展增强 RateLimiter类似于JDK的信号量Semphore,他用来限制对资源并发访问的线程 ...

  7. Guava的常用方法示例

    Guava Maven Dependency <dependency> <groupId>com.google.guava</groupId> <artifa ...

  8. Guava cache 示例

    pom.xml <!-- guava --> <dependency> <groupId>com.google.guava</groupId> < ...

  9. Guava 的EventBus示例代码(简单笔记,后期补充)

    package guavademo.event.bus; import com.google.common.eventbus.EventBus; import com.google.common.ev ...

随机推荐

  1. 基于CommonsCollections4的Gadget分析

    基于CommonsCollections4的Gadget分析 Author:Welkin 0x1 背景及概要 随着Java应用的推广和普及,Java安全问题越来越被人们重视,纵观近些年来的Java安全 ...

  2. 7.30 正睿暑期集训营 A班训练赛

    目录 2018.7.30 正睿暑期集训营 A班训练赛 T1 A.蔡老板分果子(Hash) T2 B.蔡老板送外卖(并查集 最小生成树) T3 C.蔡老板学数学(DP NTT) 考试代码 T2 T3 2 ...

  3. 喵哈哈村的魔法考试 Round #1 (Div.2) 题解

    喵哈哈村的魔法考试 Round #1 (Div.2) 题解 特别感谢出题人,qscqesze. 也特别感谢测题人Xiper和CS_LYJ1997. 没有他们的付出,就不会有这场比赛. A 喵哈哈村的魔 ...

  4. Gym 100646 You’ll be Working on the Railroad dfs

    You'll be Working on the Railroad 题目连接: http://codeforces.com/gym/100646/attachments Description Con ...

  5. JavaScript学习笔记[0]

    JavaScript学习笔记[0] 使用的是廖雪峰JavaScript教程. 数据类型 Number 表示数字,不区分浮点整形. === 比较时不转化数据类型. == 反之. NaN与任何值都不想等, ...

  6. j.u.c系列(04)---之AQS:同步状态的获取与释放

    写在前面 在前面提到过,AQS是构建Java同步组件的基础,我们期待它能够成为实现大部分同步需求的基础.AQS的设计模式采用的模板方法模式,子类通过继承的方式,实现它的抽象方法来管理同步状态,对于子类 ...

  7. git 用户名和密码保存

    git config --global credential.helper store 输入一次后,后续不再需要输入用户名密码

  8. Bitbox : a small open, DIY 32 bit VGA console

    Bitbox : a small open, DIY 32 bit VGA console Hi all, I've been developing a simple DIY console and ...

  9. Bootstrap datetimepicker “dp.change” 时间/日期 选择事件

    $('#<!--{$inputId}-->').datetimepicker({ todayHighlight: true, format: "YYYY-MM-DD<!-- ...

  10. kcon 黑客大会 github

    https://github.com/knownsec/KCon https://paper.seebug.org/107/ CISP认证 https://habo.qq.com/ https://g ...