编码其实是一个可以做到非常精致的事情,每次回头看自己以前写的代码,总是觉得可以采用更好的方式去实现。从好的方面来说自己一直在进步,但从差的方面来说就是自己一直都做的不够好。
在日常编码过程中有时候灵机一动的念头,可能就是对于解决某些场景非常有用的方案,因此我决定将一些编码过程中的优化记录下来,总结后真正的变成自己的东西,这样在往后的编码过程中就可以做到更好。当然代码优化是一个永无止境的事情,总结也是永无止境。
本文讲的就是一个简单的应用场景,如何从最常规的实现,一步步的完成代码层面的优化。
1. 场景描述
应用场景:有一个用户清单,包含有用户信息及客户信息,需要按客户编号+客户名称汇总用户数据;
假设1:用户对象定义如下:
/**
* 用户信息
*
* @author LiuQi 2019/8/20-9:11
* @version V1.0
**/
public class User {
private Long id;
private String name;
private Long customerId;
private String customerName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getCustomerId() {
return customerId;
}
public void setCustomerId(Long customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
}
这是一个简单的Java Bean对象,包含有客户相关属性及用户相关属性,并提供有Setter与Getter方法。
假设2:有一个List,需要将其转换成以下存储方式:
[
{
"customerId": 1,
"customerName": "1",
"users": [
{
"userId": 1,
"userName": "LiuQi"
},
{
"userId": 2,
"userName": "ShenYu"
}
]
},
{
"customerId": 2,
"customerName": "2",
"users": [
{
"userId": 3,
"userName": "ZhangSan"
},
{
"userId": 4,
"userName": "LiSi"
}
]
}
]
2. 常规实现
这是一个很简单的需求,拿到需求后我们就开搞,先来看最常规的实现,也是多数人会采用的实现:
public class Test {
public static void main(String[] args) {
List<User> users = init();
// 先将用户按客户编号汇聚,同时保存客户编号与客户名称的关系
Map<Long, List<Map<String, Object>>> customerUserMap = new HashMap<>(16);
Map<Long, String> customerNameMap = new HashMap<>(16);
for (User user : users) {
Long customerId = user.getCustomerId();
String customerName = user.getCustomerName();
if (!customerNameMap.containsKey(customerId)) {
customerNameMap.put(customerId, customerName);
}
List<Map<String, Object>> customerUserList = customerUserMap.get(customerId);
if (null == customerUserList) {
customerUserList = new ArrayList<>(16);
customerUserMap.put(customerId, customerUserList);
}
Map<String, Object> userInfoMap = new HashMap<>(16);
userInfoMap.put("userId", user.getId());
userInfoMap.put("userName", user.getName());
customerUserList.add(userInfoMap);
}
// 遍历所有客户,组装结果
List<Map<String, Object>> resultList = new ArrayList<>(16);
Iterator<Long> it = customerNameMap.keySet().iterator();
while (it.hasNext()) {
Long customerId = it.next();
String customerName = customerNameMap.get(customerId);
List<Map<String, Object>> customerUserList = customerUserMap.get(customerId);
Map<String, Object> customerMap = new HashMap<>(16);
customerMap.put("customerId", customerId);
customerMap.put("customerName", customerName);
customerMap.put("users", customerUserList);
resultList.add(customerMap);
}
System.out.println(resultList);
}
private static List<User> init() {
List<User> list = new ArrayList<>(16);
User user = new User();
user.setId(1L);
user.setName("1");
user.setCustomerId(1L);
user.setCustomerName("1");
list.add(user);
user = new User();
user.setId(2L);
user.setName("2");
user.setCustomerId(1L);
user.setCustomerName("1");
list.add(user);
user = new User();
user.setId(3L);
user.setName("3");
user.setCustomerId(2L);
user.setCustomerName("2");
list.add(user);
return list;
}
}
下面来对这些代码进行一步步的优化 。
3. 针对初始化代码的优化
先来看下初始化用户清单的代码:
private static List<User> init() {
List<User> list = new ArrayList<>(16);
User user = new User();
user.setId(1L);
user.setName("1");
user.setCustomerId(1L);
user.setCustomerName("1");
list.add(user);
user = new User();
user.setId(2L);
user.setName("2");
user.setCustomerId(1L);
user.setCustomerName