仅打印 java 中重复的单词

print only repeated words in java

我只想显示在字符串中出现多次的单词,不应打印字符串的单个出现。我还想打印长度大于 2 的字符串(以消除 is、was、the 等)。

我试过的代码..打印所有字符串并显示是出现次数..

代码:

public static void main(String args[])
{
    Map<String, Integer> wordcheck = new TreeMap<String, Integer>();
    String string1="world world is new world of kingdom of palace of kings palace";
    String string2[]=string1.split(" ");


    for (int i=0; i<string2.length; i++) 

     {
        String string=string2[i];
        wordcheck.put(string,(wordcheck.get(string) == null?1:   (wordcheck.get(string)+1)));

      }

    System.out.println(wordcheck);


}

输出:

{is=1, kingdom=1, kings=1, new=1, of=3, palace=2, world=3}

不应打印单独出现的字符串... 我还想打印长度大于 2 的字符串(以消除 is、was、the 等)。

TreeMap.toString() 继承自 AbstractMap 并且文档指出

Returns a string representation of this map. The string representation consists of a list of key-value mappings in the order returned by the map's entrySet view's iterator, enclosed in braces ("{}"). Adjacent mappings are separated by the characters ", " (comma and space). Each key-value mapping is rendered as the key followed by an equals sign ("=") followed by the associated value. Keys and values are converted to strings as by String.valueOf(Object).

最好您编写自己的方法以您想要的方式打印出 TreeMap。

public static void main(String args[])
{
    Map<String, Integer> wordcheck = new TreeMap<String, Integer>();
    String string1="world world is new world of kingdom of palace of kings palace";
    String string2[]=string1.split(" ");
    HashSet<String> set = new HashSet<String>();

    for (int i=0; i<string2.length; i++) 

     {
        String data=string2[i];
       for(int j=0;j<string2.length;j++)
       {
           if(i != j)
           {
            if(data.equalsIgnoreCase(string2[j]))
            {

                set.add(data);

            }
           }
       }

      }

    System.out.println("Duplicate word size :"+set.size());
    System.out.println("Duplicate words :"+set);


}

跟踪地图中出现的次数将使您能够做到这一点。

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class Test1
{
    public static void main(String[] args)
    {
        String string1="world world is new world of kingdom of palace of kings palace";
        String string2[]=string1.split(" ");
        HashMap<String, Integer> uniques = new HashMap<String, Integer>();
        for (String word : string2)
        {
            // ignore words 2 or less characters long
            if (word.length() <= 2)
            {
                continue;
            }
            // add or update the word occurrence count
            Integer existingCount = uniques.get(word);
            uniques.put(word, (existingCount == null ? 1 : (existingCount + 1)));
        }

        Set<Entry<String, Integer>> uniqueSet = uniques.entrySet();
        boolean first = true;
        for (Entry<String, Integer> entry : uniqueSet)
        {
            if (entry.getValue() > 1)
            {
                System.out.print((first ? "" : ", ") + entry.getKey() + "=" + entry.getValue());
                first = false;
            }
        }

    }
}

要仅获取出现多次的单词,您必须过滤地图。

根据您的 Java 版本,您可以使用以下任一种:

List<String> wordsOccuringMultipleTimes = new LinkedList<String>();
for (Map.Entry<String, Integer> singleWord : wordcheck.entrySet()) {
    if (singleWord.getValue() > 1) {
        wordsOccuringMultipleTimes.add(singleWord.getKey());
    }
}

或以 Java 8 这个等效的 Lambda 表达式开头:

List<String> wordsOccuringMultipleTimes = wordcheck.entrySet().stream()
        .filter((entry) -> entry.getValue() > 1)
        .map((entry) -> entry.getKey())
        .collect(Collectors.toList());

关于漂亮的打印,您必须在迭代结果时做类似的事情。

使用它

for (String key : wordcheck.keySet()) {

        if(wordcheck.get(key)>1)
            System.out.println(key + " " + wordcheck.get(key));
}

使用下面的代码

for (String key : wordcheck.keySet()) {

    if(wordcheck.get(key)>1)
        System.out.println(key + " " + wordcheck.get(key));

}