3

I'm learning Guava right now and I have a problem. I have three possible string filters. The thing is that I only want to filter the object collection when the string is not "" (empty string). My other question is how can I filter different members from the object like object.getName() == firstStringFilter. If anyone know how to do this I would be truly appreciatted.

SOLUTION

This is what I finally did with my code. If my firstString filter is not empty, then apply that filter, and the same with the other two string filters. I'm using the same List and overwriting it with the results of the filters.

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);
                    }
Dan Ponce
  • 636
  • 1
  • 8
  • 24

2 Answers2

0

It doesn't sound like your objects are strings but rather they have a string property, so this would just be something like

 Iterables.filter(objects, new Predicate<MyObject>() {
   @Override public boolean apply(MyObject object) {
     return !object.getName().isEmpty(); // or whatever condition here
   }
 });
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
0

It's not entirely clear what you're asking, but it seems like you're trying to filter a collection by the result of a method that produces a string. If that's the case, you should be able to use something like this:

public static <T> Collection<T> filter(Collection<T> collection, String filter, Function<T, String> function) {
    // assumes static import of com.google.common.base.Predicates.*
    return Collections2.filter(collection, compose(not(equalTo(filter)), function));
}

Now you can call

filter(myCollection, "", new Function<MyClass, String>() {
    @Override
    public String apply(MyClass input) {
        return input.getName();
    }
)

and it will filter from the collections any objects with an empty name.

I'm going with the assumption that you're not using Java 8. If you are, you don't really need the predicate helpers. You can just call

Collections2.filter(myCollection, obj -> !obj.getName().isEmpty())
shmosel
  • 49,289
  • 6
  • 73
  • 138