簡體   English   中英

如何將 HashMap 添加到員工列表 class

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

我創建了一個 Java class 並使用 JPA 檢索員工 ZA2F2ED4F8EBC2CBB1DZC21 的詳細信息

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

在收到 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"
  }
]

所以我試圖將 map 添加到員工列表中,但在運行時我得到了

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

我的員工 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) 
}

存儲庫:-

public interface EmployeeBeanRepository extends JpaRepository<EmployeeBean, String>{

    List < EmployeeBean > findByEmpState(String empState);

資源:-

@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;
    }
}

我已經嘗試了很多,但仍然無法找到將 map 添加到員工列表中的方法。

而不是這個:

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());

嘗試以下操作:

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

或者如果你有所有 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.

相關問題 使用“員工類別”作為值的哈希圖 可以使用員工 class 作為 HashMap 中的鍵嗎? 如何檢查哈希圖中所有員工的姓名? 如何添加HashMap <Object, String> 在實體類? 如何根據hashMap的值將類的對象添加到hashMap? 如何將HashMap值添加到列表並進行打印 如何將參數列表從JSON添加到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> 如何添加項目以列出內部哈希圖? 如何將空列表添加到 HashMap Java 中的值
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM