Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YZG
yzg-util
Commits
6a1bedd4
Commit
6a1bedd4
authored
May 13, 2022
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修复等待时间
parent
c49b7b0e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
13 deletions
+45
-13
CollectionString.java
...main/java/com/yanzuoguang/util/base/CollectionString.java
+45
-13
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/base/CollectionString.java
View file @
6a1bedd4
...
@@ -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
;
}
/**
/**
* 添加空行
* 添加空行
*
*
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment