先导入Maven依赖:上面的库任选一个即可
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
或者
<!-- https://mvnrepository.com/artifact/com.google.collections/google-collections -->
<dependency>
<groupId>com.google.collections</groupId>
<artifactId>google-collections</artifactId>
<version>1.0</version>
</dependency>
测试示例:
@org.junit.Test
public void testGoogleCollections()
{
// 所有的工具类命名与JDK的类似带有s的后缀
// 创建一个默认的List
List<String> list = Lists.newArrayList();
// 创建一个带有元素的List
List<String> list2 = Lists.newArrayList("a", "b");
// 创建一个带有容量的List
List<String> list3 = Lists.newArrayListWithCapacity(16);
// 还可以创建一些并发包下的集合,如:
List<String> list4 = Lists.newCopyOnWriteArrayList();
// Set、Map、Queue等等都是类似的创建方法
// 补充:更有许多有用的类,如Range类,参考博客:https://blog.csdn.net/thewaiting/article/details/78303258
Range<Integer> range = Range.open(1, 4); //左右开区间
System.out.print("[ ");
for (int grade : ContiguousSet.create(range, DiscreteDomain.integers()))
{
System.out.print(grade + " ");
}
System.out.println("]");
}