How can a HashMap consist only of one entry/object?

46,904

Solution 1

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()

Solution 2

Try this way:

DocsCollection = Collections.singletonMap(2, "foo.txt,hello.txt");

this Map can't be modified, if you want to to that just do this:

DocsCollection = Collections.singletonMap(3, "foo.txt,hello.txt");

Solution 3

I'll try to suggest a bit different solution to what I think your task is:

  • you have words (hello, etc)
  • you want to count how much files it is found in
  • you want to know the files

You can use a MultiMap (guava) for that:

  • map.put("hello", "file1.txt"); map.put("hello", "file2.txt");
  • map.keys().count("hello") - gets you the number of times each word is found
  • map.get("hello") returns a Collection<String> containing all the files for that word

And you can have as many words in that map as you like. If you needed one-entry-per-map, you'd need X maps for X words.

Solution 4

You are not really using a HashMap so to speak : your counter is not really a key.

What you seem to need according to your explanation is an Object representing the result of your search, such as:

public class SearchResult {
     private String searchedWord;
     private long counter;
     private List<String> containingFiles;
     // ...
}

Solution 5

Is there a reason you need to use a HashMap? You could just have an int (for the count) and a String or StringBuffer (for the filenames) and update them.

Alternatively, you could have a List into which you add the filename each time something is found. To get the count, use List.size(). But I see that @hvgotcodes already beat me to the punch with that idea.

Share:
46,904
programmer
Author by

programmer

Updated on April 07, 2021

Comments

  • programmer
    programmer about 3 years

    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

    • Brian Roach
      Brian Roach over 12 years
      I don't think you quite grasp what you use hashtables for. (This isn't it).
    • Aviram Segal
      Aviram Segal over 12 years
      Seems to me that a Map is not the correct approach to what you are trying to do...
  • hvgotcodes
    hvgotcodes over 12 years
    this is a good solution if he is searching on more than one word. But for one word, a List or array will do...
  • programmer
    programmer over 12 years
    Is the List going to be quick?Cause i'm having about 60 files and i need the work to be done quickly
  • programmer
    programmer over 12 years
    Well i need to store for each word the frequency and the files it appears in.Let's say that i have 10000 words, then i need to create 10000 classes. Isn't that going to cost me a lot in time?
  • programmer
    programmer over 12 years
    After i have created the Lists for each word, i have to iterate through the lists and concatenate every file of the list for each word.Let's say that i have 10000 words and each of the word appears in 10 files that means that i'm going to create 10000 Lists of 10 entries each. After that i have to write to a random access file for each word the files every word appears in.
  • Jean Logeart
    Jean Logeart over 12 years
    No. What is going to cost you time is searching the words. Keep your program simple and elegant and you will certainly end up having better performances than using data structures that do no meet your initial needs.
  • programmer
    programmer over 12 years
    e.g. "Hello" 5 "file1,file2,file3,file4,file5" etc. In order to find the files for each word i have to iterate through the whole list.Isn't that going to be a bit slow? Isn't that better to have only a pair <5,"file1,file2,file3,file4,file5"
  • Admin
    Admin over 12 years
    For 60 entries, you could copy the list thrice and do cartwheels each time you add an element and still spend so little time it would appear instantaneous to you. Ever heard that Knuth quote about optimization? Your bottleneck is whatever you do with those strings, not how you store them (if you want to find all files containing that word, you still have to check all files regardless of how you stored the files). Why do you think it could even begin to matter?
  • programmer
    programmer over 12 years
    What does the (1) mean here: private HashMap<Integer, String> occurrences = new HashMap<Integer, String>(1); <--?
  • user949300
    user949300 over 12 years
    Actually, his bottleneck is almost certainly the IO and the grep for the Strings. But I like the suggestion of doing cartwheels, so +1.
  • davogotland
    davogotland over 12 years
    @programmer docs.oracle.com/javase/1.5.0/docs/api/java/util/… op asked for hashmap with one entry. no point in getting 15 empty ones ;)