title: Fastest way to check the number of occurences in a list in java
body: <p>I have a list and each element of the list contains an array of Points.</p>
  
  <pre><code>List&lt;LineString&gt; lines = new LinkedList&lt;LineString&gt;()
  </code></pre>
  
  <p>The list looks like this :</p>
  
  <pre><code>object1 - ((2.36937, 48.82156, NaN), (2.36935, 48.82159, NaN), (2.36931, 48.8217, NaN))
  object2 - ((2.35855, 48.83217, NaN), (2.35851, 48.83218, NaN), (2.35846, 48.83219, NaN), (2.35842, 48.83218, NaN), (2.35838, 48.83217, NaN))
  object3 - ((2.35841, 48.832, NaN), (2.35846, 48.83199, NaN), (2.35852, 48.832, NaN), (2.35857, 48.83202, NaN), (2.3586, 48.83205, NaN), (2.35861, 48.83208, NaN), (2.35861, 48.83212, NaN), (2.35859, 48.83214, NaN))
  </code></pre>
  
  <p>And so on and so forth. I would like to count the occurrences of the first element of each object in all the objects of the list.
  Which is the fastest way to do that?</p>
  
  <p>The list has almost 70000 objects and my way of implementing takes too much time to provide the results because i am using a O(n*n) implementation:</p>
  
  <pre><code>for (ListIterator l = validLines.listIterator(); l.hasNext();)
  {
          int aux1=0, aux2=0;
          LineString validLine = (LineString) l.next();
  
          for(ListIterator m = (ListIterator) lines.listIterator(); m.hasNext();)
          {
  
                  LineString compare = (LineString) m.next();
                  for(int i=0;i&lt;compare.getCoordinates().length;i++)
                  {
                     if( validLine.getCoordinates()[0] == compare.getCoordinates()[i] )aux1++;
                     if( validLine.getCoordinates()[validLine.getCoordinates().length-1] == compare.getCoordinates()[i] ) aux2++;
                     if(aux1 &gt;1 || aux2&gt;1)break;
  
                  }
          }
  </code></pre>
  
  <p>}</p>
  
  <p>Is there any way i can make it faster?</p>