Commit 6a1bedd4 authored by yanzg's avatar yanzg

修复等待时间

parent c49b7b0e
...@@ -2,9 +2,7 @@ package com.yanzuoguang.util.base; ...@@ -2,9 +2,7 @@ package com.yanzuoguang.util.base;
import com.yanzuoguang.util.helper.StringHelper; import com.yanzuoguang.util.helper.StringHelper;
import java.util.Collection; import java.util.*;
import java.util.LinkedHashMap;
import java.util.Map;
/** /**
* 集合转换为表格字符串 * 集合转换为表格字符串
...@@ -26,16 +24,9 @@ public class CollectionString { ...@@ -26,16 +24,9 @@ public class CollectionString {
*/ */
public static <T> String getCollectionString(String tag, Collection<T> rowList) { public static <T> String getCollectionString(String tag, Collection<T> rowList) {
// 每个字段的长度 // 每个字段的长度
Map<String, Integer> mapFieldCount = new LinkedHashMap<>(15); Map<String, Integer> mapFieldCount = getValueLength(rowList);
// 每个值的长度 // 删除为0的长度,并将列名长度写入
rowList.forEach(k -> ObjectEach.each(k, (name, value, field) -> { initColumnNameLength(mapFieldCount);
// 获取字符串
String val = StringHelper.getFirst(StringHelper.toString(value));
int len = Math.max(length(name), length(val));
Integer hisLen = mapFieldCount.getOrDefault(name, len);
int maxLen = Math.max(len, hisLen);
mapFieldCount.put(name, maxLen);
}));
// 生成结果 // 生成结果
StringBuilder sbResult = new StringBuilder(); StringBuilder sbResult = new StringBuilder();
sbResult.append(tag); sbResult.append(tag);
...@@ -67,6 +58,47 @@ public class CollectionString { ...@@ -67,6 +58,47 @@ public class CollectionString {
return sbResult.toString(); return sbResult.toString();
} }
/**
* 删除为0的长度,并将列名长度写入
*
* @param mapFieldCount
*/
private static void initColumnNameLength(Map<String, Integer> mapFieldCount) {
// 添加到另外一个集合,防止遍历列名通知执行删除时出错
List<String> keys = new ArrayList<>(mapFieldCount.keySet());
keys.forEach(key -> {
int len = mapFieldCount.get(key);
if (len == 0) {
mapFieldCount.remove(key);
} else {
int maxLen = Math.max(len, length(key));
mapFieldCount.put(key, maxLen);
}
});
}
/**
* 获取值字段的长度
*
* @param rowList 行
* @param <T> 行类型
* @return 各列值长度
*/
private static <T> Map<String, Integer> getValueLength(Collection<T> rowList) {
// 每个字段的长度
Map<String, Integer> mapFieldCount = new LinkedHashMap<>(15);
// 每个值的长度
rowList.forEach(k -> ObjectEach.each(k, (name, value, field) -> {
// 获取字符串
String val = StringHelper.getFirst(StringHelper.toString(value));
int len = length(val);
Integer hisLen = mapFieldCount.getOrDefault(name, len);
int maxLen = Math.max(len, hisLen);
mapFieldCount.put(name, maxLen);
}));
return mapFieldCount;
}
/** /**
* 添加空行 * 添加空行
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment