title: Using a predicate for a query on a field
body: <p>I'm messing around with lambdas and I'm trying to create a generic way to form a predicate for a class on a field. Here's some code to illustrate:</p>
  
  <pre><code>public class A {
  
  private String b;
  private String c;
  
  public A(String b, String c) {
      this.b = b;
      this.c = c;
  }
  
  public String getB() {
      return b;
  }
  
  public String getC() {
      return c;
  }
  
  }
  
  public class Main {
  
  public static void main(String[] args) {
      List&lt;A&gt; list = Arrays.asList(new A("aa","bb"),new A("aaC","bb"));
      Test test = new Test();
      test.setList(list);
      test.createPred("aa");
  }
  
  }
  
  
  public class Test {
  
  private List&lt;A&gt; list;
  
  public void setList(List&lt;A&gt; list) {
      this.list = list;
  }
  
  public Predicate&lt;A&gt; createPred(String query) {
      return new Predicate&lt;A&gt;() {
          @Override
          public boolean test(A t) {
              return t.getB().equals(query);
          }
      };
  }
  
  public List&lt;A&gt; search(Predicate&lt;A&gt; a) {
      return list.stream().filter(a).collect(Collectors.toList());
  }
  }
  </code></pre>
  
  <p>How can I write createPred so it can take a field? I want the method to be "field-generic" I suppose. I'm guessing using java reflection here is not a good idea.</p>