我已经制作了一个应用程序,在给定关键字时打印500条最新推文,例如: “猫”。
我想使用带有正则表达式的tTrunc =
0 0.0300 0.0400 0.0500
yTrunc =
1 0 1 0
拆分推文中的所有单词,然后使用String.split()
存储每个单词并使用循环来递增每个单词的Integer值,每个单词在推文中提及的时间。
答案 0 :(得分:1)
您可以使用Java 8 Streams:
String[] words = tweetText.split(" ");
Map<String, Integer> wordCount = Arrays.stream(words)
.collect(Collectors.toMap(word -> word, word -> 1, Integer::sum));
如果你想这样做是不敏感的:
Map<String, Integer> wordCount = Arrays.stream(words)
.map(String::toLowerCase)
.collect(Collectors.toMap(word -> word, word -> 1, Integer::sum));
仅查找特定字词:
Map<String, Integer> wordCount = Arrays.stream(words)
.map(String::toLowerCase)
.filter(word -> wordsToMatch.contains(word))
.collect(Collectors.toMap(word -> word, word -> 1, Integer::sum));
其中wordsToMatch可以是您要查找的单词集或列表。
答案 1 :(得分:0)
应该是这样的:
]] Root cause of ServletException.
java.lang.RuntimeException: Evaluation of expression [${userItem.deviceBody.key}] failed due to reflection errors.
at com.tdk.ergonomics.taglib.support.TagAttributeUtils.contextSpecificEvaluate(TagAttributeUtils.java:94)
at com.tdk.ergonomics.taglib.datagrid.ColumnTag.evaluateValueFromExpression(ColumnTag.java:415)
at com.tdk.ergonomics.taglib.datagrid.ColumnTag.doEndTag(ColumnTag.java:441)
at jsp_servlet._web_45_inf._jsp._accounts.__listcbbody._jsp__tag6(__listcbbody.java:575)
at jsp_servlet._web_45_inf._jsp._accounts.__listcbbody._jsp__tag5(__listcbbody.java:503)
at jsp_servlet._web_45_inf._jsp._accounts.__listcbbody._jsp__tag4(__listcbbody.java:452)
at jsp_servlet._web_45_inf._jsp._accounts.__listcbbody._jsp__tag1(__listcbbody.java:309)
at jsp_servlet._web_45_inf._jsp._accounts.__listcbbody._jspService(__listcbbody.java:225)
at weblogic.servlet.jsp.JspBase.service(JspBase.java:34)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:341)
at weblogic.servlet.internal.ServletStubImpl.onAddToMapException(ServletStubImpl.java:478)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:367)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:238)
at weblogic.servlet.internal.RequestDispatcherImpl.invokeServlet(RequestDispatcherImpl.java:573)
at weblogic.servlet.internal.RequestDispatcherImpl.include(RequestDispatcherImpl.java:480)
at org.apache.taglibs.standard.tag.common.core.ImportSupport.acquireString(ImportSupport.java:347)
at org.apache.taglibs.standard.tag.common.core.ImportSupport.doEndTag(ImportSupport.java:204)
at jsp_servlet._web_45_inf._jsp._template.__template._jsp__tag2(__template.java:365)
at jsp_servlet._web_45_inf._jsp._template.__template._jspService(__template.java:247)
at weblogic.servlet.jsp.JspBase.service(JspBase.java:34)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:341)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:238)
at weblogic.servlet.internal.RequestDispatcherImpl.invokeServlet(RequestDispatcherImpl.java:573)
at weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:272)
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:264)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:844)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:844)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:341)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:25)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at com.tdk.ergonomics.taglib.datagrid.export.ExcelExportFilter.doFilter(ExcelExportFilter.java:39)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at com.tdk.ergonomics.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:27)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at com.tdk.ergonomics.filter.EventDispatcherFilter.doFilter(EventDispatcherFilter.java:38)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at com.tdk.ergonomics.filter.RequestUrlFilter.doFilter(RequestUrlFilter.java:31)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at com.tdk.env.ecolabel.support.context.EcolabelSecurityFilter.doFilter(EcolabelSecurityFilter.java:82)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:27)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3367)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3333)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2220)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2146)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2124)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1564)
at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:254)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:295)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:254)
Caused By: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.commons.beanutils.MethodUtils.invokeMethod(MethodUtils.java:216)
at com.tdk.ergonomics.taglib.support.TagAttributeUtils.contextSpecificEvaluate(TagAttributeUtils.java:92)
at com.tdk.ergonomics.taglib.datagrid.ColumnTag.evaluateValueFromExpression(ColumnTag.java:415)
at com.tdk.ergonomics.taglib.datagrid.ColumnTag.doEndTag(ColumnTag.java:441)
at jsp_servlet._web_45_inf._jsp._accounts.__listcbbody._jsp__tag6(__listcbbody.java:575)
at jsp_servlet._web_45_inf._jsp._accounts.__listcbbody._jsp__tag5(__listcbbody.java:503)
at jsp_servlet._web_45_inf._jsp._accounts.__listcbbody._jsp__tag4(__listcbbody.java:452)
at jsp_servlet._web_45_inf._jsp._accounts.__listcbbody._jsp__tag1(__listcbbody.java:309)
at jsp_servlet._web_45_inf._jsp._accounts.__listcbbody._jspService(__listcbbody.java:225)
at weblogic.servlet.jsp.JspBase.service(JspBase.java:34)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:341)
at weblogic.servlet.internal.ServletStubImpl.onAddToMapException(ServletStubImpl.java:478)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:367)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:238)
at weblogic.servlet.internal.RequestDispatcherImpl.invokeServlet(RequestDispatcherImpl.java:573)
at weblogic.servlet.internal.RequestDispatcherImpl.include(RequestDispatcherImpl.java:480)
at org.apache.taglibs.standard.tag.common.core.ImportSupport.acquireString(ImportSupport.java:347)
at org.apache.taglibs.standard.tag.common.core.ImportSupport.doEndTag(ImportSupport.java:204)
at jsp_servlet._web_45_inf._jsp._template.__template._jsp__tag2(__template.java:365)
at jsp_servlet._web_45_inf._jsp._template.__template._jspService(__template.java:247)
at weblogic.servlet.jsp.JspBase.service(JspBase.java:34)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:341)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:238)
at weblogic.servlet.internal.RequestDispatcherImpl.invokeServlet(RequestDispatcherImpl.java:573)
at weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:272)
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:264)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:844)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:844)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:341)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:25)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at com.tdk.ergonomics.taglib.datagrid.export.ExcelExportFilter.doFilter(ExcelExportFilter.java:39)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at com.tdk.ergonomics.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:27)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at com.tdk.ergonomics.filter.EventDispatcherFilter.doFilter(EventDispatcherFilter.java:38)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at com.tdk.ergonomics.filter.RequestUrlFilter.doFilter(RequestUrlFilter.java:31)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at com.tdk.env.ecolabel.support.context.EcolabelSecurityFilter.doFilter(EcolabelSecurityFilter.java:82)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:27)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3367)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3333)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2220)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2146)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2124)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1564)
at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:254)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:295)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:254)
Caused By: javax.servlet.jsp.el.ELException
at weblogic.jsp.internal.jsp.el.ArrayRefEvaluator.evaluateBeanType(ArrayRefEvaluator.java:252)
at weblogic.jsp.internal.jsp.el.ArrayRefEvaluator.evaluate(ArrayRefEvaluator.java:49)
at weblogic.jsp.internal.jsp.el.ELNode$RefExpression.evaluate(ELNode.java:494)
at weblogic.jsp.internal.jsp.el.ExpressionEvaluatorImpl$ELExpression.evaluate(ExpressionEvaluatorImpl.java:188)
at weblogic.jsp.internal.jsp.el.ExpressionEvaluatorImpl.evaluate(ExpressionEvaluatorImpl.java:126)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.commons.beanutils.MethodUtils.invokeMethod(MethodUtils.java:216)
at com.tdk.ergonomics.taglib.support.TagAttributeUtils.contextSpecificEvaluate(TagAttributeUtils.java:92)
at com.tdk.ergonomics.taglib.datagrid.ColumnTag.evaluateValueFromExpression(ColumnTag.java:415)
at com.tdk.ergonomics.taglib.datagrid.ColumnTag.doEndTag(ColumnTag.java:441)
at jsp_servlet._web_45_inf._jsp._accounts.__listcbbody._jsp__tag6(__listcbbody.java:575)
at jsp_servlet._web_45_inf._jsp._accounts.__listcbbody._jsp__tag5(__listcbbody.java:503)
at jsp_servlet._web_45_inf._jsp._accounts.__listcbbody._jsp__tag4(__listcbbody.java:452)
at jsp_servlet._web_45_inf._jsp._accounts.__listcbbody._jsp__tag1(__listcbbody.java:309)
at jsp_servlet._web_45_inf._jsp._accounts.__listcbbody._jspService(__listcbbody.java:225)
at weblogic.servlet.jsp.JspBase.service(JspBase.java:34)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:341)
at weblogic.servlet.internal.ServletStubImpl.onAddToMapException(ServletStubImpl.java:478)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:367)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:238)
at weblogic.servlet.internal.RequestDispatcherImpl.invokeServlet(RequestDispatcherImpl.java:573)
at weblogic.servlet.internal.RequestDispatcherImpl.include(RequestDispatcherImpl.java:480)
at org.apache.taglibs.standard.tag.common.core.ImportSupport.acquireString(ImportSupport.java:347)
at org.apache.taglibs.standard.tag.common.core.ImportSupport.doEndTag(ImportSupport.java:204)
at jsp_servlet._web_45_inf._jsp._template.__template._jsp__tag2(__template.java:365)
at jsp_servlet._web_45_inf._jsp._template.__template._jspService(__template.java:247)
at weblogic.servlet.jsp.JspBase.service(JspBase.java:34)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:341)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:238)
at weblogic.servlet.internal.RequestDispatcherImpl.invokeServlet(RequestDispatcherImpl.java:573)
at weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:272)
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:264)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:844)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:844)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:341)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:25)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at com.tdk.ergonomics.taglib.datagrid.export.ExcelExportFilter.doFilter(ExcelExportFilter.java:39)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at com.tdk.ergonomics.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:27)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at com.tdk.ergonomics.filter.EventDispatcherFilter.doFilter(EventDispatcherFilter.java:38)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at com.tdk.ergonomics.filter.RequestUrlFilter.doFilter(RequestUrlFilter.java:31)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at com.tdk.env.ecolabel.support.context.EcolabelSecurityFilter.doFilter(EcolabelSecurityFilter.java:82)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:27)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3367)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3333)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2220)
for (String word : tweetText.split("\\s+")) {
wordMap.put(word, wordMap.getOrDefault(word, 0) + 1);
}
可能只适用于java 8.代码会将您的推文拆分为单独的字词,并根据您的地图计算每条推文的字数。
答案 2 :(得分:0)
我会扩展HashMap<String, Integer>
以提供使用该值作为计数器的put(key)
:
class WordCounter extends HashMap<String, Integer> {
public void put(String key) {
Integer number = this.get(key);
this.put(key, number == null ? 1 : number + 1);
}
public static void main (String[] args) throws java.lang.Exception
{
WordCounter wc = new WordCounter();
for(String word : "this is a test and this is too".split(" ")) {
wc.put(word);
}
System.out.println(wc);
}
}
输出:
{a=1, test=1, too=1, and=1, this=2, is=2}