2017-03-17 3 views
3

다음 코드는 for loop을 사용하여 JSONArray의 요소를 반복합니다.Java 8에서 JSONArray를 반복하는 방법

import org.apache.log4j.Logger; 
import org.json.JSONArray; 
import org.json.JSONObject; 

import java.util.stream.IntStream; 
    public class Pmt { 
    private String[] patchInformation_svnRevisionpublic; 
    private final Logger logger = Logger.getLogger(Pmt.class.getName()); 

     private static final String COMMITS_IN_PATCH_IDENTIFIER = "patchInformation_svnRevisionpublic"; //key used to identify the commits in a patch from JSON response received from PMT 
     private static final String KEY_STRING = "name"; 
     private static final String VALUE_STRING = "value"; 

     public String[] getPublicGitCommitHashes(JSONArray jsonArray) { 

      for (int i = 0; i < jsonArray.length(); i++) { 
       JSONObject jsonObject = (JSONObject) jsonArray.get(i); 
       String tempName = (String) jsonObject.get(KEY_STRING); 
       if (tempName.equals(COMMITS_IN_PATCH_IDENTIFIER)) { 
        JSONArray tempCommitsJSONArray = (JSONArray) jsonObject.get(VALUE_STRING); 
        //initializing the patchInformation_svnRevisionpublic array 
        patchInformation_svnRevisionpublic = new String[tempCommitsJSONArray.length()]; 
        // for ommiting the white spaces at the begingin and end of the commits 
        IntStream.range(0, tempCommitsJSONArray.length()).forEach(j -> patchInformation_svnRevisionpublic[j] = ((String) tempCommitsJSONArray.get(j)).trim()); 

        logger.info(" The commits hashes obtained from WSO2 PMT are successfully saved to an array"); 

        System.out.println("The commit Ids are"); 
        //   for printing all the commits ID associated with a patch 
        IntStream.range(0, patchInformation_svnRevisionpublic.length).mapToObj(i1 -> patchInformation_svnRevisionpublic[i1]).forEach(System.out::println); 
        System.out.println(); 
        break; 
       } 
      } 
      //to prevent from internaal representation by returning referecnce to mutable object 
      String clonedPatchInformation_svnRevisionpublic[] = patchInformation_svnRevisionpublic.clone(); 
      return clonedPatchInformation_svnRevisionpublic; 
     } 
    } 

어떻게 같은 작업을 수행 할 streams API 또는 forEach 같은 Java 8의 새로운 기능을 사용합니까. 미리 감사드립니다.

+2

JSONArray에 사용중인 가져 오기 - 어떤 라이브러리를 게시하지 않으셨습니까? –

+0

@DmitryZvorygin은 –

답변

6

자바 8 스트림 API와 동일하거나 코드입니다. 100 %는 아니지만 주요 아이디어를 얻을 수 있습니다.

private static final String COMMITS_IN_PATCH_IDENTIFIER = "patchInformation_svnRevisionpublic"; //key used to identify the commits in a patch from JSON response received from PMT 
private static final String KEY_STRING = "name"; 
private static final String VALUE_STRING = "value"; 

public List<String> getCommitIds (JSONArray array) { 
    return arrayToStream(array) 
      .map(JSONObject.class::cast) 
      .filter(o -> o.get(KEY_STRING).equals(COMMITS_IN_PATCH_IDENTIFIER)) 
      .findFirst() 
      .map(o -> (JSONArray) o.get(VALUE_STRING)) 
      .map(Main::arrayToStream) 
      .map(commits -> 
        commits.map(Object::toString) 
          .map(String::trim) 
          .collect(Collectors.toList()) 
      ) 
      .orElseGet(Collections::emptyList); 
} 

@Nonnull 
private static Stream<Object> arrayToStream(JSONArray array) { 
    return StreamSupport.stream(array.spliterator(), false); 
} 
+0

질문에 대한 관련 수입을 추가 했습니까?이 방법을 사용하여 distinct()를 어떻게 사용할 수 있습니까? JSONObject는 equals보다는 similar를 사용하여 비교하는 것 같습니다. – Myoch

관련 문제