由于,公司的试用的框架上,存在大量的hashmap的创建,产生大量的I/O 以及内存的消耗,所以,抽了点时间,看书 effective java 这本书。
同时,也从项目的作者 framework 上学习 ,吸取 ,最终写出了,符合生产的方案。
存在的问题是,一直困惑与hashmap的清除。
package com.framework.entity.factory;
import java.util.HashMap;
import org.apache.poi.ss.formula.functions.T;
import org.apache.tools.ant.types.resources.selectors.InstanceOf;
import com.framework.entity.CompanyFormMap;
import com.framework.entity.base.FormMap;
public class FrommapFactory {
private static HashMap<Class<?>, ? extends FormMap<String, Object> > hashMap=new HashMap<>();
@SuppressWarnings({ "hiding", "unchecked" })
public static <T> T getFromBeanMap(Class<? extends FormMap<String, Object>> type) {
// TODO Auto-generated method stub
if (hashMap.containsKey(type)) {
FormMap<String, Object> t=hashMap.get(type);
t.clear();
return (T)t;
}else {
Object object = null;
try {
System.out.println(type.getSimpleName());
object = Class.forName(type.getName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (T) object;
}
}
private static HashMap<T, Object> hashMap2=new HashMap<>();
@SuppressWarnings("hiding")
public static <T extends FormMap<String, Object>> T getFromBeanMap2(T t) {
t.clear();
FormMap<String, Object> formMap = (FormMap<String, Object>) t;
return t;
}
private static HashMap<T, Object> hashMap3=new HashMap<>();
@SuppressWarnings({ "unchecked", "hiding" })
public static <T> T getBeanFormMap(Class<T> clazz) throws InstantiationException, IllegalAccessException{
if (hashMap3.containsKey(clazz)) {
FormMap<String, Object> formMap = (FormMap<String, Object>) hashMap3.get(clazz);
T t=(T) hashMap3.get(clazz);
formMap.clear();
return t;
}else {
T t=clazz.newInstance();
return t;
}
}
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
CompanyFormMap companyFormMap=getBeanFormMap(CompanyFormMap.class);
System.out.println(companyFormMap);
}
}
前面第一个,是看书之后的结果。
有错误。
估计就是class 与 map ,不能有效的相互转换,或者转换太多。
最后一个就是符合方案的产品了。
其实还是可以优化的,
就是利用 T extends ?
不过,我没有测试。觉得麻烦。
大道至简。
2.0补充
// Json生成Map,2.0 ,破解了复杂,list,树
@SuppressWarnings("unchecked")
public static Map<String, Object> jsonToMap(String jsonString) throws JSONException {
// JSONObject必须以"{"开头
JSONObject jsonObject = JSONObject.fromObject(jsonString);
Map<String, Object> resultMap = new HashMap<String, Object>();
Iterator<String> iter = jsonObject.keys();
String key = null;
Object value = null;
while (iter.hasNext()) {
key = iter.next();
value = jsonObject.get(key);
if (value.getClass() == JSONArray.class) {
JSONArray jsonArray = JSONArray.fromObject(value);
for (Object list : jsonArray) {
Map<String, Object> resultMap2 = jsonToMap(list.toString());
resultMap.put(key, resultMap2);
}
} else {
resultMap.put(key, value);
}
}
// System.out.println(resultMap);
/*
* 打印结果{map={"sex":"female","age":"23","name":"Alexia"},
* employ={"sex":"female","age":"24","name":"wjl"}}
*/
return resultMap;
}
// Json生成Map,2.0 ,破解了复杂,list,树
@SuppressWarnings("unchecked")
public static <T extends FormMap<String, Object>> T jsonToMap(String jsonString,Class<T> clazz) throws JSONException, InstantiationException, IllegalAccessException {
// JSONObject必须以"{"开头
JSONObject jsonObject = JSONObject.fromObject(jsonString);
T t=clazz.newInstance();
Iterator<String> iter = jsonObject.keys();
String key = null;
Object value = null;
while (iter.hasNext()) {
key = iter.next();
value = jsonObject.get(key);
if (value.getClass() == JSONArray.class) {
JSONArray jsonArray = JSONArray.fromObject(value);
for (Object list : jsonArray) {
Map<String, Object> resultMap2 = jsonToMap(list.toString());
t.put(key, resultMap2);
}
} else {
t.put(key, value);
}
}
// System.out.println(resultMap);
/*
* 打印结果{map={"sex":"female","age":"23","name":"Alexia"},
* employ={"sex":"female","age":"24","name":"wjl"}}
*/
return t;
}
————————————————
普通类的使用
// 定义一个泛型类
private class GenericClass<T> {
// 在此处定义泛型类的属性和方法
private List<T> getCsvToBean (Class<? extends T> obj, String pathname) {
CSVReader reader = null;
try {
reader = new CSVReader(new FileReader(pathname));
String[] nextLine = reader.readNext();
// 读取每行数据
int columnCount = nextLine.length;
ColumnPositionMappingStrategy<T> strategy = new ColumnPositionMappingStrategy<>();
strategy.setType(obj);
String[] columnMapping = new String[columnCount];
for (int i = 0; i < columnCount; i++) {
columnMapping[i] = String.valueOf(i);
}
strategy.setColumnMapping(columnMapping);
CsvToBean<T> csvToBean = new CsvToBeanBuilder<T>(reader)
.withMappingStrategy(strategy)
.build();
List<T> parse = csvToBean.parse();
return parse;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
GenericClass<AcsCmOrderDto> genericClass = new GenericClass();
List<AcsCmOrderDto> csvToBean = genericClass.getCsvToBean(AcsCmOrderDto.class, pathname);
cmOrderDtoList = csvToBean;