最近在项目中遇到这样一个问题:在给一个list对象循环赋值时,该list对象有个bean,且该bean对象有个机构list对象,在赋值时一定要反复执行:organList = this.findOrgan(); 否则最终结果会一样!这里的organList我只是赋初始值,并不像每记录一下单证信息就要重新查询一下机构赋值,这样会很影响这个报表的效率。(希望哪位可以指教一二,多谢了 O(∩_∩)O~ )
package dco;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Test {
public Test() {
List<OrganBean> organList= this.findOrgan();
List<String []> documentList= this.findDocuemnt();
Map<String, DocumentBean> map = new HashMap<String, DocumentBean>();
for (Iterator iterator = documentList.iterator(); iterator.hasNext();) {
String [] document = (String[]) iterator.next();
DocumentBean db = (DocumentBean) map.get(document[0]);
if( db == null ){
//organList = this.findOrgan();
db = new DocumentBean();
db.setDocumentId(document[0]);
db.setDocumentName(document[0]);
db.setOrganList(organList);
map.put(document[0], db);
}
List<OrganBean> docOrganList = db.getOrganList() ;
for (Iterator iterator2 = docOrganList.iterator(); iterator2.hasNext();) {
OrganBean organBean = (OrganBean) iterator2.next();
if( organBean.getOrganId().equals(document[1])){
organBean.setOrganCount(organBean.getOrganCount() + Integer.parseInt(document[2]));
}
}
}
Set<String> set = map.keySet() ;
for (Iterator iterator = set.iterator(); iterator.hasNext();) {
String documentId = (String) iterator.next();
DocumentBean db = (DocumentBean) map.get(documentId);
List<OrganBean> docOrganList = db.getOrganList() ;
String info = "" ;
for (Iterator iterator2 = docOrganList.iterator(); iterator2.hasNext();) {
OrganBean organBean = (OrganBean) iterator2.next();
info += ",["+organBean.getOrganId()+"],["+organBean.getOrganCount()+"] ";
}
System.out.println("[" +db.getDocumentId()+ "]"+ info);
}
}
/**
* 机构信息
*
* @return
*/
public List<OrganBean> findOrgan(){
List<OrganBean> organList = new ArrayList<OrganBean>();
OrganBean bean1 = new OrganBean();
bean1.setOrganId("A10");
bean1.setOrganName("长沙");
OrganBean bean2 = new OrganBean();
bean2.setOrganId("B10");
bean2.setOrganName("益阳");
OrganBean bean3 = new OrganBean();
bean3.setOrganId("C10");
bean3.setOrganName("安化");
organList.add(bean1);
organList.add(bean2);
organList.add(bean3);
return organList ;
}
/**
* 单证信息
* @return
*/
public List<String []> findDocuemnt(){
List<String []> organList = new ArrayList<String []>();
organList.add(new String[]{"CF2012A","A10","20"});
organList.add(new String[]{"CF2012B","C10","30"});
organList.add(new String[]{"CF2012A","B10","50"});
organList.add(new String[]{"CF2012A","B10","20"});
return organList ;
}
public static void main(String[] args) {
new Test();
}
}
以上中会用到两个实体类:DocumentBean.java 与 OrganBean.java
public class DocumentBean {
private StringdocumentId= "" ;
private StringdocumentName= "" ;
private List<OrganBean> organList= null ;
....
}
public class OrganBean {
private StringorganId = "" ;
private StringorganName = "" ;
private intorganCount = 0 ;
....
}