CollectionUtils利用Predicate实现Collection的快速操作

Predicate是一个接口,用于实现一个evaluate的方法,并返回是否符合evaluate条件的值,多用于函数式编程中的快捷操作,CollectionUtils中有大量包含一个Predicate参数的方法,可以实现collection的快捷操作,这里对常用的几个方法做一个简单说明

public class Test {
    public static void main(String args[]){
        List<Integer> testList=new ArrayList<Integer>(){
            {
                add(1);
                add(2);
                add(3);
            }
        };

        //返回testlist且只包含数据大于2
        CollectionUtils.filter(testList, itm->{
            return Integer.valueOf(itm.toString())>2;
        });

        //返回testlist符合条件的个数
//        int resultMatchNum=CollectionUtils.countMatches(testList,itm->{
//            return Integer.valueOf(itm.toString())>1;
//        });
        
          //判断testlist是否包含符合条件的元素
//        boolean resultExists=CollectionUtils.exists(testList, itm->{
//            return Integer.valueOf(itm.toString())>2;
//        });

        //返回testlist中符合条件的数据,注意只返回一条
//        Object reusltFindObject=CollectionUtils.find(testList, itm->{
//            return Integer.valueOf(itm.toString())>1;
//            //返回唯一一个值
//        });
        
        //返回符合条件的collection
//        Collection resultSelect=CollectionUtils.select(testList, itm->{
//            return Integer.valueOf(itm.toString())>1;
//        });
            
        //和select结果相反,返回不符合条件的collection
//        Collection resultSelectReject=CollectionUtils.selectRejected(testList, itm->{
//            return Integer.valueOf(itm.toString())>1;
//        });

        //这个比较难理解,如果后面predicate都满足,则返回testlist,官方文档为Returns a predicated (validating) collection backed by the given collection.
//        Collection resultCollect=CollectionUtils.predicatedCollection(testList, itm->{
//            //Returns a predicated (validating) collection backed by the given collection.
//            return Integer.valueOf(itm.toString())>0;
//        });
        
    }
}

 

当然常用的predicate也可以通过PredicateUtils构造出来,比如

//返回为null的collection数据
CollectionUtils.filter(testList, PredicateUtils.nullPredicate());

 

 

参考文档 http://blog.csdn.net/scgaliguodong123_/article/details/45874503

 

 

 

转载于:https://my.oschina.net/zimingforever/blog/1615870

MyBatis集成Predicate使用XML方式实现分页,我们可以使用MyBatis提供的分页插件PageHelper,它可以帮助我们自动进行分页处理,简化了分页的实现过程。 在使用PageHelper之前,我们需要在MyBatis的配置文件中配置分页插件,示例代码如下: ``` <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="helperDialect" value="mysql"/> </plugin> </plugins> ``` 在上述代码中,我们使用PageInterceptor作为分页插件,并指定数据库为MySQL。 然后,在Mapper接口中,我们定义一个方法来接收Predicate对象和分页参数,并将其作为参数传递给XML中定义的SQL语句。示例代码如下: ``` public interface UserMapper { List<User> findUserByPredicate( @Param("predicate") Predicate<User> predicate, @Param("page") PageRequest pageRequest); } ``` 在上述代码中,我们使用PageRequest类来封装分页参数,包括页码和每页记录数。然后,在XML中,我们可以使用PageHelper提供的语法来进行分页查询,示例代码如下: ``` <select id="findUserByPredicate" resultMap="userResultMap"> SELECT * FROM user <where> <if test="predicate != null"> AND <foreach collection="predicate.conditions" item="condition" separator=" AND "> ${condition.property} ${condition.operator} #{condition.value} </foreach> </if> </where> <if test="page != null"> ORDER BY id LIMIT #{page.offset}, #{page.pageSize} </if> </select> ``` 在上述代码中,我们使用if标签来判断传入的分页参数是否为null,如果不为null,则使用LIMIT子句来进行分页查询。其中,#{page.offset}表示查询偏移量,#{page.pageSize}表示每页记录数。 最后,在调用Mapper接口方法时,我们可以传入Predicate对象和分页参数,示例代码如下: ``` Predicate<User> predicate = ...; PageRequest pageRequest = new PageRequest(1, 10); List<User> userList = userMapper.findUserByPredicate(predicate, pageRequest); ``` 在上述代码中,我们使用PageRequest类来构造分页参数,并将其传递给Mapper接口方法。然后,PageHelper会自动进行分页处理,返回符合条件的记录列表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值