在运行此代码时,我面临内存异常,约束是堆大小。如果有进一步优化这段代码的方法,有人能提出建议吗?
public class getCustomerList {
public static List <Customer> retrieve() throws ParseException {
List<Customer> customers = new ArrayList<Customer>();
for (int i = 0; i < 100000; i++) {
Customer customer = new Customer();
customer.setAge(new Integer(i));
customer.setBirthDate((new SimpleDateFormat("ddMMyyyy")).parse("01061986"));
customer.setName("Customer" + new String((new Integer(i)).toString()));
customers.add(customer);
}
return customers;
}
}发布于 2022-09-10 08:26:05
很少有可能有帮助的想法:
如果年龄原语已经存在,则提供一个Integer.,而不是一个int。
customer.setAge(i);SimpleDateFormat移出循环之外,目前您创建了100000个相同的实例.customer.setBirthDate(format.parse("01061986"));Date实例吗?如果没有,您可以在每个客户中设置相同的日期实例。customer.setBirthDate(date);Integer对象,然后从它创建字符串(并丢弃整数),然后创建所述字符串的副本(并丢弃初始字符串)。只管做:customer.setName("Customer" + i);https://stackoverflow.com/questions/73669086
复制相似问题