title: Accessing object via another object
body: <p>I have three classes: <code>User</code>, <code>UserCategory</code>, and <code>PatientCase</code>.</p>
  
  <p>Now, in some methods I need to check if the category or the case belong to the user who is currently logged in.</p>
  
  <p>User:</p>
  
  <pre class="lang-java prettyprint-override"><code>private Long id;
  private String firstName;
  private String lastName;
  private String emailAddress;
  private String password;
  private Set&lt;UserCategory&gt; userCategories;
  </code></pre>
  
  <p>UserCategory:</p>
  
  <pre class="lang-java prettyprint-override"><code>private Long id;
  private String name;
  private User user;
  private Set&lt;PatientCase&gt; cases;
  </code></pre>
  
  <p>PatientCase:</p>
  
  <pre><code>private Long id;
  private String caseTitle;
  private LocalDate caseDate;
  private Set&lt;UserCategory&gt; userCategories;
  </code></pre>
  
  <p>Now given a user and a category, I can check if the category does or does not belong to the user by doing this (in this case I am doing does not belong to the user):</p>
  
  <pre class="lang-java prettyprint-override"><code>private boolean checkIfCategoryDoesNotBelongToUser(UserCategory category, User user) {
      return !category.getUser().getId().equals(user.getId());
  }
  </code></pre>
  
  <p>The problem is how can I tell if the <code>PatientCase</code> does or does not belong to the user. And I am not allowed to add in any other fields to the classes. I was thinking of doing something like:</p>
  
  <pre class="lang-java prettyprint-override"><code>private boolean checkIfCaseDoesNotBelongToUser(PatientCase patientCase, User user) {
      return !patientCase.getUserCategories()
              .stream()
              .filter(c -&gt; c.getUser().getId().equals(user.getId()))
              .
  </code></pre>
  
  <p>Also I'm aware that filter doesn't actually return a boolean so maybe, <code>noneMatch</code>. But not really sure.</p>
  
  <p>Basically, a patient case can belong to multiple categories. And a user can have multiple categories. Please suggest something.</p>