title: Find a specific object at a specific coordinate in ArrayList
body: <p>Im currently working on a 2d tile-based game and I've come to a point where I need to optimise my code quit a bit.
  Im adding a lot of different elements in javas List such as trees, rocks and so on. The reason why the elements is not a static tile is because I want the player to be able to interact with elements. Therefor all elements has to be its own object and stored in a list.</p>
  
  <p>The problem is whenever I want to interact with a specific element in the built in List in java I have to create a for-loop and go through every single element in the list to find the right element at the right coordinates. (such as collision and so on.)</p>
  
  <p>The way I do it is like this:</p>
  
  <pre><code>List&lt;Element&gt; elements = new ArrayList&lt;Element&gt;();
  
  
  public Element getElementOnPixel(int x, int y){`
  
          for(int i = 0; i &lt; elements.size(); i++){
  
              int xx = elements.get(i).getX() * Game.TILE_SIZE;
              int yy = elements.get(i).getY() * Game.TILE_SIZE;
              int w = xx + elements.get(i).width;
              int h = yy + elements.get(i).height;
  
              if(x &gt;= xx &amp;&amp; x &lt; w &amp;&amp; y &gt;= yy &amp;&amp; y &lt; h) {
                  return elements.get(i);
              }
  
          }
          return null;
      }
  </code></pre>
  
  <p>As you can see above I have to go thought the whole list until I find the right element at the right coordinate. Heres where the problem pops up because I have ALOT of elements in my game. My question follows:</p>
  
  <p>Is there any other way to find a specific element at a specific coordinate in javas built in Lists?  </p>