Predicate和Consumer接口是集合操作中常用到的俩个接口:

1、Predicate

public interface Predicate<T>{
boolean test(T t);//进行某些逻辑判断并返回一个boolean值
}

2、Consumer

public interface Consumer<T>{
void accept(T t);//对于给定的对象进行操作
}

3、结合实例

  • predicate
 Predicate<Integer> pre = x -> x >= 5;
System.out.println(pre.test(9));//输出结果为true
  • consumer
 import java.util.Collection;
import java.util.HashSet;
import java.util.function.Predicate;
import java.util.function.Consumer; public class PreCon {
public static void main(String []args){
Collection<String> c=new HashSet<String>();
c.add("!");
c.add("java");
c.add("hao");
c.add("ni");
c.add("zhe");
System.out.println(findSet(c,obj->System.out.println(obj),obj->((String)obj).length()<3)); //使用Lambda表达式(目标类型是Predicate)过滤集合
c.removeIf(ele -> ((String)ele).length() < 10);
System.out.println(c);
} public static int findSet(Collection collection, Consumer consumer,
Predicate predicate) {
int n = 0;
for (Object obj : collection) {
if (predicate.test(obj)) {
n++;
consumer.accept(obj);
}
}
return n;
}
}

 

 

Predicate接口和Consumer接口的更多相关文章

  1. Spring8中lambda表达式的学习(Function接口、BiFunction接口、Consumer接口)

    代码重构,为了确保功能的等效性,梳理代码时,发现如下代码: public SingleRespTTO fundI(SingleReqTTO request) throws Exception { re ...

  2. Java 8 : Predicate和Consumer接口

    1.consumer jdk 1.8 的 Iterable 接口中的 forEach 默认方法: public interface Iterable<T> { default void f ...

  3. Predicate和Consumer接口的使用

    //  Predicate   判断是否拥有资格,Consumer  改变输入的值 案例 public static MyTest2 getV(MyTest2 a, Predicate<MyTe ...

  4. Kafka Consumer接口

    对于kafka的consumer接口,提供两种版本,   high-level 一种high-level版本,比较简单不用关心offset, 会自动的读zookeeper中该Consumer grou ...

  5. Java常用函数式接口--Consumer接口andThen()方法使用案例(二)

    Java常用函数式接口--Consumer接口使用案例

  6. 常用的函数式接口Consumer接口练习字符串拼接输出

    题目 下面的字符串数组当中有多条信息,请按照格式"姓名: XX 性别: XX"的格式将信息打印出来,要求将 打印姓名的动作为第一个Consumer接口的Lambda实例,将打印性别 ...

  7. Callable接口、Runable接口、Future接口

    1. Callable与Runable区别 Java从发布的第一个版本开始就可以很方便地编写多线程的应用程序,并在设计中引入异步处理.Thread类.Runnable接口和Java内存管理模型使得多线 ...

  8. 源码阅读—Iterator接口和LIstIterator接口

    在继续看ArrayList源码之前,先了解Iterator接口和ListIterator接口,下篇文章详细讲解ArrayList是如何实现它们的. 我们知道,接口只是一种规范,当继承接口并实现其中的方 ...

  9. Java8函数式接口/Lambda表达式/接口默认方法/接口静态方法/接口冲突方法重写/lambda表达式指定泛型类型等

    一:函数式接口 1.函数式接口的概念就是此接口必须有且只能有一个抽象方法,可以通过@FunctionalInterface来显示规定(类似@Override),但是没有此注解的但是只有一个抽象方法的接 ...

随机推荐

  1. Datatypes translation between Oracle and SQL Server

    Datatypes translation between Oracle and SQL Server part 1: character, binary strings Datatypes tran ...

  2. c# 用户名 密码 访问 局域网共享

    #region Ping 返回true则代表可以连接成功 public bool Ping(string remoteHost) { bool Flag = false; Process proc = ...

  3. 5.3监听请求:使用eclipse的tcp/ip工具(端口转换)

    1.改用wsdl文件生成响应文件 运行浏览器输入发布的地址,获得wsdl源码保存在项目路径下, 2.创建接口转换器,window-property-tcpip 客户端执行结果:

  4. python利用redis构成一个队列

    例子在 http://peter-hoffmann.com/2012/python-simple-queue-redis-queue.html 英文 http://www.django-china.c ...

  5. 搭建基于Jenkins salt-api的运维工具

    1. 安装salt-master和salt-minion 安装过程不再赘述,请参考http://docs.saltstack.com/en/latest/topics/installation/ind ...

  6. Bash:-set设置位置变量结合while和shift使用

    #!/bin/bash aaa=( ) bbb=(a b c d) set -- ${aaa[@]} ));do } shift done set -- echo "------------ ...

  7. Scala学习资源

    Scala学习资源: Scala官方网站:http://www.scala-lang.org/ Scala github:https://github.com/scala/scala Twitter ...

  8. lua 和 c/c++ 交互 (持续更新)

    参考: http://blog.csdn.net/xiaohuh421/article/details/7476485 http://blog.csdn.net/shun_fzll/article/d ...

  9. maven+spark2.0.0最大连通分量

    运用到了spark2.0.0的grarhx包,要手动的在pom.xml里面添加依赖包,要什么就在里面添加依赖,然后在run->maven install

  10. 死性不改ISO9000系列系统 2011q4~2016q1

    百度云链接: http://pan.baidu.com/s/1o8rO3W2 密码: kabg 2016q2由于发布没多久,可能会重新修改,暂时不转载.