简体   繁体   English

如何将 HashMap 添加到员工列表 class

[英]How to add HashMap to List of Employee class

I have created a Java class and using JPA to retrieving details of Employee class which is returning list of employees like我创建了一个 Java class 并使用 JPA 检索员工 ZA2F2ED4F8EBC2CBB1DZC21 的详细信息

[
  {
    "empId": "1",
    "empName": "emp1",
    "empTag": "empTag1",
    "empState": "ACTIVE"
  },
  {
    "empId": "2",
    "empName": "emp2",
    "empTag": "empTag2",
    "empState": "ACTIVE"
  }
]

After receiving list of hash I want to add few dummy entries like在收到 hash 的列表后,我想添加一些虚拟条目,例如

[
  {
    "empId": "00",
    "empName": "DummyEmp",
    "empTag": "DummyEmpTag",
    "empState": "ACTIVE"
  },
  {
    "empId": "1",
    "empName": "emp1",
    "empTag": "empTag1",
    "empState": "ACTIVE"
  },
  {
    "empId": "2",
    "empName": "emp2",
    "empTag": "empTag2",
    "empState": "ACTIVE"
  }
]

So I am trying to add map into list of employee but at the run time I am getting所以我试图将 map 添加到员工列表中,但在运行时我得到了

java.lang.ClassCastException: java.util.HashMap$KeySet cannot be cast to com.domain.EmployeeBean 

My Employee Class:我的员工 Class:

@Table(name = "employee")
@Entity
public class EmployeeBean implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id @Column(name = "emp_id") 
    private String empId;
    
    @Column(name = "emp_name") 
    private String empName;
    
    @Column(name = "emp_tag") 
    private String empTag;
    
    @Column(name = "emp_state") 
    private String empState;

    .... (getters and setters) 
}

Repository:-存储库:-

public interface EmployeeBeanRepository extends JpaRepository<EmployeeBean, String>{

    List < EmployeeBean > findByEmpState(String empState);

Resource:-资源:-

@RestController
@RequestMapping("/empDetails")
public class EmployeeBeanResource {
    
private final EmployeeBeanRepository empBeanRepository;
    
    public EmployeeBeanResource(EmployeeBeanRepository empBeanRepository) {
        this.empBeanRepository = empBeanRepository;
    }
        
    @GetMapping("/empState/{empState}")
    @Timed
    public List<EmployeeBean> getEmployeesByStatus(@PathVariable String empState) {
        List<EmployeeBean> activeEmployeeList = empBeanRepository.findByEmpState(empState);
        
        Map<String, String> empDummy = new HashMap<>();
        
        empDummy.put("empId", "00");
        empDummy.put("empName", "DummyEmp");
        empDummy.put("empTag", "DummyEmpTag");
        empDummy.put("empState", "ACTIVE");
        
        activeEmployeeList.add((EmployeeBean) empDummy.keySet());
        activeEmployeeList.add((EmployeeBean) empDummy.values());
       
        return activeEmployeeList;
    }
}

I have tried a lot but still unable to figure out a way to add map in to list of employee.我已经尝试了很多,但仍然无法找到将 map 添加到员工列表中的方法。

Instead of this:而不是这个:

Map<String, String> empDummy = new HashMap<>();
        
empDummy.put("empId", "00");
empDummy.put("empName", "DummyEmp");
empDummy.put("empTag", "DummyEmpTag");
empDummy.put("empState", "ACTIVE");
        
activeEmployeeList.add((EmployeeBean) empDummy.keySet());

try the following:尝试以下操作:

EmployeeBean empDummy = new EmployeeBean();
        
empDummy.setEmpId("00");
empDummy.setEmpName("DummyEmp");
empDummy.setEmpTag("DummyEmpTag");
empDummy.setEmpState("ACTIVE");
        
activeEmployeeList.add(empDummy);

or if you have all args constructor:或者如果你有所有 args 构造函数:

EmployeeBean empDummy = new EmployeeBean("00", "DummyEmp","DummyEmpTag","ACTIVE");

activeEmployeeList.add(empDummy);

如何将元素添加到 Object,List 的 hashmap<object><div id="text_translate"><p> 我有一个来自 dao 的列表,我想把这个列表放在一个 HashMap&gt; 中,我的列表可以包含一个服务,它有多个参数,比如 serviceId=3。 在我的最终 HashMap 中,结果如下所示: {Service 1=[100,A],Service 2=[101,A],Service 3=[Parameter[102,B],Parameter[103,B],Parameter[104,C]]} 。</p><pre> serviceId paramId type 1 100 A 2 101 A 3 102 B 3 103 B 3 104 C</pre><p> 服务.java</p><pre> private int id; //Getters+Setters</pre><p> 参数.java</p><pre> private int id; private String type; //Getters+Setters</pre><p> 测试.java</p><pre> List result = dao.getServiceParam(); HashMap&lt;Service,List&lt;Parameter&gt;&gt; mapList = new HashMap&lt;Service, List&lt;Parameter&gt;&gt;(); if(.result;isEmpty()) { for (int i=0. i&lt; result;size(). i++) { Object[] line = (Object[])result;get(i); if ((BigDecimal) line[0]!=null) { } } }</pre></div></object> - How to add element to hashmap of Object,List<Object>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用“员工类别”作为值的哈希图 - hashmap using a “employee class” as the value 可以使用员工 class 作为 HashMap 中的键吗? - Can one use an Employee class as a key in a HashMap? 如何检查哈希图中所有员工的姓名? - How to check if all employee in hashmap have name? 如何添加HashMap <Object, String> 在实体类? - How to add a HashMap<Object, String> in an entity class? 如何根据hashMap的值将类的对象添加到hashMap? - how to add objects of class to hashMap based on their values? 如何将HashMap值添加到列表并进行打印 - How to add HashMap values to List and print the same 如何将参数列表从JSON添加到HashMap? - How to add list of parameters from JSON to HashMap? 如何将元素添加到 Object,List 的 hashmap<object><div id="text_translate"><p> 我有一个来自 dao 的列表,我想把这个列表放在一个 HashMap&gt; 中,我的列表可以包含一个服务,它有多个参数,比如 serviceId=3。 在我的最终 HashMap 中,结果如下所示: {Service 1=[100,A],Service 2=[101,A],Service 3=[Parameter[102,B],Parameter[103,B],Parameter[104,C]]} 。</p><pre> serviceId paramId type 1 100 A 2 101 A 3 102 B 3 103 B 3 104 C</pre><p> 服务.java</p><pre> private int id; //Getters+Setters</pre><p> 参数.java</p><pre> private int id; private String type; //Getters+Setters</pre><p> 测试.java</p><pre> List result = dao.getServiceParam(); HashMap&lt;Service,List&lt;Parameter&gt;&gt; mapList = new HashMap&lt;Service, List&lt;Parameter&gt;&gt;(); if(.result;isEmpty()) { for (int i=0. i&lt; result;size(). i++) { Object[] line = (Object[])result;get(i); if ((BigDecimal) line[0]!=null) { } } }</pre></div></object> - How to add element to hashmap of Object,List<Object> 如何添加项目以列出内部哈希图? - How to add an item to list inner hashmap? 如何将空列表添加到 HashMap Java 中的值 - How to add a empty list to the values in a HashMap Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM