小编Joh*_*Doe的帖子

Java 流按键查找值(如果存在)

我有简单的 DataStructure

public class DataStructure {
    private String key;
    private String value;
    //get, set
}
Run Code Online (Sandbox Code Playgroud)

我需要根据键从`List'返回值,我想用流来做Java8的方式。我认为代码不言自明:

public class Main {
    public static void main(String args[]) {
      List<DataStructure> dataList = new ArrayList<>();
      dataList.add(new DataStructure("first", "123"));
      dataList.add(new DataStructure("second", "456"));

        System.out.println(findValueOldSchool(dataList, "third")); //works ok
        System.out.println(findValueStream(dataList, "third")); //throws NoSuchElementException
    }

    static String findValueOldSchool(List<DataStructure> list, String key) {
        for (DataStructure ds : list) {
            if (key.equals(ds.getKey())) {
                return ds.getValue();
            }
        }
        return null;
    }

    static String findValueStream(List<DataStructure> list, String key) {
        return list.stream()
                .filter(ds -> …
Run Code Online (Sandbox Code Playgroud)

java java-stream

2
推荐指数
1
解决办法
2292
查看次数

Swagger codegen 生成重复的变量

我正在尝试从包含以下内容的 yaml 生成客户端

  acceptParam:
    name: Accept
    type: string
    required: true
    in: header
    description: Accepted Content-type. Should be set to application/json
  contentTypeParam:
    name: Content-Type
    type: string
    required: true
    in: header
    description: Request Content-type. Should be set to application/json
Run Code Online (Sandbox Code Playgroud)

这意味着acceptcontentType将出现在生成的方法签名中。

最重要的是,我配置了这样的插件

<plugin>
    <groupId>io.swagger.codegen.v3</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>3.0.18</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/swagger.yaml</inputSpec>
                <language>java</language>
                <configOptions>
                    <dateLibrary>joda</dateLibrary>
                    <localVarPrefix>localVar</localVarPrefix>
                </configOptions>
                <library>resttemplate</library>
                <output>${project.build.directory}/generated-sources</output>
                <modelPackage>com.example.client.model</modelPackage>
                <apiPackage>com.example.client.api</apiPackage>
                <generateApiTests>false</generateApiTests>
                <generateModelTests>false</generateModelTests>
            </configuration>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

尽管如此,之后mvn clean install

我越来越

错误:(130,31)java:变量accept已在方法中定义

生成的代码包含

public Response authorize(Request …
Run Code Online (Sandbox Code Playgroud)

java yaml http swagger swagger-codegen

2
推荐指数
1
解决办法
3138
查看次数

标签 统计

java ×2

http ×1

java-stream ×1

swagger ×1

swagger-codegen ×1

yaml ×1