美文网首页
guava函数式编程(过滤,转换,约束)与集合

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

作者: VikingOldYoung | 来源:发表于2016-11-05 11:50 被阅读0次

函数式编程

  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");
        
    }

相关文章

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

    函数式编程 过滤Collections.filter(原内容,过滤规则);返回一个新的list 转换Collect...

  • Guava使用

    Guava中针对集合的 filter和过滤功能 缓存 测试 配置 缓存使用 测试 转换器:利用 Collectio...

  • iOS-链式编程

    函数式编程------->链式编程 函数式编程: 调用方式 我们最终目的是将函数式编程转换为链式编程:首先将调用方...

  • javascript函数式编程

    参考书籍:《javascript 函数式编程》 什么是函数式编程? 函数式编程通过函数将值转换为抽象单元,接着用于...

  • Guava函数式编程

    函数式编程 截至JDK7,Java中也只能通过笨拙冗长的匿名类来达到近似函数式编程的效果。预计JDK8中会有所改变...

  • 《Kotlin入门实战》CH5 | 函数与函数式编程

    函数与函数式编程 函数式编程与命令式编程最大的不同是:函数式编程的焦点在于数据的映射,命令式编程(imperati...

  • Scala编程详解14:函数式编程之集合操作

    大纲1、Scala的集合体系结构2、List3、LinkedList4、Set5、集合的函数式编程6、函数式编程综...

  • Guava函数式编程(1)

    本章节主要介绍以下几个接口和类: Function和Functions Predicate和Predicates ...

  • Guava函数式编程(2)

    使用Predicate接口 可以说Predicate接口和Function接口是具有相似的功能,Predicate...

  • Guava函数式编程(3)

    使用Supplier接口 Supplier接口只要一个方法: 非常简单的一个定义,简而言之,得到一个对象。但它有什...

网友评论

      本文标题:guava函数式编程(过滤,转换,约束)与集合

      本文链接:https://www.haomeiwen.com/subject/jczwuttx.html