Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7825139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:50:46+00:00 2026-06-02T08:50:46+00:00

I am working on a program where i am reading a file and extracting

  • 0

I am working on a program where i am reading a file and extracting keywords and their count. Later i need to pick up word with topmost frequency and match them with a keyword.

I have stored all the keywords i have found in the file in String list.
I wish to sort these on basis of frequency. So if at index 17 i have a word “stack” with value at index 17 in other integer list to be maximum, i wish to take them to position 1.

I can sort these using collections.sort but it does not take care of other list.

Here is my code :

while(m.find()) 
    {
        if(keyword.contains(m.group()))
            {
            keywordcount.set(keyword.indexOf(m.group()),keywordcount.get(keyword.indexOf(m.group()))+1);
            //System.out.println("*"+m.group()+":"+keywordcount.get(keyword.indexOf(m.group())));
            }
        else
            {
            keyword.add(m.group());
            int var=keyword.indexOf(m.group());
            //System.out.println(m.group()+":"+var);
            keywordcount.add(var, 1);
            }
        //System.out.println(keyword.size()+"#"+keywordcount.size());                       
    }
    for(int i=0;i<keyword.size();i++)
    {
        System.out.print(keyword.get(i)+ ":" +keywordcount.get(i)+" ");
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T08:50:48+00:00Added an answer on June 2, 2026 at 8:50 am

    Typically, one would put both the String and the Integer into a single class, and sort a list of instances of that class.

    E.g.

    class StringCount implements Comparable<StringCount> {
        private final String string;
        private final int count;
    
        public StringCount(String string, int count) {
            this.string = string;
            this.count = count;
        }
    
        @Override
        public int compareTo(StringCount right) {
            return this.count < right.count ? -1
                 : this.count > right.count ? 1
                 : 0;
        }
    
        // implement equals and hashCode too
        // if a.compareTo(b) == 0, then a.equals(b) should return true.
    
    }
    

    Then, you can create a List<StringCount> and call Collections.sort(stringCountList).

    Note, that this will put the StringCount instances with the lowest values first, so they come out in ascending order.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a program that requires reading in integers from a file
I'm working on a program for class that reads data from a file, the
So I'm reading in a file (like java program < trace.dat) which looks something
I have a Perl program that is reading html tags from a text file.
I'm working on a program that takes a redirected file as input. For example,
My program is reading in a file, line by line. But if a non
I'm working on an a GPU accelerated program that requires the reading of an
Here is the sample program I am working on to read a file with
I have a program in Perl I'm working on where I would need multiple
In a project I'm working on, I need to read from a CSV file,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.