gpt4 book ai didi

java - java 中 Stream 的类型安全原始类型

转载 作者:行者123 更新时间:2023-11-30 05:20:34 24 4
gpt4 key购买 nike

我正在使用流/数组将 JSONArray 转换为 String[]。json使用的库是:jsonsimple

其中的一行如下:

import org.json.simple.parser.*;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

// other stuff

try (FileReader reader = new FileReader(
"/home/hduser/eclipse-workspace/db-simulatiob/src/generator-config.json")) {

// Get config json
JSONObject obj = (JSONObject) jsonParser.parse(reader);
JSONObject aTags = (JSONObject) obj.get("assembling-tags");
String[] assemblingTags = (String[]) ((JSONArray) aTags.get("list")).stream().toArray(String[]::new); <---- one liner

但它返回警告:

Type safety: The method toArray(IntFunction) belongs to the raw type Stream. References to generic type Stream should be parameterized

根据this similar post ,我必须给出一个明确的类型,好吧。

由于无法弄清楚单行,我将单行一分为三,并试图抑制这个新版本上的警告。到目前为止我有这个:

    JSONArray temp = (JSONArray) aTags.get("list");
Stream<JSONArray> ts = temp.stream(); <=== warning here
String[] tss = (String[]) ts.toArray(String[]::new);

但它仍然在 temp.stream(); 的第二行返回相同的警告。

如何修复代码以抑制警告?

最佳答案

假设您的代码有效,并且您确定这是您想要的,您尝试过吗?

@SuppressWarnings("unchecked")
String[] tss = (String[]) ts.toArray(String[]::new);

关于java - java 中 Stream 的类型安全原始类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59648871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com