不可变集合包括ImmutableList、ImmutableMap、ImmutableSet等。
@Test
public void testList() {
ImmutableList list = ImmutableList.of("A","B","C");
for (String o : list) {
System.out.println(o);
}
}
在 JDK 中 Map 中的一个键对应一个值,在 put 一个键值对时,如果键重复,则会覆盖掉原来的值。如果现在有这样一个需求:模拟通讯录操作,一个联系人可能有多个电话号码。如果这个时候使用 JDK 中的 Map 就有点麻烦了,在这种情况下,使用 Guava 的 Multimap 可以很好地解决。
/**
* 多值 map:
* key:HashMap.
* value:ArrayList
*/
@Test
public void testMultiMap() {
Multimap map = ArrayListMultimap.create();
map.put("Tom","15082383272");
map.put("Jack","15082383273");
map.put("Tom","5590113");
map.put("Tom","0827-1334");
map.put("Lisi","151500199876");
map.put("zhangsan","13056505519");
for (String str : map.values()) {
System.out.println(str);
}
//获取 key = "Tom" 的值(集合)
System.out.println(map.get("Tom"));
//移除某个键值中的一个元素
map.remove("Tom","15082383272");
System.out.println(map.get("Tom"));
//移除某个键相关联的所有值的集合
map.removeAll("Tom");
System.out.println(map.get("Tom").size());
}
table 表有点像二维数组,可以根据行和列定位一组数据,只不过这里的行和列类型不仅仅是 int ,还可以是其他类型。
/**
* 类似于二维数组,(行,列,值)
*/
@Test
public void testTable() {
Table table = HashBasedTable.create();
table.put("ZhangSan","Java",98);
table.put("ZhangSan","MySQL",99);
table.put("Mike","C",95);
System.out.println();
//循环输出每个单元格内容
Set.Cell> cells = table.cellSet();
for (Table.Cell cell : cells) {
System.out.println(cell.getRowKey() + "," + cell.getColumnKey() + "," + cell.getValue());
}
System.out.println();
System.out.println(table);
//获取行为 "ZhangSan" 的数据(集合)
System.out.println(table.row("ZhangSan"));
//获取行的集合
System.out.println(table.rowKeySet());
//获取列的集合
System.out.println(table.columnKeySet());
//获取单元格值
System.out.println(table.values());
}
@Test
public void testListFilter() {
List list = Arrays.asList("www.baidu.com","www.google.com","www.pzhu.edu.cn");
//筛选滤以 "com" 结尾的字符串
Collection filterList = Collections2.filter(list,(str) -> {
return str.endsWith("com");
});
for (String s : filterList) {
System.out.println(s);
}
}
/**
* 获取两个字符串相同的前缀和后缀
*/
@Test
public void testPrefixAndSuffix() {
String s1 = "cn.edu.pzhu.bean";
String s2 = "cn.edu.pzhu.dao";
//相同的前缀
String prefix = Strings.commonPrefix(s1,s2);
System.out.println("s1,s2 common prefix is " + prefix);
String s3 = "www.baidu.com";
String s4 = "www.google.com";
//相同的后缀
String suffix = Strings.commonSuffix(s3,s4);
System.out.println("s3,s4 common suffix is " + suffix);
}
判断一个字符串是否为空:
@Test
public void testString() {
String s1 = "";
String s2 = " ";
String s3 = null;
System.out.println(Strings.isNullOrEmpty(s1));//true
System.out.println(Strings.isNullOrEmpty(s2));//false
System.out.println(Strings.isNullOrEmpty(s3));//true
}
基本类型工具在 primitives 包中,是以基本类型名 +s 的方式命名的,比如 Ints 是 int 的工具类,Doubles 是 double 的工具类,这些都是针对基本类型的而不是针对包装类型。
@Test
public void testIns() {
int arr[] = {1,2,6,7,8,6,2};
//获取最大值
System.out.println("Max:" + Ints.max(arr));
//翻转元素
Ints.reverse(arr);
for (int i : arr) {
System.out.print(i + " ");
}
System.out.println("");
//降序排列
Ints.sortDescending(arr);
for (int i : arr) {
System.out.print(i + " ");
}
//将包装类集合转化为基本类型数组
List list = new ArrayList<>();
arr = Ints.toArray(list);
}