对于项目中的一些东西的记录 map ,js foreach遍历 JsonArray一些对象转换方法

1.map 集合用法:强烈推荐网址     https://blog.csdn.net/yen_csdn/article/details/52886677  map详解

存放:

Map<String, Long> map = new LinkedHashMap<String, Long>();
for(Object[] obj : countList){
Long value = StringUtils.string2long(obj[0].toString(),0);
String key = StringUtils.string2long(obj[1].toString(),0)+"";
map.put(key,value);
}

读取:
for(int i=0;i<itemWrapperList.size();i++){
for (Map.Entry<String, Long> m :map.entrySet()) {
if(itemWrapperList.get(i).getItemId() == Long.parseLong(m.getKey())){
itemWrapperList.get(i).setTotalCount(Integer.parseInt(m.getValue().toString()));
}
}
}


2.js 中数组array遍历 foreach
items 为数组
items.forEach(function(item, index){
console.info(item);
item.index = index+1;
// 如果是量表题,计算平均分
var t = 0, c=0;
if(item.itemType == 4){
item.optionList.forEach(function(element){
var count = element.count;
t += (parseInt(element.title)*count);
c += count;
});
var avg = (c > 0)? t/c : 0.0;
item.avg = avg.toFixed(1);
}
缺点:不能中断(比如中断之后执行某一操作),只能进行到底


3.关于List集合中的对象引用问题
List<AccountStatWrapper> asWrapper = new ArrayList<AccountStatWrapper>();
AccountStatWrapper asw = new AccountStatWrapper();
asw.setBalanceFlow(s.getBalanceFlow());
asw.setBalanceSpace(s.getBalanceSpace());
asw.setBalanceDuration(s.getBalanceDuration());
asWrapper.add(asw);

/**
* 已用流量
*/
List<FeeLog> feeLogList = feeLogDaoInterface.feeLogListBySponsorId(s.getSponId());
Long flowNum=0L,spaceNum=0L,durationNum=0L;
if(feeLogList==null || feeLogList.size()==0) return asWrapper;
for(FeeLog feeLog : feeLogList){
flowNum+=feeLog.getFlowNum();
spaceNum+=feeLog.getSpaceNum();
durationNum+=feeLog.getDurationNum();
}
asw.setBalanceFlow(flowNum);
asw.setBalanceDuration(durationNum);
asw.setBalanceSpace(spaceNum);
asWrapper.add(asw);
注:asw已经被加入到list集合里面,但此时只是存放的引用对象,不是本身,因此如果再次重新赋值,就会使得这个对象的值改变
其实add加进去的是对象的一个引用,对象实例化放在循环外面,你每次都更新了这个引用的值,当然list里面的值都一样的。如果把User user = new User();放到循环里面,每次都生成一个新的对象,更改自身不会对其他元素有影响....


/***
/**
/*
public Long saveAllOfExam(Exam exam, String itemJson, String optionsJson, Long organId) {
Exam exam4 = null;
exam4 = judgeMent(exam,organId);
if(exam4 == null) return null;
Exam examNew = examDaoInterface.save(exam4);
Long examId = examNew.getExamId();
if(itemJson ==null || itemJson.isEmpty()) return examId;
JSONArray itemJsonArray = JSONArray.fromObject(itemJson);
JSONArray optionsJsonArray = JSONArray.fromObject(optionsJson);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JSONObject itemJsonObject;
JSONObject optionJsonObject;
for (int i = 0; i < itemJsonArray.size(); i++) {
String itemTitle = null;
Long itemId = null;
itemJsonObject = (JSONObject) itemJsonArray.get(i);
try {
Item item = objectMapper.readValue(itemJsonObject.toString(), Item.class);
item.setExamId(examId);
if(item.getItemId()==null || item.getItemId()==-1 || item.getItemId()==0){
item.setItemId(null);
}
Item itemNew = itemDaoInterface.save(item);
itemTitle = itemNew.getTitle();
itemId = itemNew.getItemId();
} catch (IOException e) {
e.printStackTrace();
}
for(int j = 0; j<optionsJsonArray.size(); j++){
optionJsonObject = (JSONObject) optionsJsonArray.get(j);
String title = optionJsonObject.get("father").toString();
if(title.equals(itemTitle)){
try {
ItemOption itemOption = objectMapper.readValue(optionJsonObject.toString(), ItemOption.class);
if(itemOption.getOptionId()==null || itemOption.getOptionId().equals("on") || itemOption.getOptionId()==-1){
itemOption.setOptionId(null);
}
itemOption.setItemId(itemId);
itemOptionDaoInterface.save(itemOption);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
}
return examId;
}











posted @ 2018-02-24 14:46  塑料味的美年达  阅读(363)  评论(0编辑  收藏  举报