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 6657933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:49:53+00:00 2026-05-26T01:49:53+00:00

I want to store all values of a certain variable in a dataset and

  • 0

I want to store all values of a certain variable in a dataset and the frequency for each of these values. To do so, I use an ArrayList<String> to store the values and an ArrayList<Integer> to store the frequencies (since I can’t use int). The number of different values is unknown, that’s why I use ArrayList and not Array.

Example (simplified) dataset:

a,b,c,d,b,d,a,c,b

The ArrayList<String> with values looks like: {a,b,c,d} and the ArrayList<Integer> with frequencies looks like: {2,3,2,2}.

To fill these ArrayLists I iterate over each record in the dataset, using the following code.

public void addObservation(String obs){
    if(values.size() == 0){// first value
        values.add(obs);
        frequencies.add(new Integer(1));
        return;//added
    }else{
        for(int i = 0; i<values.size();i++){
            if(values.get(i).equals(obs)){
                frequencies.set(i, new Integer((int)frequencies.get(i)+1));
                return;//added
            }
        }
        // only gets here if value of obs is not found
        values.add(obs);
        frequencies.add(new Integer(1));
    }
}

However, since the datasets I will use this for can be very big, I want to optimize my code, and using frequencies.set(i, new Integer((int)frequencies.get(i)+1)); does not seem very efficient.

That brings me to my question; how can I optimize the updating of the Integer values in the ArrayList?

  • 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-05-26T01:49:54+00:00Added an answer on May 26, 2026 at 1:49 am

    Use a HashMap<String,Integer>

    Create the HashMap like so

    HashMap<String,Integer> hm = new HashMap<String,Integer>();
    

    Then your addObservation method will look like

    public void addObservation(String obs) {
        if( hm.contains(obs) )
            hm.put( obs, hm.get(obs)+1 );
        else
            hm.put( obs, 1 );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ArrayList of type String that contain certain values. I want to
I'm lazy! And I just want to store one single string value. That's all!
I want to store all of the data that is kept in encoding_symbols_tab [0]
I have problem with return statment >.< I want to store all magazine names
hi all i want to know that we can store information and session information
Below is my stored procedure. I want use stored procedure select all row of
I'm using ASP.NET MVC , and I wish to store all my DateTime values
I want to remove all stored revision info, except for the most up to
I want to convert all of my db stored procedures to linq to sql
I want a query that returns a list of all the (user) stored procedures

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.