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

Java 如何对读取多个文件后收到的计数进行排序,java,Java,我是Java新手,需要使用HashMap或TreeMap进行排序的帮助。我需要显示与特定计算机名相对应的断开连接总数。用户输入显示计算机名和计数的日期范围。数据从包含以下数据的多个文件中读取: 文件1 文件2: [C445] ComputerName:FRONTOFFICE UserID:YB Yenae Ball Station 7A LanId: | (11/23 17:01:55) | Client is disconnected from agent. [C445] ComputerN

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

文件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]);
    }}

你不能按照条目的值对地图中的条目进行排序,除非有真正邪恶、肮脏和不安全的黑客。也就是说…我还发现你缺乏泛型令人不安

你最好的办法是做类似的事情

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());
    }
  });

这基本上就是你已经在做的事情-因此,基本上,你已经有了正确的方法来做这件事。

你不能对地图中的条目进行排序。。。关于:。您应该检查比较方法中的条目是否正确null@HovercraftFullOfEels:对不起,我的意思是按值排序。@LuiggiMendoza:我们从一个已经初始化的映射中获取映射项;根据OP初始化映射的方式,不可能有空条目或空值。@LouisWasserman:我没有按排序顺序获取计数,也无法从一个文件到另一个文件获取合并计数。
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());
    }
  });