如何对读取多个文件后收到的计数进行排序

时间:2012-03-31 19:14:26

标签: java

我是Java的新手,需要帮助才能使用HashMap或TreeMap进行排序。我需要显示总数的数量。对应于特定计算机名称的断开连接。用户输入显示计算机名称和计数的日期范围。从包含以下数据的多个文件中读取数据: 文件1

[C417] ComputerName:KCUTSHALL-PC UserID:GO kcutshall Station 9900 (locked) LanId: | (11/23 11:36:13) | Client is disconnected from agent.
[C417] ComputerName:KCUTSHALL-PC UserID:GO kcutshall Station 9900 (locked) LanId: | (11/23 11:36:13) | Client is connected to agent

文件2:

[C445] ComputerName:FRONTOFFICE UserID:YB Yenae Ball Station 7A  LanId: | (11/23 17:01:55) | Client is disconnected from agent.
[C445] ComputerName:KCUTSHALL-PC UserID:GO kcutshall Station 9900 (locked) LanId: | (11/23 17:02:00) | Client is disconnected from agent.

需要输出:

Computer Name    No. of disconnects
KCUTSHALL-PC          2
FRONTOFFICE           1

没有。断开连接应该按降序排列。我尝试使用HashMap但没有得到所需的输出。请编辑我的代码或告诉我如何在阅读多个文件时获得合并计数。提前做好了很多。这是我写的

enter code here
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.Scanner;
import java.util.*;
import java.text.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;


public class ReadZip{
  public static void main(String args[]) throws ParseException {
    try {
Scanner input1=new Scanner(System.in);

   Scanner input2=new Scanner(System.in);
   System.out.println("Enter start date");
    String userDate1=input1.nextLine();
    System.out.println("Enter end date");
     String userDate2=input2.nextLine();
DateFormat df = new SimpleDateFormat ("MM/dd");
      Date d1=df.parse(userDate1);
      Date d2=df.parse(userDate2);

      ZipFile zf=new ZipFile("C:\\Users\\Engineeir\\Desktop\\QoS_logs.zip");
      Enumeration entries=zf.entries();

      BufferedReader input=new BufferedReader(new InputStreamReader(
          System.in));
      while (entries.hasMoreElements()) {
        ZipEntry ze=(ZipEntry) entries.nextElement();

     BufferedReader br=new BufferedReader(new InputStreamReader(zf.getInputStream(ze)));
        String line; String name;String compnames;int lines=0;
        while ((line=br.readLine())!=null) {

       String[] st=line.split("\\|",-1);
if(st.length>1){
String dates=st[1];
 String[] parts=dates.split("-");
 SimpleDateFormat f=new SimpleDateFormat("(MM/dd");
String[] ob=parts[0].split(" ");
String finaldate=ob[1];
HashMap first=new HashMap(); 
Date d3=f.parse(finaldate);
if((d3.after(d1) && d3.before(d2)) && line.contains("Client is disconnected from agent")==true)
{
compnames=getName(st);
lines++;}

else{break;}
first.put(compnames,lines);
ArrayList as=new ArrayList(first.entrySet());  

        Collections.sort(as,new Comparator(){  
            public int compare(Object o1,Object o2)  
            {  
                Map.Entry e1=(Map.Entry)o1 ;  
                Map.Entry e2=(Map.Entry)o2 ;  
                Integer first=(Integer)e1.getValue();  
                Integer second=(Integer)e2.getValue();  
                return second.compareTo(first);  
            }  
  });
  Iterator i=as.iterator();  
        while(i.hasNext())  
        {  
            System.out.println((Map.Entry)i.next());  
        }

}



       //br.close();
}  }  }catch (IOException e) {
      e.printStackTrace();
    }}

      public static String getName(String[] st)
    {
       String[] name=st[0].split("\\:",-1);
       String[] comp=name[1].split("\\ ",-1);

        return(comp[0]);
    }}

1 个答案:

答案 0 :(得分:0)

您无法按照Map的值对其中的条目进行排序,而不是没有真正邪恶,肮脏和不安全的黑客攻击。 (那就是说......我也发现你缺乏仿制药令人不安。)

最好的办法是做一些像

这样的事情
List<Map.Entry<String, Integer>> entries =
  new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
Collections.sort(entries, Collections.reverseOrder( // for descending order
  new Comparator<Map.Entry<String, Integer>>() {
    public int compare(
        Map.Entry<String, Integer> entry1,
        Map.Entry<String, Integer> entry2) {
      return entry1.getValue().compareTo(entry2.getValue());
    }
  });

这基本上就是你已经在做的事情 - 所以基本上,你已经有了正确的方法来做到这一点。