[英]print only repeated words in java
我只想显示在一个字符串中出现多次的单词,不应该打印单个外观的字符串。 我也想打印长度大于2的字符串(以消除是,等等)。
我尝试过的代码..打印所有字符串并显示为出现次数。
码:
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等)。
用它
for (String key : wordcheck.keySet()) {
if(wordcheck.get(key)>1)
System.out.println(key + " " + wordcheck.get(key));
}
跟踪地图中的出现次数将使您能够做到这一点。
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));
}
TreeMap.toString()从AbstractMap继承而来,文档指出
返回此映射的字符串表示形式。 字符串表示形式由键值映射列表组成,这些键值映射由映射的entrySet视图的迭代器返回,并用大括号(“ {}”)括起来 。 相邻的映射用字符“,”(逗号和空格)分隔。 每个键值映射均表示为键,后跟等号(“ =”),后跟关联值。 键和值通过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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.