Guava包学习---Sets
Sets包的内容和上一篇中的Lists没有什么大的区别,里面有些细节可以看一下:
开始的创建newHashSet()的各个重载方法、newConcurrentHashSet()的重载方法、newTreeSet()、newCopyOnWriteArraySet()等都和Lists中的很相似。Sets中有一个不常用的EnumSet,至少不没太使用过这个集合去做事情。EnumSet是Java枚举类型的泛型容器,它的速度据说比HashSet还要快。如果Sets中的值可枚举,那使用这个应该很不错。上个重载方法看下:
@GwtCompatible(serializable = true)
public static <E extends Enum<E>> ImmutableSet<E> immutableEnumSet(
E anElement, E... otherElements) {
return ImmutableEnumSet.asImmutable(EnumSet.of(anElement, otherElements));
} @GwtCompatible(serializable = true)
public static <E extends Enum<E>> ImmutableSet<E> immutableEnumSet(Iterable<E> elements) {
if (elements instanceof ImmutableEnumSet) {
return (ImmutableEnumSet<E>) elements;
} else if (elements instanceof Collection) {
Collection<E> collection = (Collection<E>) elements;
if (collection.isEmpty()) {
return ImmutableSet.of();
} else {
return ImmutableEnumSet.asImmutable(EnumSet.copyOf(collection));
}
} else {
Iterator<E> itr = elements.iterator();
if (itr.hasNext()) {
EnumSet<E> enumSet = EnumSet.of(itr.next());
Iterators.addAll(enumSet, itr);
return ImmutableEnumSet.asImmutable(enumSet);
} else {
return ImmutableSet.of();
}
}
} public static <E extends Enum<E>> EnumSet<E> newEnumSet(
Iterable<E> iterable, Class<E> elementType) {
EnumSet<E> set = EnumSet.noneOf(elementType);
Iterables.addAll(set, iterable);
return set;
}
其实还是那点东西,就是把传入的内容转换到一个集合中。
还有一种确定大小的Set:
public static <E> HashSet<E> newHashSetWithExpectedSize(int expectedSize) {
return new HashSet<E>(Maps.capacity(expectedSize));
合并两个Set:
public static <E> SetView<E> union(final Set<? extends E> set1, final Set<? extends E> set2) {
checkNotNull(set1, "set1");
checkNotNull(set2, "set2");
//--------````
}
找到两个Set的交集:
public static <E> SetView<E> intersection(final Set<E> set1, final Set<?> set2) {
checkNotNull(set1, "set1");
checkNotNull(set2, "set2");
}
在A不在B中:
public static <E> SetView<E> difference(final Set<E> set1, final Set<?> set2) {
checkNotNull(set1, "set1");
checkNotNull(set2, "set2");
}
在A不在B+在B不在A中:
public static <E> SetView<E> symmetricDifference(
final Set<? extends E> set1, final Set<? extends E> set2) {
checkNotNull(set1, "set1");
checkNotNull(set2, "set2");
}
来个有意思的,传入的是一个回调方法,然后过滤部分值:
public static <E> Set<E> filter(Set<E> unfiltered, Predicate<? super E> predicate) {
if (unfiltered instanceof SortedSet) {
return filter((SortedSet<E>) unfiltered, predicate);
}
if (unfiltered instanceof FilteredSet) {
// Support clear(), removeAll(), and retainAll() when filtering a filtered
// collection.
FilteredSet<E> filtered = (FilteredSet<E>) unfiltered;
Predicate<E> combinedPredicate = Predicates.<E>and(filtered.predicate, predicate);
return new FilteredSet<E>((Set<E>) filtered.unfiltered, combinedPredicate);
} return new FilteredSet<E>(checkNotNull(unfiltered), checkNotNull(predicate));
}
求两个Sets的笛卡尔集的,这个玩意儿真的有用?
* Sets.cartesianProduct(ImmutableList.of(
* ImmutableSet.of(1, 2),
* ImmutableSet.of("A", "B", "C")))}</pre>
*
* <p>returns a set containing six lists:
*
* <ul>
* <li>{@code ImmutableList.of(1, "A")}
* <li>{@code ImmutableList.of(1, "B")}
* <li>{@code ImmutableList.of(1, "C")}
* <li>{@code ImmutableList.of(2, "A")}
* <li>{@code ImmutableList.of(2, "B")}
* <li>{@code ImmutableList.of(2, "C")}
public static <B> Set<List<B>> cartesianProduct(List<? extends Set<? extends B>> sets) {
return CartesianSet.create(sets);
}
求Set的所有子集:
@GwtCompatible(serializable = false)
public static <E> Set<Set<E>> powerSet(Set<E> set) {
return new PowerSet<E>(set);
}
还有其他的一些方法,实在不知道在哪里有用处,就不再看了。接下来来开发中最常用的Maps吧。
Guava包学习---Sets的更多相关文章
- Guava包学习---Lists
Guava包是我最近项目中同事推荐使用的,是google推出的库.里面的功能非常多,包括了集合.缓存.原生类型支持.并发库.通用注解.字符串处理.IO等.我们项目中使用到了guava依赖,但是实际上只 ...
- Guava包学习--EventBus
之前没用过这个EventBus,然后看了一下EventBus的源码也没看明白,(-__-)b.反正大概就是弄一个优雅的方式实现了观察者模式吧.慢慢深入学习一下. 观察者模式其实就是生产者消费者的一个变 ...
- Guava包学习-Cache
这段时间用到了ehcache和memcache,memcache只用来配置在tomcat中做负载均衡过程中的session共享,然后ehcache用来存放需要的程序中缓存. Guava中的Cache和 ...
- Guava包学习---Maps
Maps包方法列表: 还是泛型创建Map: public static <K, V> HashMap<K, V> newHashMap() { return new HashM ...
- Guava包学习--Hash
我们HashMap会有一个rehash的过程,为什么呢?因为java内建的散列码被限制为32位,而且没有分离散列算法和所作用的数据,所以替代算法比较难做.我们使用HashMap的时候它自身有一个reh ...
- Guava包学习---I/O
Guava的I/O平时使用不太多,目前项目原因导致基本上只有在自己写一些文本处理小工具才用得到.但是I/O始终是程序猿最常遇到的需求和面试必问的知识点之一.同时Guava的I/O主要面向是时JDK5和 ...
- Guava包学习---Bimap
Bimap也是Guava中提供的新集合类,别名叫做双向map,就是key->value,value->key,也就是你可以通过key定位value,也可以用value定位key. 这个场景 ...
- Guava包学习-Multimap
它和上一章的MultiSet的继承结果很相似,只不过在上层的接口是Multimap不是Multiset. Multimap的特点其实就是可以包含有几个重复Key的value,你可以put进入多个不同v ...
- Guava包学习--Table
Table,顾名思义,就好像HTML中的Table元素一样,其实就是行+列去确定的值,更准确的比喻其实就是一个二维矩阵. 其实它就是通过行+列两个key去找到一个value,然后它又containsv ...
随机推荐
- Python——基本的方法(2)
Python内置了很多有用的函数,我们可以直接调用. 要调用一个函数,需要知道函数的名称和参数,可以直接从Python的官方网站查看文档. 常见的方法: 绝对值方法abs(-100),得到100: 最 ...
- Mysql与InnoDB优化
Mysql可以从以下几个方面进行数据库优化: SQL及索引优化: sql优化: 优化count select count(*) ,count(id) from t; #count(*) 会包含null ...
- g2o error2
./pose_estimation_3d2d: error while loading shared libraries: libg2o_core.so: cannot open shared obj ...
- layui登录后token问题
layui是一个非常简单且实用的后台管理系统搭建框架,里面的插件丰富使用简单,只需要在原有基础上进行修改即可,但是在数据处理方面略显薄弱,内置的jquery在实际过程中略显不足,若是能添加内置的mvc ...
- 6 springboot Docker 部署
安装请参考其他的教程请参考http://www.runoob.com/docker/centos-docker-install.htm 拉取zookeeper镜像 docker pull zookee ...
- bnu 10805 矩形神码的 平面向量的运行
矩形神码的 Time Limit: 1000ms Memory Limit: 65536KB Special Judge 64-bit integer IO format: %lld J ...
- Struts2 (三) — OGNL与值栈
一.OGNL表达式 1.概述 1.1什么是OGNL OGNL是Object-Graph Navigation Language的缩写,俗称对象图导航语言. 它是一种功能强大的表达式语言,通过它简单 ...
- OpenStack IceHouse 部署 - 4 - 计算节点部署
Nova计算服务(计算节点) 参考 本页内容依照官方安装文档进行,具体参见Configure a compute node(nova service) 前置工作 数据库 由于我们在Nova(计算管理 ...
- c++开源日志log4cplus使用开发文档
下载地址:http://files.cnblogs.com/files/lizhigang/LOG4CPLUS%E5%BC%80%E5%8F%91%E4%B8%8E%E4%BD%BF%E7%94%A8 ...
- unity对象物体闪烁
Material mat; // float a=0; //控制闪烁速度 float speed=0.6f; float b=1; Update(){ if(a>0.3f){ b=1; } if ...