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   5 Nov, 2018 13:47
User Image
Hemanth18

Newbie

Joined: 7 Sep, 2018

Points: 100

Threads: 4

Posts: 8

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

| Quote Date : 5 Nov, 2018    Views:210    

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 : 8 May, 2017

Points : 230

Threads: 57

Posts: 216

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

Reply Date : 9 Nov, 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 : 21 Feb, 2018

Points : 110

Threads: 11

Posts: 30

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

Reply Date : 8 Nov, 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