im having trouble displaying the 10 most common word pairs. im sure it is just a matter of sorting and taking the data but i am unsure on how to do this?

private static void generateOutput()
    {
        for(Entry e : result.entrySet())
        {
            System.out.println("Symbol: " + e.getKey());
            Map<String, Integer> count = new HashMap<String,Integer>();
            List<String> words = (List)e.getValue();

            for(String s : words)
            {
                if(!count.containsKey(s))
                {
                    count.put(s, 1);
                }
                else
                {
                    count.put(s, count.get(s)+1);
                }
            }

            for (Entry f : count.entrySet())
            {
                System.out.println("\tfollowing symbol: " + f.getKey() + " : " + f.getValue());
            }
        }
        System.out.println();
    }

this is my output, but it displays everything. how do just sort this and display the top 10 most commonly used word pairs?

Recommended Answers

All 6 Replies

sort them, based on the value of occurences, and only print the first ten, using a normal for or a while loop.

would i be able to sort it using a method like this used to sort an array of numbers?

 for (int i = numbers.length-2; i >= 0; i--)
            for (int j = 0; j <= i; j++)
                if (numbers[j] > numbers[j+1])
                {
                    int temp = numbers[j];
                    numbers[j] = numbers[j+1];
                    numbers[j+1] = temp;
                }  

sure. unless you can think of a reason why you wouldn't be?

what would i substitute for numbers.length-2?
ive tried this:

for (Entry f : count.entrySet())
            {

                     if (f.getValue() > f.getValue()+1)
                    {
                        int temp = f.getValue();
                        f.getValue() = f.getValue()+1;
                        f.getValue()+1 = temp;
                    }

but this doesnt work? im not exactly sure how to retrieve the integer variable to compare it

that's because it doesn't make sense. you should be comparing the values of two elements f. you'll need to use a normal for (not the enhanced) and use those indices you get from it.

thank you it is has solved the problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.