guava函数式编程(过滤,转换,约束)与集合

函数式编程

  1. 过滤
    Collections.filter(原内容,过滤规则);返回一个新的list
/**
     * 过滤,找出回文
     */
    public static void test02() {
        List<String> list = Lists.newArrayList("big", "mom", "hoh", "dad", "refer", "Viking");
        Collection<String> list2 = Collections2.filter(list, new Predicate<String>() {

            // 匿名内部类,只使用一次,因为没有对象名
            public boolean apply(String input) {
                // 这里不满足要求就会被过滤出去
                return new StringBuilder(input).reverse().toString().equals(input);
            }
        });

        for (String temp : list2) {
            System.out.println(temp);
        }
    }
  1. 转换
    Collections2.transform();
public static void main(String[] args) {
        //类型转换,function的意思是函数
        List<Long> longtime=new ArrayList<>();
        longtime.add(100000000L);
        longtime.add(999999999999999L);
        longtime.add(200000000L);
        
        Collection<String> timeStrCol=Collections2.transform(longtime, new Function<Long, String>() {

            public String apply(Long input) {
                
                return new SimpleDateFormat("yyyy-MM-dd").format(input);
            }
        });
        
        for(String temp:timeStrCol){
            System.out.println(temp);
        }
    }

函数嵌套使用

public static void main(String[] args) {
        //组合式函数编程
        //确保容器内字符串的长度不超过5,超过得部分截断,然后全转换成大写。
        List<String> list=Lists.newArrayList("hello","happiness","Viking","iloveu");
        
        Function<String, String> f1=new Function<String, String>() {
            //f1判断是否长度是否大于5,并采取截断操作
            public String apply(String input) {
                return input.length()>5?input.substring(0, 5):input;
            }
        };
        
        Function<String, String> f2=new Function<String, String>() {
            //f2将字符串转成大写的
            public String apply(String input) {
                return input.toUpperCase();
            }
        };
        
        //组合函数
        Function<String, String> f=Functions.compose(f1, f2);
        
        Collection<String> col=Collections2.transform(list, f);
        
        for(String temp:col){
            System.out.println(temp);
        }
    }
  1. 约束
    似乎guava里面没有这个Constraint这个类或者借口了

集合的操作

  • 交集
    sets.intersection();
  • 差集
    sets.difference();
  • 并集
    sets.union();
public static void main(String[] args) {
        //集合操作
        Set<Integer> set1=Sets.newHashSet(1,2,3,4,5,6);
        Set<Integer> set2=Sets.newHashSet(4,5,6,7,8,9);
        
        Set<Integer> intersectionSet=Sets.intersection(set1, set2);
        System.out.println("交集:");
        for(Integer i:intersectionSet)
            System.out.print(i+"\t");
        
        Set<Integer> differenceSet=Sets.difference(set1, set2);
        System.out.println("\n"+"差集:");
        for(Integer i:differenceSet)
            System.out.print(i+"\t");
        
        Set<Integer> unionSet=Sets.union(set1, set2);
        System.out.println("\n"+"并集:");
        for(Integer i:unionSet)
            System.out.print(i+"\t");
        
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • 一、 1、请用Java写一个冒泡排序方法 【参考答案】 public static void Bubble(int...
    独云阅读 1,421评论 0 6
  • 多态 任何域的访问操作都将有编译器解析,如果某个方法是静态的,它的行为就不具有多态性 java默认对象的销毁顺序与...
    yueyue_projects阅读 994评论 0 1
  • 【程序1】 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔...
    叶总韩阅读 5,166评论 0 41
  • 很久了 我还在想象 不,那是回忆 我还想要和你走在暖阳下 还想再多看一眼你眼里的我 那个蝉鸣的夏季 我遇见了最美的...
    里小木阅读 104评论 0 1