List
集合深拷贝
/**
* list集合深拷贝
*
* @param src
* @param <T>
* @return
*/
public static <T> List<T> deepCopy(List<T> src) {
try {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(src);
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
@SuppressWarnings("unchecked")
List<T> dest = (List<T>) in.readObject();
return dest;
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
字符串集合去重
使用Java8中的
Stream
流对字符串集合进行去重
/**
* 字符串List去重
*
* @param strList
* @return
*/
public static List<String> distinct(List<String> strList) {
return strList
.stream()
.distinct()
.collect(Collectors.toList());
}
/**
* 字符串List去重
*
* @param strList
* @param isFilterBlank 是否过滤掉空字符串
* @return
*/
public static List<String> distinct(List<String> strList, boolean isFilterBlank) {
return strList
.stream()
.distinct()
.filter(s -> isFilterBlank ? StringUtils.isNotBlank(s) : true)
.collect(Collectors.toList());
}
示例:
@Test
public void distinctTest() {
List<String> stringList = Arrays.asList("Test1", "Test1", "Test2", "Test2", null, null);
List<String> stringList1 = ListUtils.distinct(stringList);
System.out.println(JSON.toJSONString(stringList1));
List<String> stringList2 = ListUtils.distinct(stringList, true);
System.out.println(JSON.toJSONString(stringList2));
List<String> stringList3 = ListUtils.distinct(stringList, false);
System.out.println(JSON.toJSONString(stringList3));
}
执行结果:
["Test1","Test2",null]
["Test1","Test2"]
["Test1","Test2",null]
对象集合转属性集合
对象集合转属性集合的意思是有一个对象
List
,只想把这个对象中的某个属性单独提取出来,生成一个List
实现方式:利用Java
的反射和Java8
中的Lambda
表达式、Stream
流进行的简单实现
/**
* 获取属性List
*
* @param list 对象List
* @param fieldName 属性名称
* @param <E>
* @param <T>
* @return
*/
public static <E, T> List<E> getFieldList(List<T> list, String fieldName) {
if (CollectionUtils.isEmpty(list) || StringUtils.isBlank(fieldName)) {
return new ArrayList();
}
try {
Field field = list.get(0).getClass().getDeclaredField(fieldName);
field.setAccessible(true);
return (List<E>) list
.stream()
.map(e -> getFieldValue(e, field))
.distinct()
.collect(Collectors.toList());
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
return new ArrayList();
}
/**
* 获取属性对应的值
*
* @param t 目标对象
* @param field 属性
* @param <E>
* @param <T>
* @return
*/
private static <E, T> E getFieldValue(T t, Field field) {
try {
return (E) field.get(t);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
示例:
public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(Integer id, String name) {
this.id = id;
this.name = name;
}
}
@Test
public void getFieldListTest() {
List<User> userList = Arrays.asList(
new User(1, "Test1"),
new User(2, "Test2"),
new User(3, "Test3"),
new User(4, "Test4")
);
List<Integer> userIds = ListUtils.getFieldList(userList, "id");
System.out.println(userIds);
}
执行结果:
[1, 2, 3, 4]
集合转Map
将一个对象集合封装成
Map
,Map
的Key
为对象的一个属性值,Value
默认是对象本身,也可以指定为对象的一个属性值。
实现方式:利用Java
的反射和Java8
中的Lambda
表达式、Stream
流进行的简单实现
Map
的Value
为对象
/**
* List转Map
* key:成员变量
* value:实体类
*
* @param list
* @param fieldName
* @param <K>
* @param <V>
* @return
*/
public static <K, V> Map<K, V> listToMap(List<V> list, String fieldName) {
if (CollectionUtils.isEmpty(list) || StringUtils.isBlank(fieldName)) {
return new HashMap(0);
}
try {
Field field = list.get(0).getClass().getDeclaredField(fieldName);
field.setAccessible(true);
return list
.stream()
.filter(e -> Objects.nonNull(e))
.collect(Collectors.toMap(e -> getFieldValue(e, field), Function.identity(), (v1, v2) -> v1));
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
return new HashMap(0);
}
/**
* 获取属性对应的值
*
* @param t 目标对象
* @param field 属性
* @param <E>
* @param <T>
* @return
*/
private static <E, T> E getFieldValue(T t, Field field) {
try {
return (E) field.get(t);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
示例:
public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(Integer id, String name) {
this.id = id;
this.name = name;
}
}
@Test
public void listToMapTest() {
List<User> userList = Arrays.asList(
new User(1, "Test1"),
new User(2, "Test2"),
new User(3, "Test3"),
new User(4, "Test4")
);
Map<Integer, User> userMap = ListUtils.listToMap(userList, "id");
System.out.println(JSON.toJSONString(userMap));
}
执行结果:
{1:{"id":1,"name":"Test1"},2:{"id":2,"name":"Test2"},3:{"id":3,"name":"Test3"},4:{"id":4,"name":"Test4"}}
Map
的Value
为属性值
/**
* List转Map
* key:keyFieldName字段
* value:valueFieldName字段
*
* @param list 对象List
* @param keyFieldName key属性
* @param valueFieldName value属性
* @param <K>
* @param <V>
* @param <E>
* @return
*/
public static <K, V, E> Map<K, V> listToMap(List<E> list, String keyFieldName, String valueFieldName) {
if (CollectionUtils.isEmpty(list) || StringUtils.isBlank(keyFieldName) || StringUtils.isBlank(valueFieldName)) {
return new HashMap<>(0);
}
try {
Field keyField = list.get(0).getClass().getDeclaredField(keyFieldName);
keyField.setAccessible(true);
Field valueField = list.get(0).getClass().getDeclaredField(valueFieldName);
valueField.setAccessible(true);
return list
.stream()
.filter(e -> Objects.nonNull(e))
.collect(Collectors.toMap(e -> getFieldValue(e, keyField), e -> getFieldValue(e, valueField), (v1, v2) -> v1));
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
return new HashMap(0);
}
/**
* 获取属性对应的值
*
* @param t 目标对象
* @param field 属性
* @param <E>
* @param <T>
* @return
*/
private static <E, T> E getFieldValue(T t, Field field) {
try {
return (E) field.get(t);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
示例:
public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(Integer id, String name) {
this.id = id;
this.name = name;
}
}
@Test
public void listToMapTest() {
List<User> users = Arrays.asList(
new User(1, "Test1"),
new User(2, "Test2"),
new User(3, "Test3"),
new User(4, "Test4")
);
Map<Integer, Integer> map = ListUtils.listToMap(users, "id", "name");
System.out.println(JSON.toJSONString(map));
}
执行结果:
{1:"Test1",2:"Test2",3:"Test3",4:"Test4"}