dushkin:
私はすべての真のオブジェクトをフィルタリングするには、このコードを思い付いたが、私はそれもすっきり持つことができますか?intstreamとブール鋳造は私かゆみされています...
import org.json.JSONArray;
import org.json.JSONObject;
JSONArray arr = new JSONArray();
arr.put(false);
arr.put(true);
arr.put(false);
//JSONArray is a list of objects
boolean b = IntStream.range(0, arr.length())
.filter(index -> ((boolean) arr.get(index)) == true)
.findAny()
.isPresent();
私が使う
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
依存。それが最良の選択ですか?
ありがとう!
ダニ・Mesejo:
あなたは使用することができIntStreamをしgetBoolean:
JSONArray arr = new JSONArray();
arr.put(false);
arr.put(true);
arr.put(false);
//JSONArray is a list of objects
boolean b = IntStream.range(0, arr.length())
.anyMatch(arr::getBoolean);
System.out.println(b);
出力
true
別の方法として次のように使用できます。
boolean b = StreamSupport.stream(arr.spliterator(), false)
.anyMatch(Boolean.TRUE::equals);
上記の解決策は、それが返し例えば、JSONExceptionの問題を処理しtrue
、次の入力のために:
JSONArray arr = new JSONArray();
arr.put(false);
arr.put(1);
arr.put(true);