欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java 字符串詞頻統(tǒng)計(jì)實(shí)例代碼

 更新時(shí)間:2013年03月30日 09:51:29   作者:  
java 字符串詞頻統(tǒng)計(jì)實(shí)例代碼,需要的朋友可以參考一下

復(fù)制代碼 代碼如下:

package com.gpdi.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WordsStatistics {

    class Obj {
        int count ;
        Obj(int count){
            this.count = count;
        }
    }

    public List<WordCount> statistics(String word) {
        List<WordCount> rs = new ArrayList<WordCount>();
        Map <String,Obj> map = new HashMap<String,Obj>();

        if(word == null ) {
            return null;
        }
        word = word.toLowerCase();
        word = word.replaceAll("'s", "");
        word = word.replaceAll(",", "");
        word = word.replaceAll("-", "");
        word = word.replaceAll("\\.", "");
        word = word.replaceAll("'", "");
        word = word.replaceAll(":", "");
        word = word.replaceAll("!", "");
        word = word.replaceAll("\n", "");

        String [] wordArray = word.split(" ");
        for(String simpleWord : wordArray) {
            simpleWord = simpleWord.trim(); 
            if (simpleWord != null && !simpleWord.equalsIgnoreCase("")) {
                Obj cnt = map.get(simpleWord);
                if ( cnt!= null ) {
                    cnt.count++;
                }else {
                    map.put(simpleWord, new Obj(1));
                }
            }
        }

        for(String key : map.keySet()) {
            WordCount wd = new WordCount(key,map.get(key).count);
            rs.add(wd);
        }

        Collections.sort(rs, new java.util.Comparator<WordCount>(){
            @Override
            public int compare(WordCount o1, WordCount o2) {
                int result = 0 ;
                if (o1.getCount() > o2.getCount() ) {
                    result = -1;
                }else if (o1.getCount() < o2.getCount()) {
                    result = 1;
                }else {
                    int strRs = o1.getWord().compareToIgnoreCase(o2.getWord());
                    if ( strRs > 0 ) {
                        result = 1;
                    }else {
                        result = -1 ;
                    }
                }
                return result;
            }

        });
        return rs;
    }

     
    public static void main(String args[]) {
        String word = "Pinterest is might be aa ab aa ab marketer's dream  - ths site is largely used to curate products " ;
        WordsStatistics s = new WordsStatistics();
        List<WordCount> rs = s.statistics(word);
        for(WordCount word1 : rs) {
            System.out.println(word1.getWord()+"*"+word1.getCount());
        }
    }

}

相關(guān)文章

  • Java GC垃圾回收算法分析

    Java GC垃圾回收算法分析

    垃圾回收機(jī)制簡(jiǎn)稱(chēng)GC,主要用于Java堆的管理。在JVM中程序計(jì)數(shù)器、虛擬機(jī)棧、本地方法棧生命周期隨跟隨線(xiàn)程,棧幀的進(jìn)棧和入棧能實(shí)現(xiàn)自動(dòng)清理。而 jdk8后元空間為本地內(nèi)存也不受GC控制,所以垃圾回收主要是在堆中
    2022-12-12
  • 詳解在Spring Boot中使用Https

    詳解在Spring Boot中使用Https

    本篇文章主要介紹了詳解在Spring Boot中使用Https,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05
  • java中多個(gè)@Scheduled定時(shí)器不執(zhí)行的解決方法

    java中多個(gè)@Scheduled定時(shí)器不執(zhí)行的解決方法

    在應(yīng)用開(kāi)發(fā)中經(jīng)常需要一些周期性的操作,比如每5分鐘執(zhí)行某一操作等,這篇文章主要給大家介紹了關(guān)于java中多個(gè)@Scheduled定時(shí)器不執(zhí)行的解決方法,需要的朋友可以參考下
    2023-04-04
  • 詳解Java中類(lèi)的加載順序

    詳解Java中類(lèi)的加載順序

    Java中什么時(shí)候類(lèi)加載,第一次需要使用類(lèi)信息時(shí)加載。類(lèi)加載的原則:延遲加載,能不加載就不加載。下面這篇文章主要介紹了Java中類(lèi)的加載順序,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-01-01
  • Java中MyBatis Plus知識(shí)點(diǎn)總結(jié)

    Java中MyBatis Plus知識(shí)點(diǎn)總結(jié)

    在本篇文章里小編給大家整理一篇關(guān)于Java中MyBatis Plus知識(shí)點(diǎn)總結(jié),需要的朋友們參考下。
    2019-10-10
  • java 如何遠(yuǎn)程控制tomcat啟動(dòng)關(guān)機(jī)

    java 如何遠(yuǎn)程控制tomcat啟動(dòng)關(guān)機(jī)

    這篇文章主要介紹了java 遠(yuǎn)程控制tomcat啟動(dòng)關(guān)機(jī)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-04-04
  • JAVA操作HDFS案例的簡(jiǎn)單實(shí)現(xiàn)

    JAVA操作HDFS案例的簡(jiǎn)單實(shí)現(xiàn)

    本篇文章主要介紹了JAVA操作HDFS案例的簡(jiǎn)單實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08
  • SpringBoot引入Redis報(bào)Redis?command?timed?out兩種異常情況

    SpringBoot引入Redis報(bào)Redis?command?timed?out兩種異常情況

    這篇文章主要給大家介紹了關(guān)于SpringBoot引入Redis報(bào)Redis?command?timed?out兩種異常情況的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-08-08
  • JAXB命名空間及前綴_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    JAXB命名空間及前綴_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要給大家介紹了關(guān)于JAXB命名空間及前綴的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • 在idea中將java項(xiàng)目中的單個(gè)類(lèi)打包成jar包操作

    在idea中將java項(xiàng)目中的單個(gè)類(lèi)打包成jar包操作

    這篇文章主要介紹了在idea中將java項(xiàng)目中的單個(gè)類(lèi)打包成jar包操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-08-08

最新評(píng)論