title: Correct way of looping to avoid creating double objects
body: <p>I have a program that collects information from the database. </p>
  
  <p>in my previous question i asked about an execption and one of the reponses made me rethink the idea of my loop Link to the old question: <a href="https://stackoverflow.com/questions/13609323/concurrentmodificationexecption">ConcurrentModificationExecption</a></p>
  
  <p>So here is the background i collest alot of information from the database one of the information is the name of the call type that is stored in my Database for example: Mail, Telefon ect. would be different type of contact in formation (which we call, CallQueues)</p>
  
  <p>Since the information that i am extracting is over several days many of the call types will have dublicates. The following is an example of how a row in the database looks like:</p>
  
  <p>ID  Name date  NoC NoAC</p>
  
  <p>1  Mail 2012-11-27 3 3</p>
  
  <p>Where NoC = number of calls
  and NoAC = number of answered calls.</p>
  
  <p>Now to my question. </p>
  
  <p>My original idea was to loop through the list of queues and see if the name reapeared however this would not work as i could not change the list while looping through it. So here is my new idea starting from the while loop, what i want to know is: Is this the best way to avoid dublicates in this situation and if not could you please explain to me how i should do it?</p>
  
  <p>** CODE **</p>
  
  <pre><code>    ArrayList&lt;CallQueue&gt; queues = new ArrayList&lt;&gt;();
      while (query.next()) {
  
          boolean isNew = true;
          if (!queues.isEmpty()) {
              for (CallQueue callQueue : queues) {
                  if (callQueue.getType().equals(query.getString("NAME"))) {
                      double decimalTime = query.getDouble("DATE");
                      int hourOfDay = (int)Math.round(24 * decimalTime);
                      int callAmount = query.getInteger("NoC");
                      if (hourOfDay &gt; 19) {
                          hourOfDay = 19;
                      }
  
                      callQueue.addCallsByTime(hourOfDay, callAmount);
                      isNew = false;
                  }else {
                      isNew = true;
                  }
              } 
  
              /* Out side the foreach loop, checks if the boolean isNew is true if it is create a new object and insert into the list*/
              if (isNew) {
                  String queueName = query.getString("NAME");
                  if (!queueName.equalsIgnoreCase("PrivatOverflow")) {
                      CallQueue cq = new CallQueue(query.getString("NAME"));
                      double decimalTime = query.getDouble("DATE");
                      int hourOfDay = (int)Math.round(24 * decimalTime); 
                      int callAmount = query.getInteger("NoC");
                      if (hourOfDay &gt; 19) {
                          hourOfDay = 19;
                      }
                      cq.addCallsByTime(hourOfDay, callAmount);
                      queues.add(cq);
                  }
              }
              /* if queues is empty which it will be the first time*/
          }else {
              String queueName = query.getString("NAME");
              if (!queueName.equalsIgnoreCase("PrivatOverflow")) {
                  CallQueue cq = new CallQueue(query.getString("NAME"));
                  double decimalTime = query.getDouble("DATE");
                  int hourOfDay = (int)Math.round(24 * decimalTime); 
                  int callAmount = query.getInteger("NoC");
                  if (hourOfDay &gt; 19) {
                      hourOfDay = 19;
                  }
                  cq.addCallsByTime(hourOfDay, callAmount);
                  queues.add(cq);
  
              }
  
          }
      }
  </code></pre>