首页 > 解决方案 > 将字符串列表传递给声明的类文件

问题描述

大家好,下面是我的Employeeinformation类文件java。我必须通过java代码将值传递给它我很好地传递字符串,但我无法传递技术列表和字符串列表。

技术.java

package com.ElasticSearchCrud.ElasticSearchCrud;

import lombok.Data;

@Data
public class Technologies {

    private String name;
    private  String yearsOfExperience;

}

员工信息.java

package com.ElasticSearchCrud.ElasticSearchCrud;

import lombok.Data;

import java.util.List;

@Data
public class EmployeeInformation {
    private String id;
    private String firstName;
    private String lastName;
    private List<Technologies> technologies;
    private List<String> emails;


    public EmployeeInformation(String id, String firstName, String lastName, List<Technologies> technologies, List<String> emails) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.technologies = technologies;
        this.emails = emails;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public List<Technologies> getTechnologies() {
        return technologies;
    }

    public void setTechnologies(List<Technologies> technologies) {
        this.technologies = technologies;
    }

    public List<String> getEmails() {
        return emails;
    }

    public void setEmails(List<String> emails) {
        this.emails = emails;
    }
}

将值传递给类文件

EmployeeInformation EmployeePost = new EmployeeInformation("3", "Vignesh", "Murali","?","?");

如何在上面的代码中传递技术列表和字符串列表。有人可以帮助我实现这一目标吗?

标签: java

解决方案


List<Technologies> technologies = new ArrayList<>();
    technologies.add(new Technologies("name1", "1999"));
    technologies.add(new Technologies("name2", "1999"));

    List<String> emails = new ArrayList<>();
    strs.add("one");
    strs.add("two");

    EmployeeInformation EmployeePost = new EmployeeInformation("3", "Vignesh", "Murali",technologies,emails);

还将@AllArgsConstructor 添加到技术类。或者使用 setter 来初始化 Technologies 对象:

@Data
@AllArgsConstructor
public class Technologies {

    private String name;
    private  String yearsOfExperience;

}

推荐阅读