简体   繁体   English

java计数重复的键值对?

[英]java counting repeated key value pairs?

I've a Tuple class which has: 我有一个Tuple课,其中有:

private class Tuple {
    private int fileno;
    private int position;

    public Tuple(int fileno, int position) {
        this.fileno = fileno;
        this.position = position;
    }
}

I also have an hashmap which refrences this list like 我也有一个哈希表像这样引用此列表

    Map<String, List<Tuple>> index = new HashMap<String, List<Tuple>>();

Now there is a scenario where i need to count how many words in a file: The data is as follows: 现在有一种情况,我需要计算一个文件中有多少个单词:数据如下:

abc 10.txt
abc 10.txt
abc 10.txt
abc 12.txt
abc 12.txt
ghost 15.txt
and so on....

Now how to count number of occurences of the above? 现在如何计算上述次数? It is easy but i've been coding so long and also new to java. 这很容易,但是我已经编码了很长时间了,而且对于Java还是新的。 I also learnt duplicates can't go into hashmap! 我还了解到重复项不能进入哈希图! Thanks. 谢谢。

To add data to list: 要将数据添加到列表:

List<Tuple> idx = index.get(word);
                            if (idx == null) {
                                idx = new LinkedList<Tuple>();
                                index.put(word, idx);
                            }
                            idx.add(new Tuple(fileno, pos));

Above code just dumps the data, Now i will compare with words from a string array[]. 上面的代码只是转储数据,现在我将与字符串array []中的单词进行比较。 All i need at end is like this: abc 10.txt count - 3 abc 12.txt count - 2 ghost 15.txt count - 1 我最后需要的是这样的:abc 10.txt计数-3 abc 12.txt计数-2 ghost 15.txt计数-1

I'm not sure if map helps/i need to use a list again/write a function to do this? 我不确定地图是否可以帮助/我是否需要再次使用列表/编写函数来执行此操作? Thanks! 谢谢!

I solved the above problem with simple condition statements! 我用简单的条件语句解决了以上问题! Thanks @codeguru 谢谢@codeguru

/*
                     consider all cases and update wc as along
                     Lesson learnt - Map does not handle duplicates
                                    - List does not work
                                    - spend half a day figuring out 
                     */
                    if(wordInstance == null && fileNameInstance == null) {
                          wordInstance = wordOccurence;
                          fileNameInstance = files.get(t.fileno);
                    }
                    if(wordInstance == wordOccurence && fileNameInstance ==files.get(t.fileno)) {
                          wc++;
                    }
                    if(wordInstance == wordOccurence && fileNameInstance !=files.get(t.fileno)) {
                        wc=0;
                        fileNameInstance = files.get(t.fileno);
                        wc++;
                    }
                    if(wordInstance != wordOccurence && fileNameInstance ==files.get(t.fileno)) {
                        wc=0;
                        wordInstance = wordOccurence;
                        wc++;
                    }

I think you may be making this more complicated than it needs to be. 我认为您可能会使此过程变得比所需的复杂。 I suggest that you take a step back from the computer. 我建议您从计算机后退一步。 First you should take a simple input example and work out, by hand, what the output should be. 首先,您应该以一个简单的输入示例为例,并手工计算出输出应该是什么。 Next, write a few sentences (in your native language) describing the steps you took to work out this output. 接下来,用您的母语写几句话,描述为计算此输出所采取的步骤。 From there, you need to refine your description until you can easily translate it into Java. 从那里开始,您需要完善描述,直到可以轻松将其翻译成Java。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM