Guava之Iterables使用示例
这是一个常量工具类。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使用示例的更多相关文章
- Guava之FluentIterable使用示例
FluentIterable 是guava集合类中常用的一个类,主要用于过滤.转换集合中的数据:FluentIterable是一个抽象类,实现了Iterable接口,大多数方法都返回FluentIte ...
- Guava之ImmutableMap使用示例
ImmutableMap 的作用就是:可以让java代码也能够创建一个对象常量映射,来保存一些常量映射的键值对. 分析以下情景,来具体讨论这个的好处. 假设现在有需求如下:根据数据库存的某个key字段 ...
- Guava RateLimiter限流器使用示例
Guava中的RateLimiter可以限制单进程中某个方法的速率,本文主要介绍如何使用,实现原理请参考文档:推荐:超详细的Guava RateLimiter限流原理解析和推荐:RateLimiter ...
- Guava Cache 使用笔记
https://www.cnblogs.com/parryyang/p/5777019.html https://www.cnblogs.com/shoren/p/guava_cache.html J ...
- Spring cloud微服务安全实战-3-3 API安全机制之流控
首先要保证你的服务是可用的,其中一个重要的手段就是流控.就是流量控制.比如我的系统每秒只能处理500个请求,那么多余的请求就拒绝掉.这样我的系统不会被压死 实际的开发中,所要面对的流控场景实际是非常复 ...
- Guava并发:ListenableFuture与RateLimiter示例
ListenableFuture顾名思义就是可以监听的Future,它是对java原生Future的扩展增强 RateLimiter类似于JDK的信号量Semphore,他用来限制对资源并发访问的线程 ...
- Guava的常用方法示例
Guava Maven Dependency <dependency> <groupId>com.google.guava</groupId> <artifa ...
- Guava cache 示例
pom.xml <!-- guava --> <dependency> <groupId>com.google.guava</groupId> < ...
- Guava 的EventBus示例代码(简单笔记,后期补充)
package guavademo.event.bus; import com.google.common.eventbus.EventBus; import com.google.common.ev ...
随机推荐
- 007.Zabbix监控图形绘制
一 Graphs配置 1.1 新建图形 Graphs是将数据展示为图像,以视觉化形式展示,Graphs的配置保存在主机和模板中. Configuration---->Hosts---->G ...
- kafka配置监控和消费者测试
概念 运维 配置 监控 生产者与消费者 流处理 分区partition 一定条件下,分区数越多,吞吐量越高.分区也是保证消息被顺序消费的基础,kafka只能保证一个分区内消息的有序性 副本 每个分区有 ...
- 深入分析Spring Boot2,解决 java.lang.ArrayStoreException异常
将某个项目从Spring Boot1升级Spring Boot2之后出现如下报错,查了很多不同的解决方法都没有解决: Spring boot2项目启动时遇到了异常: java.lang.ArraySt ...
- [转]关于一些SPFA的标程
SPFA算法 求单源最短路的SPFA算法的全称是:Shortest Path Faster Algorithm. 最短路径快速算法-SPFA算法是西南交通大学段凡丁于1994年发表的. 适用范围:给定 ...
- Redis高可用之集群配置(六)
0.Redis目录结构 1)Redis介绍及部署在CentOS7上(一) 2)Redis指令与数据结构(二) 3)Redis客户端连接以及持久化数据(三) 4)Redis高可用之主从复制实践(四) 5 ...
- hdu 5761 Rower Bo 物理题
Rower Bo 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5761 Description There is a river on the Ca ...
- MikroTik RouterOS安装后初始化配置(PPPOE拨号上网)
1.修改登入密码 路由器默认登入账号为admin,密码为空,强烈建议修改登入密码保证安全: 2.修改接口名称 选择Interface,切换到Ethernet标签,找到状态是R(run)的两个端口. 给 ...
- VIM简单配置(windows)
set number set history=1000000 set tabstop=4 set shiftwidth=4 set smarttab set nocp filetype plugin ...
- 使用winrar自解压功能制作安装包
参考文献: bat脚本设置文件的只读属性:http://wenda.tianya.cn/question/0f484c28ffd8d4e9 bat脚本创建internet快捷方式:http://www ...
- netty 支持多种通讯协议
通讯协议,指的是把Netty通讯管道中的二进制流转换为对象.把对象转换成二进制流的过程.转换过程追根究底还是ChannelInboundHandler.ChannelOutboundHandler的实 ...