操纵字符串

时间:2011-01-10 01:09:39

标签: java string

我试图在字符串中找到最重复的单词,使用以下代码:

public class Word 
{
    private String toWord;
    private int Count;

    public Word(int count, String word){
        toWord = word;
        Count = count;
    }

    public static void main(String args[]){
        String str="my name is neo and my other name is also neo because I am neo";
        String []str1=str.split(" ");
        Word w1=new Word(0,str1[0]);
        LinkedList<Word> list = new LinkedList<Word>();
        list.add(w1);
        ListIterator itr = list.listIterator();
        for(int i=1;i<str1.length;i++){
            while(itr.hasNext()){
                if(str1[i].equalsTO(????));
                else
                    list.add(new Word(0,str1[i]));
            }
        }
    }
}

如何将String Array str1中的String与链表中存储的String进行比较,然后如何增加相应的计数。

然后我将打印出具有最高计数的字符串, 我也不知道怎么做。

6 个答案:

答案 0 :(得分:4)

我建议使用HashMap而不是链表。

Iterate through the string.
For each word,
Check if the word is in the Map,
If it is there increment count and
Otherwise insert with count 1

答案 1 :(得分:0)

你需要将每个单词存储到一个列表中,也许是一个带有计数变量的long,表示该单词的使用次数。

对于每个单词,如果计数已经在列表中,则递增计数,如果不是,则将其添加到列表中。

答案 2 :(得分:0)

C#?您可以尝试使用LINQ GroupBy然后使用Count或Max - 非常简单。

答案 3 :(得分:0)

使用Google Guava

Multiset<String> words = HashMultiset.create(Splitter.on(" ").split(input));

然后

String topWord = words.isEmpty() ? null 
    : Iterables.get(Ordering.natural().immutableSortedCopy(words), 0);

您可以使用words.count(topWord)获取热门字词的频率。

答案 4 :(得分:0)

我认为你可以在这里使用一些正则表达式,如

    final String str = "my name is neo and my other name is also neo because I am neo";

    final String[] arr = str.split (" ");
    final Set <String> set = new HashSet <String> ();
    for (final String word : arr) {
        System.out.println ("arr " + word);
        set.add (word);
    }

    String preWord = "";
    int preCount = 0;
    for (final String word : set) {
        System.out.println ("----------------");

        final Pattern p2 = Pattern.compile ("\\b" + word + "\\b");
        final Matcher m2 = p2.matcher (str);
        int count = 0;

        while (m2.find ()) {
            count++;
        }

        System.out.println ("preCount " + preWord + ":" + word + ":" + preCount + ":" + count);

        if ((preCount < count)) {
            preWord = word;
            preCount = count;
            System.out.println ("assigning word " + word + ":" + count);
        }
    }

    System.out.println ("result " + preWord + ":" + preCount);

答案 5 :(得分:0)

使用Apache Commons StringUtils org.apache.commons.lang.StringUtils来获取计数。

String str="my name is neo and my other name is also neo because I am neo";
// Make a unique list (java.util.Set) of words.
Set<String> stSet = new HashSet<String>(Arrays.asList(str.split(" ")));
int sz = stSet.size();
int[] counts = new int[sz];
Map<Integer,String> matches = new HashMap<Integer,String>(sz);
int i = 0;
for (String s : stSet) {
   // saves the individual word count in a sortable array.
   counts[i] = StringUtils.countMatches(str,s));
   // saves the word count and the word in a HashMap for easy retrieval.
   matches.put(counts[i],s);
   i++;
}
Arrays.sort(counts);
int max = counts.length - 1;
System.out.println("The the word with the most occurrances is: "+matches.get(counts[max])+", the number of occurrances is: "+counts[max]);