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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:05:01+00:00 2026-05-29T05:05:01+00:00

I have created a hash map in which each entry corresponds to 3 values

  • 0

I have created a hash map in which each entry corresponds to 3 values
Key object values ( which are two in number)

I have created a class, whose object I create and store the results in a hash map.
This is my code below in which I compare my incoming data with the previous values in the hash map. If the same data comes then I just increment the counter of that data. Problem is that it prints out the same data multiple times. Why is that?

class Info {
    int counter;
    String Data;
}
Info info = new Info();

int i=0;
int lines=0;

HashMap<String, Info> hMap = new HashMap<String, Info>();

try {
    BufferedReader reader =
                     new BufferedReader(new FileReader("E:\\write1.txt"));
    String line = null;
    while ((line = reader.readLine()) != null)
    {
        lines++;
        if(line.startsWith("Data:"))
        {
            String comingdata = line.replaceAll("Data:","");
            if(hMap.isEmpty())  // first time to put data in hashmap
            {
                info.counter=1;
                info.Data=comingdata;
                hMap.put("1",info);
            }
            else
            {
                int m=0;
                // everytime it will run to
                // check to increment the counter if the value already exists
                for(i=1;i<=hMap.size();i++)
                {
                    String skey = Integer.toString(i);

                    if(hMap.get(skey).Data.equals(comingdata))
                    {
                        hMap.get(skey).counter=  hMap.get(skey).counter+1;
                        m=2;
                    }
                }
                // making new entry in hashmap
                if(m==0)
                {                                  
                    String skey = Integer.toString(hMap.size()+1);
                    info.counter=1;
                    info.Data=comingdata;
                    hMap.put(skey,info);
                }
            }// else end
        }  //if end
    } //while end
}  //try end
catch (Exception e) { }
for(i=1;i<=hMap.size();i++)
{
    String skey= Integer.toString(i);
    System.out.println(hMap.get(skey).Data);
    System.out.println(hMap.get(skey).counter);
    System.out.println("\n");
}
  • 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-29T05:05:05+00:00Added an answer on May 29, 2026 at 5:05 am

    You only ever create one Info object, so all keys in the HashMap map to the same object. When you insert anything into the map, you should create a new Info object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of hashes. Each entry looks like this: - !map:Hashie::Mash name:
I have a List and each entry has a dynamic count of attributes which
I have a java class that contains a hash map as a member. This
I have created a C# class file by using a XSD-file as an input.
I have an object Line which contains 2 objects of type Point called Point1
I have a HashMap called examList which stored exam grades of each course a
I have this field: HashMap<String, HashMap> selects = new HashMap<String, HashMap>(); For each Hash<String,
Ok, so I have this program with many (~300) threads, each of which communicates
I want to create a Hash Map (or another structure, if you have any
I have created one listview, in this each row contain check box and the

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.