Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

filtering an ArrayList using an object's field

Tags:

I have an ArrayList which is filled by Objects.

My object class called Article which has two fields ;

public class Article {      private int codeArt;     private String desArt;    public Article(int aInt, String string) {         this.desArt = string;         this.codeArt = aInt;     }      public int getCodeArt() {return codeArt; }     public void setCodeArt(int codeArt) {this.codeArt = codeArt;}     public String getDesArt() {return desArt;}     public void setDesArt(String desArt) { this.desArt = desArt;}  } 

I want to filter my List using the desArt field, and for test I used the String "test".

I used the Guava from google which allows me to filter an ArrayList.

this is the code I tried :

private List<gestionstock.Article> listArticles = new ArrayList<>();  //Here the I've filled my ArrayList  private List<gestionstock.Article> filteredList filteredList = Lists.newArrayList(Collections2.filter(listArticles, Predicates.containsPattern("test"))); 

but this code isn't working.

like image 797
Aimad Majdou Avatar asked May 31 '13 11:05

Aimad Majdou


People also ask

How do you filter data in an ArrayList?

ArrayList removeIf() method in Java The removeIf() method of ArrayList is used to remove all of the elements of this ArrayList that satisfies a given predicate filter which is passed as a parameter to the method.

How do you filter data in Java?

Java stream provides a method filter() to filter stream elements on the basis of given predicate. Suppose you want to get only even elements of your list then you can do this easily with the help of filter method. This method takes predicate as an argument and returns a stream of consisting of resulted elements.

How will you run a filter on a collection?

You can filter Java Collections like List, Set or Map in Java 8 by using the filter() method of the Stream class. You first need to obtain a stream from Collection by calling stream() method and then you can use the filter() method, which takes a Predicate as the only argument.


2 Answers

In Java 8, using filter

List<Article> articleList = new ArrayList<Article>(); List<Article> filteredArticleList= articleList.stream().filter(article -> article.getDesArt().contains("test")).collect(Collectors.toList()); 
like image 84
Free-Minded Avatar answered Nov 25 '22 10:11

Free-Minded


This is normal: Predicates.containsPattern() operates on CharSequences, which your gestionStock.Article object does not implement.

You need to write your own predicate:

public final class ArticleFilter     implements Predicate<gestionstock.Article> {     private final Pattern pattern;      public ArticleFilter(final String regex)     {         pattern = Pattern.compile(regex);     }      @Override     public boolean apply(final gestionstock.Article input)     {         return pattern.matcher(input.getDesArt()).find();     } } 

Then use:

 private List<gestionstock.Article> filteredList      = Lists.newArrayList(Collections2.filter(listArticles,               new ArticleFilter("test"))); 

However, this is quite some code for something which can be done in much less code using non functional programming, as demonstrated by @mgnyp...

like image 44
fge Avatar answered Nov 25 '22 09:11

fge