Java 如何加入地图
如何加入Java 如何加入地图,java,collections,map,Java,Collections,Map,如何加入custMap中的newMap详细信息 Map<String, Customer> custMap= new HashMap<String,Customer>(); Map<String, DoCustomer> newMap= new HashMap<String,DoCustomer>(); for (Map.Entry<String, DoCustomer> cust: newMap.entrySet(
custMap
中的newMap
详细信息
Map<String, Customer> custMap= new HashMap<String,Customer>();
Map<String, DoCustomer> newMap= new HashMap<String,DoCustomer>();
for (Map.Entry<String, DoCustomer> cust: newMap.entrySet()) {
custMap.put(cust.getKey(),cust.getValue());
}
public class DoCustomer {
private Long id;
private String custName;
private String description;
private String status;
private List<DoCustomerBranch> doCustomerBranch=new ArrayList<DoCustomerBranch>
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
getter/setters of doCustomerBranch
}
@Entity
@Table(name = "CUSTOMER")
public class Customer implements Serializable{
private static final long serialVersionUID = 1L;
private Long id;
private String custName;
private String description;
private String createdBy;
private Date createdOn;
private String updatedBy;
private Date updatedOn;
private Set<CustomerBranch> customerBranch=new HashSet<CustomerBranch>
@Id
@GeneratedValue(generator = "CUSTOMER_SEQ")
@SequenceGenerator(name = "CUSTOMER_SEQ", sequenceName = "CUSTOMERN_SEQ", allocationSize = 1)
@Column(name = "ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "CUST_NAME",nullable=false)
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
@Column(name = "DESCRIPTION")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Column(name = "CREATED_BY", length = 50)
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "CREATED_ON")
public Date getCreatedOn() {
return createdOn;
}
public void setCreatedOn(Date createdOn) {
this.createdOn = createdOn;
}
@Column(name = "UPDATED_BY", length = 50)
public String getUpdatedBy() {
return updatedBy;
}
public void setUpdatedBy(String updatedBy) {
this.updatedBy = updatedBy;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "UPDATED_ON")
public Date getUpdatedOn() {
return updatedOn;
}
public void setUpdatedOn(Date updatedOn) {
this.updatedOn = updatedOn;
}
@OneToMany(cascade = { CascadeType.PERSIST, CascadeType.REMOVE }, fetch = FetchType.LAZY, mappedBy = "customer")
public Set<CustomerBranch> getCustomerBranch() {
return customerBranch;
}
public void setCustomerBranch(Set<CustomerBranch> customerBranch) {
this.customerBranch = customerBranch;
}
}
你是说:
custMap.putAll(newMap)
代码的问题在于,您希望将
DoCustomer
放入Customer
容器中。只有当DoCustomer
是Customer
的子类时,它才起作用
编辑1:您可以使用
BeanUtils
将DoCustomer
转换为客户
。这是一个很好的教程。正如其他人所指出的,我们需要知道DoCustomer能够提供哪些帮助
但是,根据您给我们的信息,我建议将每个客户都分配给一个客户,或者更准确地说,从每个客户的领域中创建一个新客户
比如:
custMap.put(cust.getKey(), new Customer(cust.getValue().getId(), cust.getValue().getCustName(), and so on..));
在for循环中
我可以看到您提供的定义的customer类没有构造函数,因此您自然需要向它添加一个构造函数您所说的
join
是什么意思?您的示例代码有什么问题?它能做你想要的吗?我想将newMap值迁移到custMap。我得到的错误是类型映射中的方法put(String,Customer)不适用于参数(String,DoCustomer)你能发布Customer和DoCustomer类的定义吗?谢谢,DoCustomer不是Customer的子类,在本例中,客户是实体,DocCustomer是客户的属性,您能告诉我此sol的另一种方式吗,是否可以使用BeanUtils…..是的,我想要此sol,但我得到的错误是类型映射中的方法put(String,Customer)不适用于参数(String,DocCustomer)根据类DoCustomer
的名称,我假设它是Customer
类的一个子类,但我犯了一个错误。因此,我提出的方法不适合您的情况。谢谢Michael,您写的是,但在我的客户实体中,是由CustomerBranch映射的,当时我是,我有带成本结构的pblm,您指的是Set CustomerBranch吗?此字段是否与DoCustomer中的List doCustomerBranch相同?我可以理解,在两者之间进行转换会使这成为一个非常冗长的解决方案。custBranchesList是列表的列表类型,CustBranchChangesList是列表的列表类型。是否可以加入custBranchesList.add(CustBranchChangesList)…这与您原来的问题相同;只有当您愿意使用相同的技术将它们从DoCustomerBranch转换为CustomerBranch时,才能这样做。
custMap.put(cust.getKey(), new Customer(cust.getValue().getId(), cust.getValue().getCustName(), and so on..));