Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8911885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:03:37+00:00 2026-06-15T04:03:37+00:00

I have a program that collects information from the database. in my previous question

  • 0

I have a program that collects information from the database.

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: ConcurrentModificationExecption

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)

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:

ID Name date NoC NoAC

1 Mail 2012-11-27 3 3

Where NoC = number of calls
and NoAC = number of answered calls.

Now to my question.

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?

** CODE **

    ArrayList<CallQueue> queues = new ArrayList<>();
    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 > 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 > 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 > 19) {
                    hourOfDay = 19;
                }
                cq.addCallsByTime(hourOfDay, callAmount);
                queues.add(cq);

            }

        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:03:39+00:00Added an answer on June 15, 2026 at 4:03 am

    Explanation

    Don’t make queues an ArrayList, make it a HashSet or some other sort of Set. This will catch duplicates for you in O(1) time instead of O(n). Once you’re done loading the query, you can always take the data out of the HashSet and put it into an ArrayList for future purposes (which has the added advantage of you will know exactly how long to make the ArrayList. It would be an O(n) copy, but you’d only have to do it once instead of every time like the other way.

    To do this correctly, you will probably have to override CallQueue.hashCode() and .equals(), but that’s easy enough, just return the .hashCode() and .equals() methods of the String name field.

    Bug??

    By the way, I assumed that query is a java.sql.ResultSet, but ResultSet doesn’t have getInteger, it has getInt. Does your code compile?

    The Code

    Here’s the code, to see what I mean:

    HashMap<String, CallQueue> queues = new HashMap<String, CallQueue>(); 
    
    while (query.next()) {
      if (!queues.isEmpty()) {
        if (queues.containsKey(query.getString("NAME"))) {
          CallQueue oldQueue = queues.get(query.getString("NAME"));
          double decimalTime = query.getDouble("DATE");
          int hourOfDay = (int)Math.round(24 * decimalTime);
          int callAmount = query.getInt("NoC");
          if (hourOfDay > 19) {
            hourOfDay = 19;
          }
    
          oldQueue.addCallsByTime(hourOfDay, callAmount);
        } 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.getInt("NoC");
            if (hourOfDay > 19) {
              hourOfDay = 19;
            }
            cq.addCallsByTime(hourOfDay, callAmount);
            queues.put(query.getString("NAME"), cq);
          }
        }
      }
    }
    
    // you could return this if you just want a collection...
    Collection<CallQueue> values = queues.values();
    
    // Or this if you MUST have an ArrayList...
    return new ArrayList(values);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am working at my project where it collects information from two database, one
I have a database that collects client information using assessments and other tools. Often
I am writing a small program that collects information from a text file, in
I have a program that gets a JSON from the server using getJSON and
I have a program that reads from a file that grabs 4 bytes from
I am working on a simple program that collects data from the field on
I have a program that needs to read data from a number of sources.
I have a big winform class that calls from a little one like this:
I have a program that collects objects over time. Those objects are often, but
I have program that requires Python 3, but I develop Django and it uses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.