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
c8c7afc1
Commit
c8c7afc1
authored
Sep 23, 2022
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
升级新版本
parent
e9ac67c4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
81 additions
and
45 deletions
+81
-45
MapHelper.java
.../src/main/java/com/yanzuoguang/util/helper/MapHelper.java
+81
-45
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/MapHelper.java
View file @
c8c7afc1
...
@@ -5,22 +5,40 @@ import java.util.HashMap;
...
@@ -5,22 +5,40 @@ import java.util.HashMap;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
/**
* 映射处理
*
* @author 颜佐光
*/
public
class
MapHelper
{
public
class
MapHelper
{
/**
/**
* 将一个对象列表转换为HashMap
* 将一个对象列表转换为HashMap
*
*
* @param list 需要转换的列表
* @param list 需要转换的列表
* @param <T>
* @param <T>
泛型
* @return
* @return
泛型处理
*/
*/
public
static
<
T
extends
MapKey
>
Map
<
String
,
T
>
getMap
(
List
<
T
>
list
)
{
public
static
<
T
extends
MapKey
<
T
>>
Map
<
String
,
T
>
getMap
(
List
<
T
>
list
)
{
Map
<
String
,
T
>
map
=
new
HashMap
<>();
return
getMap
(
list
,
(
item
)
->
item
.
getKey
(
item
));
if
(
list
!=
null
)
{
}
for
(
T
item
:
list
)
{
String
key
=
item
.
getKey
(
item
);
/**
map
.
put
(
key
,
item
);
* 将一个对象列表转换为HashMap
}
*
* @param list 需要转换的列表
* @param proxy 获取主键的函数
* @param <T> 泛型
* @return 泛型处理
*/
public
static
<
T
extends
Object
>
Map
<
String
,
T
>
getMap
(
List
<
T
>
list
,
MapKey
<
T
>
proxy
)
{
if
(
list
==
null
)
{
return
new
HashMap
<>(
0
);
}
Map
<
String
,
T
>
map
=
new
HashMap
<>(
list
.
size
());
for
(
T
item
:
list
)
{
String
key
=
proxy
.
getKey
(
item
);
map
.
put
(
key
,
item
);
}
}
return
map
;
return
map
;
}
}
...
@@ -30,16 +48,18 @@ public class MapHelper {
...
@@ -30,16 +48,18 @@ public class MapHelper {
*
*
* @param list 需要转换的列表
* @param list 需要转换的列表
* @param proxy 获取主键的函数
* @param proxy 获取主键的函数
* @param <T>
* @param <T> 类型
* @return
* @param <M> 关键字类型
* @return 映射结果
*/
*/
public
static
<
T
extends
Object
>
Map
<
String
,
T
>
getMap
(
List
<
T
>
list
,
MapKey
<
T
>
proxy
)
{
public
static
<
T
,
M
>
Map
<
M
,
T
>
getMapType
(
List
<
T
>
list
,
MapKeyType
<
T
,
M
>
proxy
)
{
Map
<
String
,
T
>
map
=
new
HashMap
<>();
if
(
list
==
null
)
{
if
(
list
!=
null
)
{
return
new
HashMap
<>(
0
);
for
(
T
item
:
list
)
{
}
String
key
=
proxy
.
getKey
(
item
);
Map
<
M
,
T
>
map
=
new
HashMap
<>(
list
.
size
());
map
.
put
(
key
,
item
);
for
(
T
item
:
list
)
{
}
M
key
=
proxy
.
getKey
(
item
);
map
.
put
(
key
,
item
);
}
}
return
map
;
return
map
;
}
}
...
@@ -48,17 +68,18 @@ public class MapHelper {
...
@@ -48,17 +68,18 @@ public class MapHelper {
* 将一个对象列表转换为HashMap
* 将一个对象列表转换为HashMap
*
*
* @param list 需要转换的列表
* @param list 需要转换的列表
* @param <T>
* @param <T>
泛型
* @return
* @return
泛型处理
*/
*/
public
static
<
T
extends
MapKey
>
List
<
String
>
getKeys
(
List
<
T
>
list
)
{
public
static
<
T
extends
MapKey
<
T
>>
List
<
String
>
getKeys
(
List
<
T
>
list
)
{
List
<
String
>
to
=
new
ArrayList
<>();
if
(
list
==
null
)
{
if
(
list
!=
null
)
{
return
new
ArrayList
<>();
for
(
T
item
:
list
)
{
}
String
key
=
item
.
getKey
(
item
);
List
<
String
>
to
=
new
ArrayList
<>(
list
.
size
());
if
(!
to
.
contains
(
key
))
{
for
(
T
item
:
list
)
{
to
.
add
(
key
);
String
key
=
item
.
getKey
(
item
);
}
if
(!
to
.
contains
(
key
))
{
to
.
add
(
key
);
}
}
}
}
return
to
;
return
to
;
...
@@ -69,8 +90,8 @@ public class MapHelper {
...
@@ -69,8 +90,8 @@ public class MapHelper {
*
*
* @param list 需要转换的列表
* @param list 需要转换的列表
* @param proxy 获取主键的函数
* @param proxy 获取主键的函数
* @param <T>
* @param <T>
泛型
* @return
* @return
泛型处理
*/
*/
public
static
<
T
extends
Object
>
List
<
String
>
getKeys
(
List
<
T
>
list
,
MapKey
<
T
>
proxy
)
{
public
static
<
T
extends
Object
>
List
<
String
>
getKeys
(
List
<
T
>
list
,
MapKey
<
T
>
proxy
)
{
List
<
String
>
to
=
new
ArrayList
<>();
List
<
String
>
to
=
new
ArrayList
<>();
...
@@ -85,24 +106,15 @@ public class MapHelper {
...
@@ -85,24 +106,15 @@ public class MapHelper {
return
to
;
return
to
;
}
}
/**
* 一个获取对象关键字的处理函数
*
* @param <T>
*/
public
interface
MapKey
<
T
>
{
String
getKey
(
T
from
);
}
/**
/**
* 将建添加到子数组中
* 将建添加到子数组中
*
*
* @param mapList
* @param mapList
列表
* @param key
* @param key
关键字
* @param item
* @param item
对象
* @param <T>
* @param <T>
关键字类型
* @param <M>
* @param <M>
关键字对象
* @return
* @return
最终结果
*/
*/
public
static
<
T
,
M
>
List
<
M
>
addMapList
(
Map
<
T
,
List
<
M
>>
mapList
,
T
key
,
M
item
)
{
public
static
<
T
,
M
>
List
<
M
>
addMapList
(
Map
<
T
,
List
<
M
>>
mapList
,
T
key
,
M
item
)
{
if
(!
mapList
.
containsKey
(
key
))
{
if
(!
mapList
.
containsKey
(
key
))
{
...
@@ -112,4 +124,28 @@ public class MapHelper {
...
@@ -112,4 +124,28 @@ public class MapHelper {
ret
.
add
(
item
);
ret
.
add
(
item
);
return
ret
;
return
ret
;
}
}
/**
* 一个获取对象关键字的处理函数
*
* @param <T> 泛型
*/
public
interface
MapKey
<
T
>
extends
MapKeyType
<
T
,
String
>
{
}
/**
* 一个获取对象关键字的处理函数
*
* @param <T> 泛型
* @param <M> 泛型
*/
public
interface
MapKeyType
<
T
,
M
>
{
/**
* 获取关键字
*
* @param from 来源数据
* @return 关键字
*/
M
getKey
(
T
from
);
}
}
}
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