2016-09-07 5 views
1

안녕하세요 여러분, 제 문제 할당 중 하나가 붙어 있습니다. 나는 다른 접근법을 시도했지만 여전히 그것을 할 수 없다.두 개의 TSV 파일간에 고유 한 요소 찾기

+1

당신의 접근 방식을 듣고, 왜 그들이 작동하지 않았던 지부터 시작하겠습니다. –

답변

1

이 트릭을 수행 할 것입니다. 이전 파일을 먼저 처리 한 다음 새 파일에서 기존 파일을 겹쳐 쓰십시오.

import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

... 

public class ContactsProcessor { 
    public static void main(String[] args) { 
     List<String> contactsNew = Files.readAllLines(Paths.get("contactsNew.tsv")); 
     List<String> contactsOld = Files.readAllLines(Paths.get("contactsOld.tsv")); 
     List<String> contactsGmail = new ArrayList<String>(); 
     Map<String, String> gmailMap = new HashMap<String, String>(); 

     // process old contacts first -- add to a Map 
     for (String info : contactsOld) { 
      String[] parts = info.split("\\t"); 
      if (info.endsWith("@gmail.com")) { 
       gmailMap.put(parts[0], info); 
      } 
     } 
     // process new contacts second -- add to a Map, overwriting old contacts with same name 
     for (String info : contactsNew) { 
      String[] parts = info.split("\\t"); 
      if (info.endsWith("@gmail.com")) { 
       gmailMap.put(parts[0], info); 
      } 
     } 
     contactsGmail.addAll(gmailMap.values()); 
     Collections.sort(contactsGmail); 

     Files.write(Paths.get("contactsGmail.tsv"), contactsGmail); 
    } 
} 
+0

감사합니다. Matt. 하지만 gmailMap.put (parts [2], info)가 arrayindexoutofbound 예외를 던지고 있습니다 – rahul

+0

사실 rahul은 입력 내용에 문제가있을 수 있습니다. 탭 사이의 내용 중 하나가 비어 있으면 목록이 축소되고 ArrayIndexOutOfBoundsException이 발생합니다. – byxor

+0

여러분의 도움에 감사드립니다. 하지만 위의 방법은 여전히 ​​중복 된 내용을 씁니다. – rahul

관련 문제