查找字符串中重复的单词并计算重复次数
我需要找到字符串上重复的单词,然后计算它们重复的次数。所以基本上,如果输入字符串是这样的:
String s = "House, House, House, Dog, Dog, Dog, Dog";
我需要创建一个不重复的新字符串列表,并将每个单词的重复次数保存在其他地方,如下所示:
New String: "House, Dog"
New Int Array: [3, 4]
有没有办法用Java轻松做到这一点?我已经设法使用 s.split() 分隔字符串,但是如何计算重复次数并消除新字符串上的重复次数?谢谢!
I need to find repeated words on a string, and then count how many times they were repeated. So basically, if the input string is this:
String s = "House, House, House, Dog, Dog, Dog, Dog";
I need to create a new string list without repetitions and save somewhere else the amount of repetitions for each word, like such:
New String: "House, Dog"
New Int Array: [3, 4]
Is there a way to do this easily with Java? I've managed to separate the string using s.split() but then how do I count repetitions and eliminate them on the new string? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(29)
你已经完成了艰苦的工作。现在您可以使用
Map
来计算出现次数:使用
map.get(word)
会告诉您某个单词出现的次数。您可以通过迭代map.keySet()
来构造一个新列表:请注意,您从
keySet
中获取的内容的顺序是任意的。如果您需要根据单词首次出现在输入字符串中的时间对其进行排序,则应使用LinkedHashMap
。You've got the hard work done. Now you can just use a
Map
to count the occurrences:Using
map.get(word)
will tell you many times a word occurred. You can construct a new list by iterating throughmap.keySet()
:Note that the order of what you get out of
keySet
is arbitrary. If you need the words to be sorted by when they first appear in your input String, you should use aLinkedHashMap
instead.试试这个,
}
Try this,
}
正如其他人提到的,使用 String::split(),后跟一些映射(hashmap 或 linkedhashmap),然后合并结果。为了完整起见,放置代码。
新字符串是
Output = House, Dog
Int 数组(或更确切地说是列表)
Values = [3, 4]
(您可以使用 List::toArray)来获取数组。As mentioned by others use String::split(), followed by some map (hashmap or linkedhashmap) and then merge your result. For completeness sake putting the code.
New String is
Output = House, Dog
Int array (or rather list)
Values = [3, 4]
(you can use List::toArray) for getting an array.使用 java8
另外,如果您想保留插入顺序,请使用 LinkedHashMap
输出
Using java8
Also, use a LinkedHashMap if you want to preserve the order of insertion
Output
如果这是作业,那么我只能说:使用
String.split()
和HashMap
。(我发现你已经找到了 split() 。那么你就走对了。)
If this is a homework, then all I can say is: use
String.split()
andHashMap<String,Integer>
.(I see you've found split() already. You're along the right lines then.)
它可能会以某种方式帮助你。
It may help you somehow.
一旦你从字符串中获取了单词,事情就很容易了。
从 Java 10 开始,您可以尝试以下代码:
输出:
Once you have got the words from the string it is easy.
From Java 10 onwards you can try the following code:
Output:
您可以使用前缀树 (trie) 数据结构来存储单词并跟踪前缀树节点内的单词计数。
插入所有单词后,您必须打印单词并通过迭代 Maxheap 进行计数。
You can use Prefix tree (trie) data structure to store words and keep track of count of words within Prefix Tree Node.
After inserting all words, you have to print word and count by iterating Maxheap.
如果您传递一个字符串参数,它将计算每个单词
输出的重复次数:
If you pass a String argument it will count the repetition of each word
output:
我希望这会对您有所帮助
public void countInPara(String str) {
I hope this will help you
public void countInPara(String str) {
输入:这就是它是什么,这就是它可以
输出:
[这,是,什么,它,是,这,是,什么,它,可以, be]
{can=1, What=2, be=1, this=2, is=3, it=2}
[1, 2, 1, 2, 3, 2]
[can, What, be, this, is , 它]
Input: this is what it is this is what it can be
Output:
[this, is, what, it, is, this, is, what, it, can, be]
{can=1, what=2, be=1, this=2, is=3, it=2}
[1, 2, 1, 2, 3, 2]
[can, what, be, this, is, it]
}
}
请使用下面的代码。根据我的分析,这是最简单的。希望你会喜欢它:
Please use the below code. It is the most simplest as per my analysis. Hope you will like it:
}
}
对于没有空格的字符串,我们可以使用下面提到的代码
将一些输入传递为“hahaha”或“ba na na”或“xxxyyyzzzxxxzzz”给出所需的输出。
For Strings with no space, we can use the below mentioned code
Passing some input as "hahaha" or "ba na na" or "xxxyyyzzzxxxzzz" give the desired output.
希望这有帮助:
Hope this helps :
使用 Java 8 流 收集器:
输入:
“House,House,House,Dog,Dog,Dog,Dog,Cat”< /code>
输出:
{Cat=1,House=3,Dog=4}
Using Java 8 streams collectors:
Input:
"House, House, House, Dog, Dog, Dog, Dog, Cat"
Output:
{Cat=1, House=3, Dog=4}
请尝试这些可能对您有帮助。
please try these it may be help for you.
因为流的引入改变了我们编码的方式;我想添加一些使用它来做到这一点的方法
as introduction of stream has changed the way we code; i would like to add some of the ways of doing this using it
在 Collectors.groupingBy 中使用 Function.identity() 并将所有内容存储在 MAP 中。
在Python中,我们可以使用collections.Counter()
输出:
“Roopa”重复2次。
“Roopi”重复了两次。
“颜色”重复1次。
“绿色”重复 1 次。
“爱”重复了1次。
Use Function.identity() inside Collectors.groupingBy and store everything in a MAP.
In Python we can use collections.Counter()
Output :
"Roopa" is repeated 2 times.
"Roopi" is repeated 2 times.
"color" is repeated 1 time.
"green" is repeated 1 time.
"loves" is repeated 1 time.