在数组列表对象中查找字数

时间:2014-12-16 17:04:02

标签: java

我需要计算存储在数组列表中的不同String对象的重复单词......

我尝试了以下代码:

import java.io.*;
import java.util.*;
public class Runcmd {


    public static void main(String[] args) {
        ArrayList arrList=new ArrayList();
        try
        {
            Process pr=Runtime.getRuntime().exec("netstat -an");
            BufferedReader rd= new BufferedReader(new InputStreamReader(pr.getInputStream()));
            String line=rd.readLine();
            while(line!=null)
            {
                arrList.add(line);
                line=rd.readLine();
            }
            Iterator it=arrList.iterator();
            while(it.hasNext())
            {
                String sent=(String)it.next();
                System.out.println(sent);
            }
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }

    }

}

我得到了这个输出:

Active Connections

  Proto  Local Address          Foreign Address        State
  TCP    0.0.0.0:135            0.0.0.0:0              LISTENING
  TCP    0.0.0.0:445            0.0.0.0:0              LISTENING
  TCP    0.0.0.0:3306           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49152          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49153          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49154          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49155          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49156          0.0.0.0:0              LISTENING
  TCP    10.50.170.205:139      0.0.0.0:0              LISTENING
  TCP    10.50.170.205:49904    74.125.236.86:443      ESTABLISHED
  TCP    10.50.170.205:50044    23.201.102.42:80       TIME_WAIT
  TCP    10.50.170.205:50045    23.201.102.42:80       TIME_WAIT
  TCP    10.50.170.205:50046    23.201.102.42:80       TIME_WAIT
  TCP    10.50.170.205:50047    23.201.102.42:80       TIME_WAIT
  TCP    10.50.170.205:50048    23.57.214.140:80       TIME_WAIT
  TCP    10.50.170.205:50049    23.57.214.140:80       TIME_WAIT
  TCP    10.50.170.205:50050    23.57.214.140:80       TIME_WAIT
  TCP    10.50.170.205:50051    23.57.214.140:80       TIME_WAIT
  TCP    10.50.170.205:50052    23.201.102.33:80       TIME_WAIT
  TCP    10.50.170.205:50053    23.57.214.140:80       TIME_WAIT
  TCP    [::]:135               [::]:0                 LISTENING
  TCP    [::]:445               [::]:0                 LISTENING
  TCP    [::]:3306              [::]:0                 LISTENING
  TCP    [::]:49152             [::]:0                 LISTENING
  TCP    [::]:49153             [::]:0                 LISTENING
  TCP    [::]:49154             [::]:0                 LISTENING
  TCP    [::]:49155             [::]:0                 LISTENING
  TCP    [::]:49156             [::]:0                 LISTENING
  UDP    0.0.0.0:5355           *:*                    
  UDP    10.50.170.205:137      *:*                    
  UDP    10.50.170.205:138      *:*                    
  UDP    10.50.170.205:1900     *:*                    
  UDP    127.0.0.1:1900         *:*                    
  UDP    127.0.0.1:49397        *:*                    
  UDP    [::1]:1900             *:*                    
  UDP    [::1]:49396            *:* 

现在我想获取出现次数的字数列表(LISTENING,TIME-WAIT,ESTABLISHED) 例如:

LISTENING : 17
TIME-WAIT : 10
ESTABLISHED : 1

请告诉我如上所述的想法......

3 个答案:

答案 0 :(得分:5)

定义一些计数变量并在打印时检查字符串...

        while(it.hasNext())
        {
            String sent=(String)it.next();
            System.out.println(sent);

            if (sent.indexOf("LISTENING") > 0) listening ++;
            if (sent.indexOf("TIME-WAIT") > 0) wait++;
            if (sent.indexOf("ESTABLISHED") > 0) established ++;

        }

答案 1 :(得分:1)

关于downvoting的风险,使用bash

可以更轻松地做到这一点
#!/bin/bash
res=$(netstat -an)
n=$(echo "$res" | grep 'LISTENING' | wc -l)
echo "LISTENING : $n"
n=$(echo "$res" | grep 'TIME-WAIT' | wc -l)
echo "TIME-WAIT : $n"
n=$(echo "$res" | grep 'ESTABLISHED' | wc -l)
echo "ESTABLISHED : $n"

甚至更紧凑(也更灵活):

#!/bin/bash
res=$(netstat -an)
for tp in {LISTENING,TIME-WAIT,ESTABLISHED}
do
    n=$(echo "$res" | grep -c "$tp")
    echo "$tp : $n"
done

答案 2 :(得分:0)

您只需要有三个类别的运行计数,然后在最后打印出来。

try
    {
        Process pr=Runtime.getRuntime().exec("netstat -an");
        BufferedReader rd= new BufferedReader(new InputStreamReader(pr.getInputStream()));
        String line=rd.readLine();
        while(line!=null)
        {
            arrList.add(line);
            if(line.contains("LISTENING")) listeningCount++;
            if(line.contains("TIME_WAIT")) waitingCount++;
            if(line.contains("ESTABLISHED")) establishedCount++;
            line=rd.readLine();
        }
        Iterator it=arrList.iterator();
        while(it.hasNext())
        {
            String sent=(String)it.next();
            System.out.println(sent);
        }
        System.out.println("LISTENING = " + listeningCount);
        System.out.println("TIME_WAIT = " + waitingCount);
        System.out.println("ESTABLISHED = " + establishedCount);
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }