Thread: i have text with some text i want to count duplicate words in file and print that duplicate words

Forum : Java SE FAQ?s and Interview Questions Java SE   14 Days ago
User Image
Hemanth18

Newbie

Joined: Sep 7, 2018

Points: 100

Threads: 3

Posts: 8

i have text with some text i want to count duplicate words in file and print that duplicate words

| Quote Date : Nov 5, 2018    Views:110    

how can we achieve this using java.?

i have text with some text i want to find /count duplicate words in file and print that duplicate words from file





Comments

User Image
Amritk

Newbie

Joined : May 8, 2017

Points : 230

Threads: 57

Posts: 214

Re: i have text with some text i want to count duplicate words in file and print that duplicate words

Reply Date : Nov 9, 2018

there are multiple way to find out duplicate words in File.
see below..

public Map<String, Integer> getWordCount(String fileName){
 
        FileInputStream fis = null;
        DataInputStream dis = null;
        BufferedReader br = null;
        Map<String, Integer> wordMap = new HashMap<String, Integer>();
        try {
            fis = new FileInputStream(fileName);
            dis = new DataInputStream(fis);
            br = new BufferedReader(new InputStreamReader(dis));
            String line = null;
            while((line = br.readLine()) != null){
                StringTokenizer st = new StringTokenizer(line, " ");
                while(st.hasMoreTokens()){
                    String tmp = st.nextToken().toLowerCase();
                    if(wordMap.containsKey(tmp)){
                        wordMap.put(tmp, wordMap.get(tmp)+1);
                    } else {
                        wordMap.put(tmp, 1);
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try{if(br != null) br.close();}catch(Exception ex){}
        }
        return wordMap;
    }
     
    public List<Entry<String, Integer>> sortByValue(Map<String, Integer> wordMap){
         
        Set<Entry<String, Integer>> set = wordMap.entrySet();
        List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
        Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
        {
            public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
            {
                return (o2.getValue()).compareTo( o1.getValue() );
            }
        } );
        return list;
    }
     
    public static void main(String a[]){
        MaxDuplicateWordCount mdc = new MaxDuplicateWordCount();
        Map<String, Integer> wordMap = mdc.getWordCount("C:/MyTestFile.txt");
        List<Entry<String, Integer>> list = mdc.sortByValue(wordMap);
        for(Map.Entry<String, Integer> entry:list){
            System.out.println(entry.getKey()+" ==== "+entry.getValue());
        }
    };

User Image
satya1995

Newbie

Joined : Feb 21, 2018

Points : 100

Threads: 11

Posts: 18

Re: i have text with some text i want to count duplicate words in file and print that duplicate words

Reply Date : Nov 8, 2018

you want to print all the duplicate words or you want to take user input and find the duplicate is there or not?

Load More

No More Comments