2012-03-31 3 views
0

저는 Java를 처음 사용하고 HashMap 또는 TreeMap을 사용하여 정렬하는 데 도움이 필요합니다. 전체 수를 표시해야합니다. FILE 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 

FILE 2 :

를 특정 컴퓨터 name.User 대응 단절의 컴퓨터 이름과 개수가 같은 데이터를 포함하는 복수의 파일로부터 판독 displayed.Data이되는 기간에 진입
[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에와 일을 시도 내림차순으로해야하지만, 원하는 얻을 didnt는 내 코드를 편집하거나 여러 파일을 읽는 동안 어떻게 합산 된 수를 얻을 수 있는지 보여주십시오. 미리 감사드립니다. 여기 당신은 정말 악 더러운, 안전하지 않은 해킹하지 않고, 그 값에 의해 Map의 항목을 정렬 할 수 없습니다

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

답변

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

같은 것을 할 것입니다 - 그래서 기본적으로, 당신은 이미 이것을 할 올바른 방법이 있습니다.

+0

''지도에서 항목을 정렬 할 수 없습니다. '...'[정렬 된지도 인터페이스] (http://docs.oracle.com/javase/tutorial/collections/interfaces/sorted-map. html). –

+0

당신은'compare' 메소드의 엔트리가 null이 아닌지 확인해야합니다. –

+0

@HovercraftFullOfEels : 죄송합니다. _values_로 정렬하는 것이 좋습니다. –

관련 문제