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

适当的java数据结构,从文件中查找唯一的单词,java,collections,Java,Collections,我需要解析一个包含1000个句子的文件。 我必须在文件中找到唯一的单词,这意味着该单词在文件中出现的次数不超过一次。 我应该使用的数据结构(只需要使用java的DS执行此操作)以及为什么和如何使用 第二个问题是 map.put("abc","hello"); map.put.("ABC","hi"); 当我们在地图对象中插入上述代码时,会发生什么情况。您可以在地图中填充您的单词,地图上有一个关键字,即单词和出现次数的值。 当您完成在地图循环中再次填充数据时,再次检查值“如果伟大,则为开”或“等

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

第二个问题是

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

当我们在地图对象中插入上述代码时,会发生什么情况。

您可以在地图中填充您的单词,地图上有一个关键字,即单词和出现次数的值。
当您完成在地图循环中再次填充数据时,再次检查值“如果伟大,则为开”或“等于一”,如果“伟大,则为一”,则将其从地图中删除。 您可以在最后返回此地图的大小,以了解唯一单词的数量


如果您不想只使用
abc
abc
,则在地图中插入时可以使用
小写
大写

这里有一个简单的例子,你可以去扔:

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());
}
publicstaticvoidmain(字符串[]args){
我认为你知道如何从一个文件中学习,我用一个简单的数组来解释。
String[]listWords={“你好”,“单词”,“你好”,“堆栈”,“你好”,“单词”,“123”,“什么?”};
//在地图上填上你的单词
Map Map=newhashmap();
for(字符串字:列表字){
if(!map.containsKey(word.toLowerCase())){
map.put(word.toLowerCase(),1);
}否则{
map.put(word.toLowerCase(),map.get(word.toLowerCase())+1);
}
}
//打印地图
map.entrySet().forEach((条目)->{
System.out.println(“单词:“+entry.getKey()+”出现:“+entry.getValue()”);
});
System.out.println(“*******************************************************************”);
//循环抛出地图并删除出现次数>1的单词
for(Iterator it=map.entrySet().Iterator();it.hasNext();){
Map.Entry=it.next();
if(entry.getValue()>1){
it.remove();
}
}
//再次打印地图
map.entrySet().forEach((条目)->{
System.out.println(“单词:“+entry.getKey()+”出现:“+entry.getValue()”);
});
//末端地图的大小
System.out.println(“映射大小=“+map.Size());
}

一些很好的参考资料,您可以根据这些参考资料使用地图:




希望这能给你一个想法。

使用Map,使用单词作为键,使用值作为计数,对于你在Map中输入的每个单词,将计数增加一

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

对于第二个问题,这两个put都将添加到地图中,因为地图键区分大小写。

谢谢。这对我理解有很大帮助。