English 中文(简体)
哈希马普如何只包括一个入境点。
原标题:How can a HashMap consist only of one entry/object?
  • 时间:2012-01-12 20:01:23
  •  标签:
  • java
  • hashmap

我愿有一张<代码>HashMap,只有一个关键价值物体。

我建立了以下<代码>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”>

我每次在档案中找到一个具体字句,都想说:

  1. B. 在钥匙中增加反射

  2. 添加价值中的新档案

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 是指我发现三个档案中的“Hello”一词。

价值包括发现该词的档案

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"); 

提前感谢

最佳回答

地图方法可能不是最佳办法。 问题是,你正在改变其关键价值。

或许最好是有<条码>。 清单和编号;String>,每当你与这个词吻合时,就在名单上添加档案。 您可以轻易获得<代码>清单>。

问题回答

采用这种方式:

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

如果你想这样做的话,该地图可以修改:

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

我认为,你的任务是:

  • 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

你可以像你一样在地图上讲这么多的话。 如果你需要一张入境地图,你需要X地图。

是否有必要使用哈希姆普? 你们只能有一只 in子(计数)和一个“强硬”或“强硬”(档案名称),并更新。

或者,在发现每一件事时,你可以有一份名单,在名单上添加。 为了计算,使用清单。 但我认为,“hvgotcodes”已经把这个想法寄给我。

你没有真正使用<代码>HashMap,这样说:你的反响实际上不是关键。

根据您的解释,您似乎需要的是。 目标,代表您的查询结果,例如:

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

确保你想要的工作:

  1. Declare the value to be a List<String>.
  2. Remove the original key/value pair and replace it with the new contents every time you found a word.

类似:

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);
}

不是好的出路。

取而代之的是Map<String, Set<String>>,其中关键是关键词,其价值是你发现的关键词。 之后将考虑:

//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);
}

然后,当需要时,你就能够从该地图收集信息。 尤其是,为了收集卷宗数量,发现关键词,请:

for (final String keyword: map.keySet())
    System.out.printf("%s was encountered %d time(s)
",
        keyword, map.get(keyword).size());
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);
    }
}

当你找到文件时,请在你发现时,在你的地图上再说一遍。

addFile("hello.txt");

确实值得注意的是,这完全是受挫的。

a. 着手解决 v问题;

这些日子可能使用<代码>Map.of(钥匙,价值)。

    /**
 * Returns an unmodifiable map containing a single mapping.
 * See <a href="#unmodifiable">Unmodifiable Maps</a> for details.
 *
 * @param <K> the {@code Map} s key type
 * @param <V> the {@code Map} s value type
 * @param k1 the mapping s key
 * @param v1 the mapping s value
 * @return a {@code Map} containing the specified mapping
 * @throws NullPointerException if the key or the value is {@code null}
 *
 * @since 9
 */
static <K, V> Map<K, V> of(K k1, V v1) {
    return new ImmutableCollections.Map1<>(k1, v1);
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...