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 ...
随机推荐
- SQL中的坑
一.where,group by,having --group by 和having 解释:前提必须了解sql语言中一种特殊的函数:聚合函数, 例如SUM, COUNT, MAX, AVG等.这些函数 ...
- map的基本操作函数
C++ maps是一种关联式容器,包含“关键字/值”对 begin() 返回指向map头部的迭代器 clear() 删除所有元素 count() 返回指定 ...
- gson 生成json有\u003d异常字符处理
只需将Gson的初始化修改为 Gson gson = new GsonBuilder().disableHtmlEscaping().create(); 连接 http://blog.csdn.net ...
- [CQOI2007]涂色
[CQOI2007]涂色 题目大意: 假设你有一条长度为\(n\)的木版,初始时没有涂过任何颜色.每次你可以把一段连续的木版涂成一个给定的颜色,后涂的颜色覆盖先涂的颜色.问达到给定的目标至少要多少次操 ...
- URAL 1970 J - 皇后像廣場 dfs
J - 皇后像廣場 题目连接: http://acm.hust.edu.cn/vjudge/contest/123332#problem/J Description Vova was walking ...
- Linux——线程
线程 我们都知道一个程序的执行是由进程来完成的,而进程里真正执行代码却是由线程来完成,它是真正的执行流.通常将一个程序⾥里一个执行路线就叫做线程(thread).对它更准确的定义是:线程是“一个进程内 ...
- 编译 php-memcache 扩展时提示Cannot find autoconf
下载memcache扩展 http://pecl.php.net/package/memcache ,到 /usr/local/src目录下并解压 [root@bogon src]# .tgz [ro ...
- Booting LPC-Link2, Updating LPCXpresso firmware
Booting LPC-Link2 The recommended way to use LPC-Link2 with the LPCXpresso IDE is to boot and soft l ...
- 用sourceTree提交代码时遇到的问题
xcuserstate 每次并没有改什么东西,只是随便点了几下就会出现的未暂存文件,可以对其停止追踪! 右键,停止追踪,提交,推送.以后就不会再有这个讨厌的文件出现了! 还没有提交就拉代码的囧境 有的 ...
- drp错误集锦---“Cannot return from outside a function or method”
好久都不动的项目,今天打开项目突然是红色感叹号.详细错误表现为: 也就是说,如今MyEclipse已经不识别在JSP页面中使用的return方法了(并且不止一处这种警告),那怎么办?????顿时闹钟一 ...