Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
schbrain-parent
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
framework
schbrain-parent
Commits
607eb517
Commit
607eb517
authored
Aug 30, 2023
by
liaozan
🏀
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
TreeUtils use collection
parent
cb7b9fb1
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
18 deletions
+18
-18
commons/common-util/src/main/java/com/schbrain/common/util/TreeUtils.java
...til/src/main/java/com/schbrain/common/util/TreeUtils.java
+18
-18
No files found.
commons/common-util/src/main/java/com/schbrain/common/util/TreeUtils.java
View file @
607eb517
package
com.schbrain.common.util
;
import
cn.hutool.core.collection.
List
Util
;
import
cn.hutool.core.collection.
Collection
Util
;
import
com.google.common.collect.Maps
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.commons.collections4.MapUtils
;
...
...
@@ -18,28 +18,28 @@ import java.util.function.Function;
*/
public
class
TreeUtils
{
public
static
<
T
,
K
>
List
<
T
>
buildTree
(
List
<
T
>
nodes
,
public
static
<
T
,
K
>
List
<
T
>
buildTree
(
Collection
<
T
>
nodes
,
Function
<
T
,
K
>
keyExtractor
,
Function
<
T
,
K
>
parentKeyExtractor
,
BiConsumer
<
T
,
List
<
T
>>
childrenSetter
,
BiConsumer
<
T
,
Collection
<
T
>>
childrenSetter
,
@Nullable
K
parentId
)
{
return
buildTree
(
nodes
,
keyExtractor
,
parentKeyExtractor
,
Function
.
identity
(),
childrenSetter
,
parentId
);
}
public
static
<
T
,
K
,
E
>
List
<
E
>
buildTree
(
List
<
T
>
nodes
,
public
static
<
T
,
K
,
E
>
List
<
E
>
buildTree
(
Collection
<
T
>
nodes
,
Function
<
T
,
K
>
keyExtractor
,
Function
<
T
,
K
>
parentKeyExtractor
,
Function
<
T
,
E
>
childMapper
,
BiConsumer
<
E
,
List
<
E
>>
childrenSetter
,
BiConsumer
<
E
,
Collection
<
E
>>
childrenSetter
,
@Nullable
K
parentId
)
{
return
buildTree
(
nodes
,
keyExtractor
,
parentKeyExtractor
,
childMapper
,
childrenSetter
,
null
,
parentId
);
}
public
static
<
T
,
K
,
E
>
List
<
E
>
buildTree
(
List
<
T
>
nodes
,
public
static
<
T
,
K
,
E
>
List
<
E
>
buildTree
(
Collection
<
T
>
nodes
,
Function
<
T
,
K
>
keyExtractor
,
Function
<
T
,
K
>
parentKeyExtractor
,
Function
<
T
,
E
>
childMapper
,
BiConsumer
<
E
,
List
<
E
>>
childrenSetter
,
BiConsumer
<
E
,
Collection
<
E
>>
childrenSetter
,
@Nullable
Comparator
<
E
>
childrenComparator
,
@Nullable
K
parentId
)
{
if
(
CollectionUtils
.
isEmpty
(
nodes
))
{
...
...
@@ -54,10 +54,10 @@ public class TreeUtils {
return
doBuildTree
(
keyExtractor
,
childrenSetter
,
childMapper
,
parentWithSubNodes
,
childrenComparator
,
parentId
);
}
public
static
<
T
,
E
>
List
<
E
>
getParents
(
E
id
,
List
<
T
>
nodeList
,
Function
<
T
,
E
>
keyMapper
,
Function
<
T
,
E
>
parentMapper
,
boolean
includeSelf
)
{
public
static
<
T
,
E
>
List
<
E
>
getParents
(
E
id
,
Collection
<
T
>
nodes
,
Function
<
T
,
E
>
keyMapper
,
Function
<
T
,
E
>
parentMapper
,
boolean
includeSelf
)
{
// toMap 不允许 value 为空,当 parentId 为空时,单独处理下
Map
<
E
,
E
>
parentMap
=
Maps
.
newHashMapWithExpectedSize
(
node
List
.
size
());
for
(
T
node
:
node
List
)
{
Map
<
E
,
E
>
parentMap
=
Maps
.
newHashMapWithExpectedSize
(
node
s
.
size
());
for
(
T
node
:
node
s
)
{
parentMap
.
put
(
keyMapper
.
apply
(
node
),
parentMapper
.
apply
(
node
));
}
return
getParents
(
id
,
parentMap
,
includeSelf
);
...
...
@@ -74,7 +74,7 @@ public class TreeUtils {
return
getParents
(
id
,
parentMap
,
parentIds
);
}
public
static
<
T
,
E
>
List
<
E
>
buildNodeList
(
List
<
T
>
tree
,
Function
<
T
,
List
<
T
>>
childGetter
,
Function
<
T
,
E
>
mapper
)
{
public
static
<
T
,
E
>
List
<
E
>
buildNodeList
(
Collection
<
T
>
tree
,
Function
<
T
,
Collection
<
T
>>
childGetter
,
Function
<
T
,
E
>
mapper
)
{
List
<
E
>
nodes
=
new
ArrayList
<>();
if
(
CollectionUtils
.
isEmpty
(
tree
))
{
return
nodes
;
...
...
@@ -83,7 +83,7 @@ public class TreeUtils {
return
nodes
;
}
private
static
<
E
,
T
>
void
doBuildNodeList
(
List
<
T
>
tree
,
Function
<
T
,
List
<
T
>>
childGetter
,
Function
<
T
,
E
>
mapper
,
List
<
E
>
nodes
)
{
private
static
<
E
,
T
>
void
doBuildNodeList
(
Collection
<
T
>
tree
,
Function
<
T
,
Collection
<
T
>>
childGetter
,
Function
<
T
,
E
>
mapper
,
List
<
E
>
nodes
)
{
tree
.
forEach
(
node
->
{
nodes
.
add
(
mapper
.
apply
(
node
));
doBuildNodeList
(
childGetter
.
apply
(
node
),
childGetter
,
mapper
,
nodes
);
...
...
@@ -100,15 +100,15 @@ public class TreeUtils {
}
private
static
<
E
,
K
,
T
>
List
<
E
>
doBuildTree
(
Function
<
T
,
K
>
keyExtractor
,
BiConsumer
<
E
,
List
<
E
>>
childrenSetter
,
BiConsumer
<
E
,
Collection
<
E
>>
childrenSetter
,
Function
<
T
,
E
>
childMapper
,
Map
<
K
,
List
<
T
>>
parentWithSubNodes
,
Comparator
<
E
>
childrenComparator
,
K
parentId
)
{
List
<
T
>
subNodes
=
parentWithSubNodes
.
remove
(
parentId
);
Collection
<
T
>
subNodes
=
parentWithSubNodes
.
remove
(
parentId
);
List
<
E
>
treeList
=
StreamUtils
.
toList
(
subNodes
,
subNode
->
{
E
child
=
childMapper
.
apply
(
subNode
);
List
<
E
>
children
=
doBuildTree
(
keyExtractor
,
childrenSetter
,
childMapper
,
parentWithSubNodes
,
childrenComparator
,
keyExtractor
.
apply
(
subNode
));
Collection
<
E
>
children
=
doBuildTree
(
keyExtractor
,
childrenSetter
,
childMapper
,
parentWithSubNodes
,
childrenComparator
,
keyExtractor
.
apply
(
subNode
));
sort
(
children
,
childrenComparator
);
childrenSetter
.
accept
(
child
,
children
);
return
child
;
...
...
@@ -117,9 +117,9 @@ public class TreeUtils {
return
treeList
;
}
private
static
<
E
>
void
sort
(
List
<
E
>
dataList
,
Comparator
<
E
>
comparator
)
{
if
(
comparator
!=
null
)
{
ListUtil
.
sort
(
dataList
,
Comparator
.
nullsLast
(
comparator
)
);
private
static
<
E
>
void
sort
(
Collection
<
E
>
data
,
Comparator
<
E
>
comparator
)
{
if
(
comparator
!=
null
&&
CollectionUtils
.
isNotEmpty
(
data
)
)
{
CollectionUtil
.
sort
(
data
,
comparator
);
}
}
...
...
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