java 8 json_如何在Java 8中遍历JSONArray

我有以下代码,它使用for loop来遍历中的元素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;

}

}

我如何使用Java 8like中的新功能streams API或forEach执行相同任务。提前致谢

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值