Guava学习笔记(三):集合
添加Maven依赖
ListsTest
import com.google.common.collect.Lists;
import org.hamcrest.core.Is;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class ListsTest {
@Test
public void test_new_normal() {
ArrayList<String> objects = Lists.newArrayList("a", "b", "c");
List<List<String>> partition = Lists.partition(objects, 2);
Assert.assertEquals(2, partition.size());
}
@Test
public void test_reverse() {
ArrayList<String> strings = Lists.newArrayList("a", "b", "c");
ArrayList<String> reverse = Lists.newArrayList("c", "b", "a");
Assert.assertThat(Lists.reverse(strings), Is.is(reverse));
}
@Test(expected = AssertionError.class)
public void test_partition_ex() {
ArrayList<String> objects = Lists.newArrayList("a");
List<List<String>> partition = Lists.partition(objects, 2);
Assert.assertEquals(2, partition.size());
}
}
MapsTest
import com.google.common.collect.*;
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
public class MapsTest {
@Test
public void test_asMap() {
Map<Integer, Integer> map = Maps.asMap(Sets.newHashSet(1, 2, 3), i -> i == null ? 0 : i * i);
Assert.assertThat(map, is(Map.of(1, 1, 2, 4, 3, 9)));
}
/**
* ArrayListMultimap:接口Multimap的实现,它使用ArrayList存储给定键的值。 HashMap将每个键与值的ArrayList相关联。
* 迭代此类提供的集合时,给定键的值的排序与添加值的顺序一致。
* 此多图允许重复的键值对。 添加等于现有键值对的新键值对后,ArrayListMultimap将包含新值和旧值的条目。
* 键和值可以为null。 支持所有可选的multimap方法,并且所有返回的视图都是可修改的。
* get,removeAll和replaceValues返回的列表都实现了java.util.RandomAccess。
* 当任何并发操作更新multimap时,此类不是线程安全的。
* 并发读取操作将正常工作。 要允许并发更新操作,请通过调用Multimaps.synchronizedListMultimap来包装。
*/
@Test
public void test_arrayListMultiMap() {
ArrayListMultimap<String, String> multiMap = ArrayListMultimap.create();
multiMap.put("Foo", "1");
multiMap.put("Foo", "2");
multiMap.put("Foo", "3");
multiMap.put("Foo", "3");
List<String> expected = Lists.newArrayList("1", "2", "3", "3");
assertEquals(multiMap.get("Foo"), expected);
}
/**
* 使用哈希表实现Multimap。
* 多图不存储重复的键值对。 添加等于现有键值对的新键值对无效。
* 键和值可以为null。 支持所有可选的multimap方法,并且所有返回的视图都是可修改的。
* 当任何并发操作更新multimap时,此类不是线程安全的。 并发读取操作将正常工作。
* 要允许并发更新操作,请通过调用Multimaps.synchronizedSetMultimap来包装。
*/
@Test
public void test_hashMultiMap() {
HashMultimap<String, String> multiMap = HashMultimap.create();
multiMap.put("Foo", "1");
multiMap.put("Foo", "3");
multiMap.put("Foo", "2");
multiMap.put("Foo", "3");
Set<String> expected = Sets.newHashSet("1", "2", "3");
assertEquals(multiMap.get("Foo"), expected);
}
@Test(expected = IllegalArgumentException.class)
public void test_biMap() {
BiMap<String, String> biMap = HashBiMap.create();
biMap.put("1", "Tom");
biMap.put("2", "Tom");
}
/**
* forcePut:另一种put形式,它在继续执行put操作之前以静默方式删除任何具有值的现有条目。
* 如果bimap以前包含提供的键值映射,则此方法无效。
* 警告:如果删除具有此值的现有条目,则会丢弃该条目的键,并且不会返回该键。
*/
@Test
public void test_biMap_forcePut() {
BiMap<String, String> biMap = HashBiMap.create();
biMap.forcePut("1", "Tom");
biMap.forcePut("2", "Tom");
assertTrue(biMap.containsKey("2"));
assertFalse(biMap.containsKey("1"));
}
/**
* 返回此bimap的反向视图,该视图将每个bimap的值映射到其关联的键。
* 两个bimaps由相同的数据支持; 对一个的任何更改都会出现在另一个中。
*/
@Test
public void test_biMap_reverse() {
BiMap<String, String> biMap = HashBiMap.create();
biMap.put("1", "Tom");
biMap.put("2", "Harry");
assertThat(biMap.get("1"), is("Tom"));
assertThat(biMap.get("2"), is("Harry"));
BiMap<String, String> inverseMap = biMap.inverse();
assertThat(inverseMap.get("Tom"), is("1"));
assertThat(inverseMap.get("Harry"), is("2"));
}
@Test
public void test_multimap_builder() {
Multimap<Integer, String> map = new ImmutableListMultimap.Builder<Integer, String>().put(1, "Foo").putAll(2, "Foo", "Bar", "Baz").putAll(4, "Huey", "Duey", "Luey").put(3, "Single").build();
ArrayListMultimap<Integer, String> multimap = ArrayListMultimap.create();
multimap.put(1, "Foo");
multimap.putAll(2, Arrays.asList("Foo", "Bar", "Baz"));
multimap.put(3, "Single");
multimap.putAll(4, Arrays.asList("Huey", "Duey", "Luey"));
Assert.assertThat(map, is(multimap));
}
}
SetsTest
import com.google.common.collect.Sets;
import org.junit.Assert;
import org.junit.Test;
import java.util.Set;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
public class SetsTest {
/**
* 返回两组差异的不可修改视图。 返回的集合包含set1包含但未包含在set2中的所有元素。 而set2包含set1却不存在的元素都会被忽略。
* 返回集的迭代顺序与set1的迭代顺序匹配。
*/
@Test
public void test_difference() {
Set<String> s1 = Sets.newHashSet("1", "2", "3");
Set<String> s2 = Sets.newHashSet("2", "3", "4");
Sets.SetView<String> difference = Sets.difference(s1, s2);
Assert.assertTrue(difference.contains("1"));
Assert.assertEquals(1, difference.size());
}
/**
* 返回的集合包含set1或set2中包含但不同时包含在两者中的所有元素。
* 返回集的迭代顺序未定义。
*/
@Test
public void test_symmetricDifference() {
Set<String> s1 = Sets.newHashSet("1", "2", "3");
Set<String> s2 = Sets.newHashSet("2", "3", "4");
Sets.SetView setView = Sets.symmetricDifference(s1, s2);
//Would return [1,4]
Assert.assertEquals(2, setView.size());
Assert.assertTrue(setView.contains("1"));
Assert.assertTrue(setView.contains("4"));
}
/**
* 返回两个集合的交集。 返回集的迭代顺序与set1的迭代顺序匹配。
*/
@Test
public void test_intersection() {
Set<String> s1 = Sets.newHashSet("1", "2", "3");
Set<String> s2 = Sets.newHashSet("3", "2", "4");
Sets.SetView<String> sv = Sets.intersection(s1, s2);
assertThat(sv.size() == 2 && sv.contains("2") && sv.contains("3"), is(true));
}
/**
* 返回两个集合的合集,返回集的迭代顺序与set1的迭代顺序匹配,剩余的与set2的迭代顺序匹配。
*/
@Test
public void test_union() {
Set<String> s1 = Sets.newHashSet("1", "2", "3");
Set<String> s2 = Sets.newHashSet("3", "2", "4");
Sets.SetView<String> sv = Sets.union(s1, s2);
assertThat(sv.size() == 4 && sv.contains("2") && sv.contains("3") && sv.contains("4") && sv.contains("1"), is(true));
}
@Test
public void test_filter() {
Set<Integer> s1 = Sets.newHashSet(1, 4, 2, 6);
Set<Integer> filter = Sets.filter(s1, input -> input != null && input > 2);
assertThat(filter.size() == 2 && filter.contains(4) && filter.contains(6), is(true));
}
}
Guava学习笔记(三):集合的更多相关文章
- [Guava学习笔记]Collections: 集合工具类
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3861431.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- Oracle学习笔记三 SQL命令
SQL简介 SQL 支持下列类别的命令: 1.数据定义语言(DDL) 2.数据操纵语言(DML) 3.事务控制语言(TCL) 4.数据控制语言(DCL)
- Guava学习笔记目录
Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libra ...
- guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用
guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用 1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection ...
- 学习笔记(三)--->《Java 8编程官方参考教程(第9版).pdf》:第十章到十二章学习笔记
回到顶部 注:本文声明事项. 本博文整理者:刘军 本博文出自于: <Java8 编程官方参考教程>一书 声明:1:转载请标注出处.本文不得作为商业活动.若有违本之,则本人不负法律责任.违法 ...
- angular学习笔记(三十)-指令(7)-compile和link(1)
这篇主要讲解指令中的compile,以及它和link的微妙的关系. link函数在之前已经讲过了,而compile函数,它和link函数是不能共存的,如果定义了compile属性又定义link属性,那 ...
- angular学习笔记(三十)-指令(5)-link
这篇主要介绍angular指令中的link属性: link:function(scope,iEle,iAttrs,ctrl,linker){ .... } link属性值为一个函数,这个函数有五个参数 ...
- Java学习笔记之---集合
Java学习笔记之---集合 (一)集合框架的体系结构 (二)List(列表) (1)特性 1.List中的元素是有序并且可以重复的,成为序列 2.List可以精确的控制每个元素的插入位置,并且可以删 ...
- 软件测试之loadrunner学习笔记-02集合点
loadrunner学习笔记-02集合点 集合点函数可以帮助我们生成有效可控的并发操作.虽然在Controller中多用户负载的Vuser是一起开始运行脚本的,但是由于计算机的串行处理机制,脚本的运行 ...
- guava 学习笔记 使用瓜娃(guava)的选择和预判断使代码变得简洁
guava 学习笔记 使用瓜娃(guava)的选择和预判断使代码变得简洁 1,本文翻译自 http://eclipsesource.com/blogs/2012/06/06/cleaner-code- ...
随机推荐
- 潭州课堂25班:Ph201805201 第九课 函数作用域和匿名函数 (课堂笔记)
匿名函数: lambda obj:str(obj).isdigit 语法规则: lambda 参数:表达式 列: ma1 = map( lambda obj:'binbin','abcdef' ) ...
- Android开发中遇到的问题(二)——新建android工程的时候eclipse没有生成MainActivity和layout布局
一.新建android工程的时候eclipse没有生成MainActivity和layout布局 最近由于工作上的原因,开始学习Android开发,在入门的时候就遇到了不少的坑,遇到的第一个坑就是&q ...
- Gitlab用户在组中有五种权限:Guest、Reporter、Developer、Master、Owner
Gitlab权限管理Gitlab用户在组中有五种权限:Guest.Reporter.Developer.Master.Owner Guest:可以创建issue.发表评论,不能读写版本库Reporte ...
- SSH框架搭建详细图文教程(转)
这篇文章看的我醍醐灌顶的感觉,比之前本科时候学习的SSH架构 要清晰数倍 非常感觉这篇博主的文章 文章链接为:http://blog.sina.com.cn/s/blog_a6a6b3cd01017 ...
- PHP中日志相关处理
内置函数: 1.error_log() ,第三个参数不能是绝对路径,必须是相对路径.写入文件: error_log("warn:\nthis is a warn!\n",3,&qu ...
- VMWare虚拟机中CPU过高的问题
在VMWare中按默认方式创建的虚拟机,安装的Windows Server 2016 x64操作系统.可打开一个稍微大一点的程序CPU就飙到90%以上,自然整个系统操作起来很卡. 在VMWare中看到 ...
- 调用 setState 之后发生了什么?
(1)代码中调用 setState 函数之后,React 会将传入的参数对象与组件当前的状态合并,然后触发所谓的调和过程(Reconciliation).(2)经过调和过程,React 会以相对高效的 ...
- DataGuard---->物理StandBy的角色切换之switchover
Switchover,无损切换,通常是用户手动触发或者有计划地让其自动触发,如硬件升级等. 步骤: 1.Primary数据库转换为StandBy角色 2.StandBy数据库(之一)转换为Primar ...
- 单片机成长之路(51基础篇) - 017 C51中data,idata,xdata,pdata的区别(转)
从数据存储类型来说,8051系列有片内.片外程序存储器,片内.片外数据存储器,片内程序存储器还分直接寻址区和间接寻址类型,分别对应code.data.xdata.idata以及根据51系列特点而设定的 ...
- FeignClient使用
在使用Spring Cloud开发微服务应用时中,各个微服务服务提供者都是以HTTP接口的形式对外提供服务,因此服务消费者在调用服务提供者时,通过HTTP Client的方式访问.当然我们可以使用JD ...