guava Splitter 学习
@Test
public void Str2ListGuava(){
String str = "1,2,3,4,5,6,7,8,30,,";
// 用逗号切割并转为对象
List<String> list1 = Splitter.on(',').splitToList(str);
//去除空对象并转为其他类型
List<Integer> list2 = Splitter.on(',').omitEmptyStrings().splitToList(str).stream().map(Integer::parseInt).collect(Collectors.toList());
System.out.println("结果:" + list1);
System.out.println("结果:" + list2);
}
guava Sets 集合类取交集、差集、并集
public static void main(String[] args) {
List<Integer> list1 = Lists.newArrayList(1, 2, 3, 4, 5, 6);
List<Integer> list2 = Lists.newArrayList(3, 4, 5, 6, 7, 8, 9);
// 交集
System.out.println("交集为:");
Sets.SetView<Integer> intersection = Sets.intersection(Sets.newHashSet(list1), Sets.newHashSet(list2));
// 差集
System.out.println("差集为:");
Sets.SetView<Integer> diff = Sets.difference(Sets.newHashSet(list1), Sets.newHashSet(list2));
// 并集
System.out.println("并集为:");
Sets.SetView<Integer> union = Sets.union(Sets.newHashSet(list1), Sets.newHashSet(list2));
// 取并集并转为List
List<Integer> integers = Lists.newArrayList(union);
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。