Guava 学习

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);
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Guava简单介绍 1 资料链接 极客学院http://wiki.jikexueyuan.com/project/...
    田园小丁阅读 1,944评论 0 3
  • 本文是对 Guava 中 Splitter 的学习介绍。欢迎加入学习项目: LearningGuava。 使用示例...
    天未的博客阅读 17,941评论 0 6
  • Guava学习笔记:Optional优雅的使用null 在我们学习和使用Guava的Optional之前,我们需要...
    it_zzy阅读 514评论 0 0
  • 通过使用guava库,让代码简洁易扩展。 1、条件检查 业务代码书写过程中,各种判空和参数检查是不可避免的,重复繁...
    hhfchyl阅读 344评论 0 0
  • Guava学习笔记之Joiner ,Strings,Splitter 工具实例 一.Strings 二,Joine...
    wanggs阅读 548评论 0 1