加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 仅当谓词输入不是使用Guava的空字符串时才过滤对象

发布时间:2020-12-15 02:15:54 所属栏目:Java 来源:网络整理
导读:我现在正在学习番石榴,我有一个问题.我有三个可能的字符串过滤器.问题是我只想在字符串不是“”(空字符串)时过滤对象集合.我的另一个问题是如何从对象中过滤不同的成员,如object.getName()== firstStringFilter.如果有人知道如何做到这一点,我会非常感激. 解
我现在正在学习番石榴,我有一个问题.我有三个可能的字符串过滤器.问题是我只想在字符串不是“”(空字符串)时过滤对象集合.我的另一个问题是如何从对象中过滤不同的成员,如object.getName()== firstStringFilter.如果有人知道如何做到这一点,我会非常感激.

这是我最终用我的代码做的.如果我的firstString过滤器不为空,则应用该过滤器,并与其他两个字符串过滤器相同.我正在使用相同的List并用过滤器的结果覆盖它.

List<Field> fieldList = mLand.getFields();

                    if(!filterPattern.equalsIgnoreCase(""))
                    {
                        Predicate predicate = new Predicate<Field>()
                        {
                            @Override
                            public boolean apply(Field input)
                            {
                                return input.getName()
                                        .toLowerCase()
                                        .contains(filterPattern);
                            }
                        };

                        Collection<Field> result = Collections2.filter(fieldList,predicate);

                        fieldList = new ArrayList<>(result);

                    }

                    if(!cropStringPattern.equalsIgnoreCase(""))
                    {
                        Predicate predicate = new Predicate<Field>()
                        {
                            @Override
                            public boolean apply(Field input)
                            {
                                return input.getCrop().getName()
                                        .toLowerCase()
                                        .contains(cropStringPattern);
                            }
                        };

                        Collection<Field> result = Collections2.filter(fieldList,predicate);

                        fieldList = new ArrayList<>(result);
                    }

                    if(!varietyStringPattern.equalsIgnoreCase(""))
                    {
                        Predicate predicate = new Predicate<Field>()
                        {
                            @Override
                            public boolean apply(Field input)
                            {
                                return input.getCrop().getVariety().getName()
                                        .toLowerCase()
                                        .contains(varietyStringPattern);
                            }
                        };

                        Collection<Field> result = Collections2.filter(fieldList,predicate);

                        fieldList = new ArrayList<>(result);
                    }

解决方法

它听起来不像你的对象是字符串,而是它们有一个字符串属性,所以这就像是

Iterables.filter(objects,new Predicate<MyObject>() {
   @Override public boolean apply(MyObject object) {
     return !object.getName().isEmpty(); // or whatever condition here
   }
 });

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读