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 ...
随机推荐
- 阿里云 rds python sdk不支持python3处理
阿里云文档中心的python版本aliyun-python-sdk-rds不支持python3处理 问题:默认情况下文档中心的python版本只支持python2,不兼容python3版本 需要稍微修 ...
- 命令:mktemp
简介 mktemp命令用于创建一个临时的文件或者目录. 语法格式 mktemp [OPTION]... [TEMPLATE] 示例 不带选项和参数的mktemp用于创建临时文件,带-d选项用于创建临时 ...
- css 控制文字超出时显示省略号
不多说,直接看代码吧: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...
- HTML基础单页面总结(基于w3school教程)
学习了一阵http://www.w3school.com.cn网站上的html教程,发现各个知识点比较分散,个人比较倾向于用一页html文档就把所有涉及的基本html标签元素知识点都展示出来的形式.个 ...
- brew安装sshpass
有以下解决方法: # 1 brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formu ...
- spring-boot 速成(10) -【个人邮箱/企业邮箱】发送邮件
发邮件是一个很常见的功能,代码本身并不复杂,有坑的地方主要在于各家邮件厂家的设置,下面以qq个人邮箱以及腾讯企业邮箱为例,讲解如何用spring-boot发送邮件: 一.添加依赖项 compile ' ...
- nginx php-fpm安装配置(转)
nginx本身不能处理PHP,它只是个web服务器,当接收到请求后,如果是php请求,则发给php解释器处理,并把结果返回给客户端. nginx一般是把请求发fastcgi管理进程处理,fascgi管 ...
- How to tell if a file is an EXE or a DLL?
How to tell if a file is an EXE or a DLL? void DumpFile(LPWSTR filename) { HANDLE hFile = CreateFile ...
- python实例[判断操作系统类型]
参考文献:http://bbs.chinaunix.net/thread-1848086-1-1.html 经常地我们需要编写跨平台的脚本,但是由于不同的平台的差异性,我们不得不获得当前所工作的平台( ...
- FindWindow用法
函数功能:该函数获得一个顶层窗口的句柄,该窗口的类名和窗口名与给定的字符串相匹配.这个函数不查找子窗口.在查找时不区分大小写. 函数型:HWND FindWindow(LPCTSTR IpClassN ...