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

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

I created the following HashMap :

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

In HashMap, I would like to have only one record / object. The key type is an integer. The value type is a string.

eg. = <1, "foo.txt">

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

  • Key counter increment

  • Add a new file to the value

eg. Say I'm looking for the word “Hello” in a DocsCollection , I have to store the term “frequency” for each occurrence of the word “Hello” and merge the new file with the previous value.

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

3 means that I found the word "Hello" in three files.

and the value consists of files in which the word was found

If I use the put method, a new entry is created in the HashMap because the key is changing. It is not stable. It starts with "1", but when I find the word a second time, the key is incremented, and then the put method inserts a new record with a new key. But I would like to have only one record and change the key. It can be done? How can I have only one object in the HashMap and change the key every time?

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

Thanks in advance

+8
java hashmap
source share
9 answers

The map approach may not be the best. The problem is that you are changing your key value.

Note that it is best to have a List<String> , and each time you match a word, just add the file to the list. You can easily get the score using list.size()

+6
source share

Try as follows:

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

this map cannot be changed if you want to do this:

 DocsCollection = Collections.singletonMap(3, "foo.txt,hello.txt"); 
+16
source share

I will try to offer a slightly different solution for what, in my opinion, is your task:

  • Do you have words ( hello , etc.)
  • you want to count how many files are found in
  • do you want to know files

You can use MultiMap (guava) for this:

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

And you can have as many words on this card as you want. If you need one entry per card, you will need X cards for X words.

+5
source share

Is there a reason you need to use a HashMap? You can simply have int (for count) and String or StringBuffer (for file names) and update them.

In addition, you may have a list in which you add a file name every time something is found. To get the score, use List.size (). But I see that @hvgotcodes already beat me up to this idea.

+2
source share

You really don't use HashMap , so to speak: your counter is not really the key.

What seems to you according to your explanation is an Object representing the result of your search, for example:

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

Here is the idea of ​​having a map with one key / value: create a map, add a single key-value pair, and then make it unmodifiable using Collections.unmodifiableMap() . Thus, no other elements can be added to the map. Like this:

 HashMap<Integer, String> docsCollection = new HashMap<Integer, String>(); docsCollection.put(2, "foo.txt,hello.txt"); docsCollection = Collections.unmodifiableMap(docsCollection); 

This only works if you know the key / value in advance; after calling unmodifiableMap map is effectively frozen, and you cannot add / remove other elements from it.

Now, what you ask in the question is not suitable for using the map, this is not the correct data structure to use in this case. You would be better off having an ArrayList<String> by adding the file names where the word was found to it, and using the size() list method to determine the number of files in which the word was found.

+1
source share

To ensure that you want to work:

  • Declare the value of List<String> .
  • Remove the original key / value pair and replace it with new content every time you find a word.

Something like:

 HashMap<Integer, List<String>> map = new HashMap<Integer, List<String>>(); // some loop if(/* new word found */) { Integer key = (Integer)map.keySet().toArray()[0]; List<String> value = (List<String>)map.get(key); value.add(word); map.remove(key); map.put((key + 1), value); } 
+1
source share

Not the best way to go.

Try Map<String, Set<String>> instead, where the key is the keyword and the value is the set of files in which you found the keyword. Then adding to it will look like this:

 //further up final Map<String, Set<String>> map = new HashMap<String, Set<String>>(); //then: public void addRef(final String keyword, final String filename) { if (!map.containsKey(keyword)) // keyword not encountered yet map.put(keyword, new HashSet<String>()); map.get(keyword).add(filename); } 

Then you can collect information from this card when the need arises. In particular, to collect the number of files in which the keyword was found, you should:

 for (final String keyword: map.keySet()) System.out.printf("%s was encountered %d time(s)\n", keyword, map.get(keyword).size()); 
+1
source share
 public class YourClass { private HashMap<Integer, String> occurrences = new HashMap<Integer, String>(1); public void addFile(String name) { int count = 0; String names = ""; if(occurrences.size() > 0) { count = (int)(occurrences.keySet().toArray()[0]); names = occurrences.get(count); names += ","; } count++; names += name; occurrences.remove(count); occurrences.put(count, names); } } 

when you find the file (call it hello.txt), and suppose you are in YourClass when you find it:

 addFile("hello.txt"); 

notice that this is completely late>. <

go with vakimshaar solution;)

+1
source share

All Articles