适当的java数据结构,从文件中查找唯一的单词

时间:2017-03-29 07:05:54

标签: java collections

我需要解析文件中包含1000个句子的文件。 我必须在文件中找到一个唯一的单词意味着在文件中没有多次出现。 我应该使用哪种数据结构。(只需要使用java的DS执行此操作)以及为什么以及如何使用

第二个问题是

map.put("abc","hello");
map.put.("ABC","hi");

当我们按照上面给出的代码插入map对象时会发生什么。

2 个答案:

答案 0 :(得分:0)

你可以在地图中填写你的单词,这个单词的关键字和值的出现次数 当您再次在地图循环中填写数据时,请再次检查值是否大于等于1,如果大于1,则将其从地图中删除。 并且您最后可以返回此地图的大小,以了解唯一字数

如果您不想仅使用abcABC,则可以在地图中插入时使用LowerCaseUpperCase

这里有一个简单的例子:

public static void main(String[] args) {
    //i consider you know how to learn from a file, i use a simple array just to explain
    String[] listWords = {"hello", "word", "hello", "stack", "HELLO", "WORD", "123", "what?"};

    //fill your words in your map
    Map<String, Integer> map = new HashMap<>();
    for (String word : listWords) {
        if (!map.containsKey(word.toLowerCase())) {
            map.put(word.toLowerCase(), 1);
        } else {
            map.put(word.toLowerCase(), map.get(word.toLowerCase()) + 1);
        }
    }

    //print your map
    map.entrySet().forEach((entry) -> {
        System.out.println("word : " + entry.getKey() + " occurence : " + entry.getValue());
    });
    System.out.println("**************************************************");
    //loop throw your map and remove the words which occurrence > 1
    for (Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator(); it.hasNext();) {
        Map.Entry<String, Integer> entry = it.next();
        if (entry.getValue() > 1) {
            it.remove();
        }
    }

    //print your map again
    map.entrySet().forEach((entry) -> {
        System.out.println("word : " + entry.getKey() + " occurence : " + entry.getValue());
    });

    //size of your end map
    System.out.println("Size of map = " + map.size());
}

一些很好的参考资料,您可以根据地图进行参考:

How to update a value, given a key in a java hashmap?
iterating over and removing from a map

希望这可以给你一个想法。

答案 1 :(得分:0)

使用地图,使用单词作为键和值作为计数,对于您将其放入地图中的每个单词,并将计数增加一。

if(map.containsKey("some")){
    // get the current value
    int currentValue = map.get("some");
    // put back the key with incremented value
    map.put("some",currentValue+1);
} else {
    // first time
    map.put("some",1);
}

对于第二个问题,由于map键区分大小写,因此两个put都将添加到Map中。