title: hashCode and equals with a list property
body: <p>I have a class with a <strong>for-loop within the equals/hashCode</strong>:</p>
  
  <pre><code>class User {
  
  private List&lt;Task&gt; tasks;
  private ZonedDateTime date;
  
  
  @Override
  public int hashCode() {    
      int hash = 17;
      hash = 31 * hash + (date != null ? date() : 0);
      for (var task : tasks) {
          hash = 31 * hash + task.hashCode();
       }
  
      return hash;
      }
  
      @Override
      public boolean equals(Object obj) {
          if (this == obj) return true;
          if (obj == null || getClass() != obj.getClass()) return false;
  
          final User other = (User) obj;
  
          if (tasks.size() != other.tasks.size()) return false;
  
          // needed?
          for (int i = 0; i &lt; tasks.size(); i++) {
              if (!tasks.get(i).equals(other.tasks.get(i))) {
                  return false;
              }
          }
  
          return Objects.equals(timeStamp, other.timeStamp) &amp;&amp; Objects.equals(tasks, other. tasks);
  
      }
  }
  </code></pre>
  
  <p>I am used to have this version <strong>(version 2)</strong> of equals/hashCode, which is shorter and faster:</p>
  
  <pre><code>@Override
  public int hashCode() {
      return Objects.hash(date, tasks);
  }
  
  @Override
  public boolean equals(Object obj) {
      if (this == obj) return true;
      if (obj == null || getClass() != obj.getClass()) return false;
  
      final User other = (User) obj;
      return Objects.equals(timeStamp, other.timeStamp) &amp;&amp; Objects.equals(tasks, other. tasks);
  
  }
  </code></pre>
  
  <p>Can I replace the former equals/hashCode with the version 2 without worrying about correctness? </p>
  
  <p>Are both versions return the same result?</p>
  
  <p><strong>To sum up:</strong></p>
  
  <p>for typcial List implementation we can use version 2 instead of version 1. </p>
  
  <p><strong>One additional question related to this:</strong></p>
  
  <p>Will <strong>version 2</strong> be also valid, if the property <code>task</code> is not a <code>List</code> but a <code>Stream</code>? (<code>Stream&lt;Task&gt; tasks</code>).</p>