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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:44:07+00:00 2026-05-28T03:44:07+00:00

I would like to have a HashMap with only one key-value object. I have

  • 0

I would like to have a HashMap with only one key-value object.

I have created the following HashMap:

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

In the HashMap I would like to have only one entry/object.
The key type is an Integer.
The value type is a String.

e.g. = <1,”foo.txt”>

Every time I find a specific word in a file I would like to

  1. Increment the counter in key

  2. Add the new file in the value

e.g. Let’s say that I’m searching for the word “Hello” in a DocsCollection,
I have to store for every appearance of the word “Hello” the term frequency and concatenate the new file to the previous value.

<3,”foo.txt,hello.txt,test.txt”>

3 means that I’ve found the word “Hello” in three files.

and the Value consists of the files where the word was found

If I use the method put, a new entry is created in the HashMap cause the key changes.
It’s not stable. It begins with “1” but when I find the word second time , the key increments and then the put method inserts a new entry with a new key
But i would like to have only one entry and modify the key.
Can this be done?
How can i have only one object in a HashMap and modify the key every time?

   DocsCollection.put(2,"foo.txt,hello.txt"); 

Thanks, in advance

  • 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-28T03:44:07+00:00Added an answer on May 28, 2026 at 3:44 am

    The Map approach might not be the best. The issue is you are changing your key value.

    Note it might be better to just have a List<String>, and everytime you match the word, just add the file to the list. You can get the count easily with list.size()

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

Sidebar

Related Questions

I have a map: Map<String, String> ht = new HashMap(); and I would like
I have a final non-static member: private final HashMap<String,String> myMap; I would like to
I have the following code: InputStream reportFile = MyPage.this.getClass().getResourceAsStream(test.jrxml); HashMap<String, String> parameters = new
I have the following String[] temp; which returns red blue green I would like
I would like to have a HashMap<String, String> , that every day at midnight,
I would like to access hashmap set of values created in main class from
I would like to store a HashMap in an SQLite Database. I have tried
I have a mutable HashMap and would like to use it like a default-dictionary.
I have a problem that I would like have solved via a SQL query.
I would like to have a reference for the pros and cons of using

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.