title: How to search a generic collection object in java
body: <p>I am working on java and currently trying to search a list of object of a particular <code>DummyUserList</code> type.</p>
  
  <pre><code>public class DummyUserList implements Serializable {
  
          String firstName;
          String lastName;
          String city;
          ----
          ----
  
          setter andd getter will go here.
      }
  </code></pre>
  
  <p>and</p>
  
  <pre><code> public static void main(String[] args) {
  
          List&lt;DummyUserList&gt; searchListObj = new ArrayList&lt;DummyUserList&gt;();
  
          searchListObj.add(dummyUserList1);
          searchListObj.add(dummyUserList2);
          searchListObj.add(dummyUserList3);
  
          String toBeSearched = "Singhaniya";
  
          List&lt;DummyUserList&gt; result = searchListObj.stream()
              .filter(a -&gt; Objects.equals(a.lastName, toBeSearched))
              .collect(Collectors.toList());
  
          System.out.println("result---&gt;" + result);
  
      }
  </code></pre>
  
  <p>Currently, the above code is searching <code>DummyUserList</code> object on the basis of <code>lastName</code>. It's working fine, but how can I pass a generic collection object to the <code>filter</code> method for searching?</p>
  
  <p>I am trying in the following way: I am passing a generic collection object and it's supposed to be search and return a list of searched  object, but I'm getting a syntax error saying "<strong>searchText</strong> can not be resolved or is not a field".I want to do something like below code.</p>
  
  <pre><code>public static &lt;T&gt; List&lt;T&gt; search(Collection&lt;T&gt; collectionObject,
              String searchProperty, String searchText) {     
  
          List&lt;T&gt; result = collectionObject.stream()
              .filter(a -&gt; Objects.equals(((List&lt;T&gt;) collectionObject).searchText))
              .collect(Collectors.toList());
  
      }
  </code></pre>
  
  <p>How do I pass a generic collection to the above method for searching? Is it possible to search a dynamic collection object?</p>