字符串组合(长度> 3)在给定ArrayList中出现的次数

时间:2018-04-16 08:14:52

标签: java string arraylist hashmap

我想找到给定输入中字符串组合(长度大于3)的次数。

input:
scientists found way to reduce global warming
scientists, found way to minimize water pollution
scientists said that they are successful
Rony said that they are successful
johnny said that he failed

desired output:
scientists found-2
said that-3

"科学家发现"在第1和第2声明中,

"说"在第3,第4和第5声明,

"他们成功了#34;不包含在""的长度中。不超过3。

我已将程序划分为块并添加了对这些块正在做什么的评论,如何获得所需的输出?是否有更有效的解决方案??

package project1;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class combo{
//----------Block 1 starts---------------------------------------
  public static void main(String args[]) {
    ArrayList<String> exampleList = new ArrayList<>();
    exampleList.add("scientists found way to reduce global warming".toLowerCase());
    exampleList.add("scientists, found way to minimize water pollution".toLowerCase());
    exampleList.add("scientists, said that they are successful".toLowerCase());
   exampleList.add("Rony, said that they are successful".toLowerCase());
   exampleList.add("johnny, said that he failed".toLowerCase());

Map<String, Integer> keywordList = new HashMap<String, Integer>();
ArrayList<String> strmatch=new ArrayList<>();
for(int i=0;i<exampleList.size();i++){
    String[] tokens = exampleList.get(i).split("[ ,-;()//:']");
    for (String token : tokens)
    {
        if(token.length()>3){
        if(!keywordList.containsKey(token))
            keywordList.put(token,1);
        else{
            keywordList.put(token,keywordList.get(token)+1);
        }
        }
    }
      for (int j=0;j<tokens.length;j++)//content of tokens array 
        {
        System.out.println(tokens[j]);  //to check content of tokens.
        }
}
//------------Block 1 ends---------------------------------------

//content of keywordList
/*for (String name: keywordList.keySet()){

            String key =name.toString();
            String value = keywordList.get(name).toString();  
            System.out.println(key + " " + value); //to check keywordList content. 
} */
//------------Block 2 starts-------------------------------------
System.out.println(keywordList.size());
Iterator it = keywordList.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry pair = (Map.Entry)it.next();
    if((int)pair.getValue()<2)
        it.remove();
    System.out.println(pair.getKey() + " = " + pair.getValue()); /*to get 
                  content of keywordList which are repeated more than once.*/  
}
//-----------Block 2 ends--------------------------------------
//-----------Block 3 starts------------------------------------
 it = keywordList.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry pair = (Map.Entry)it.next();
    System.out.println(pair.getKey() + " ::" + pair.getValue());
    strmatch.add((String)pair.getKey());
}
//-----------Block 3 ends----------------------------------------

//-----------Block 4 starts--------------------------------------

System.out.println(strmatch);//content of strmatch
String[] str= new String[strmatch.size()];
//int[][] variable2=new int[keywordList.size()][keywordList.size()];
for(int i=0;i<exampleList.size();i++){
    for(int j=0;j<strmatch.size();j++)
        for (int k=0;k<strmatch.size();k++){
            if(j==k)
                continue;
            if(exampleList.get(i).contains(strmatch.get(j))&&exampleList.get(i).contains(strmatch.get(k)))
                str[i]=strmatch.get(j)+" "+strmatch.get(k);
        }
}
//-----------Block 4 ends----------------------------------------

for(int p=0;p<strmatch.size();p++)//contents of str array
    {
     System.out.println(str[p]);   //to get desired output
    }
}

0 个答案:

没有答案