title: Search for field in a List of Objects in Java
body: <p>In an application I'm designing I have a list of custom Objects called <code>Shop</code>. The Shop class can be seen here.</p>
  <h3>Shop.java</h3>
  <pre><code>public class Shop extends Object {
  
      private String title = &quot;&quot;;
      private List&lt;HashMap&lt;String, String&gt;&gt; branchDetails = new ArrayList&lt;&gt;();
      private String description = &quot;&quot;;
      private String imageLink = &quot;&quot;;
      private String webLink = &quot;&quot;;
  
      public Shop() {
  
      }
  }
  </code></pre>
  <p>In my application I create a <code>List&lt;Shop&gt;</code> object and populate it with 1500 Shop objects.</p>
  <p>Now I would like to search through the List and find the index of a Shop with a &quot;Weblink&quot; that matches a string. i.e. I would like to query the List the same way you would a Database. Is there a way to do this without having to build and populate a Database?</p>
  <p>I have overridden the equals and hashcode methods in Shop.java and can use the <a href="https://docs.oracle.com/javase/7/docs/api/java/util/List.html#contains(java.lang.Object)" rel="nofollow noreferrer">List#contains</a> method to verify the list contains a object with a matching weblink but cannot get the index of that object.</p>
  <p>The equals and hashcode methods:</p>
  <pre><code>public boolean equals(Object o) {
      if (this == o) return true;
      if (o == null || getClass() != o.getClass()) return false;
  
      Shop shop = (Shop) o;
  
      return getWebLink().equals(shop.getWebLink());
  
  }
  
  @Override
  public int hashCode() {
      return getWebLink().hashCode();
  }
  </code></pre>
  <p>To use this I create a new shop with webLink I would like to query</p>
  <pre><code>Shop shop = new Shop();
  shop.setWebLink(&quot;http://somelink.com&quot;);
  
  shopsList.contains(shop) //returns true if weblink string matches one from list of shops
  </code></pre>