Java工具类
package org.common;
import lombok.extern.slf4j.Slf4j;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
public final class CollectorHelper {
/**
* @param element
* @param propertyName
* @param <E>
* @return
*/
public static <E> List toList(Collection<E> element, String propertyName) {
if (element == null) {
return Collections.emptyList();
}
return element.stream().map(item -> {
return ReflectCallField.getForInstance(item, propertyName);
}).collect(Collectors.toList());
}
/**
* @param element
* @param propertyName
* @param <E>
* @return
*/
public static <E> List extractList(Collection<E> element, String propertyName) {
if (element == null) {
return Collections.emptyList();
}
List dataList = new ArrayList();
for (E item : element) {
Object obj = ReflectCallField.getForInstance(item, propertyName);
if (obj != null) {
dataList.add(obj);
}
}
return dataList;
}
/**
* @param element
* @param propertyName
* @param <K>
* @param <V>
* @return
*/
public static <K, V> Map<K, V> toMap(Collection<V> element, String propertyName) {
if (element == null) {
return Collections.emptyMap();
}
try {
return (Map<K, V>) element.stream().collect(Collectors.toMap(item -> {
return ReflectCallField.getForInstance(item, propertyName);
}, item -> item));
} catch (Exception e) {
log.error(e.getMessage());
}
return Collections.emptyMap();
}
/**
* @param element
* @param propertyName
* @param <K>
* @param <V>
* @return
*/
public static <K, V> Map<K, V> extractMap(Collection<V> element, String propertyName) {
if (element == null) {
return Collections.emptyMap();
}
Map<K, V> dataMap = new HashMap();
for (V item : element) {
Object key = ReflectCallField.getForInstance(item, propertyName);
if (key != null) {
dataMap.put((K) key, item);
}
}
return dataMap;
}
}
package org.common;
import java.lang.reflect.Field;
public class ReflectCallField {
/**
* @param entryClass
* @param fieldName
* @param <T>
* @return
*/
public static <T> T getForStatic(Class entryClass, String fieldName) {
if (entryClass == null) {
return null;
}
T result = null;
try {
// 反射获取
Field callField = entryClass.getDeclaredField(fieldName);
callField.setAccessible(true);
return (T) callField.get(entryClass);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* @param entryClass
* @param fieldName
* @param value
*/
public static void setForStatic(Class entryClass, String fieldName, Object value) {
if (entryClass == null) {
return;
}
try {
Field callField = entryClass.getDeclaredField(fieldName);
callField.setAccessible(true);
callField.set(fieldName, value);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param entryObject
* @param fieldName
* @param <T>
* @return
*/
public static <T> T getForInstance(Object entryObject, String fieldName) {
if (entryObject == null) {
return null;
}
T result = null;
try {
// 反射获取
Field callField = entryObject.getClass().getDeclaredField(fieldName);
callField.setAccessible(true);
return (T) callField.get(entryObject);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* @param entryObject
* @param fieldName
* @param value
*/
public static void setForInstance(Object entryObject, String fieldName, Object value) {
if (entryObject == null) {
return;
}
try {
Field callField = entryObject.getClass().getDeclaredField(fieldName);
callField.setAccessible(true);
callField.set(entryObject, value);
} catch (Exception e) {
e.printStackTrace();
}
}
}
测试
package org.common;
import lombok.Data;
@Data
public class Franchisee {
private Integer id;
private String name;
}
package org.common;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class CollectorHelperTest {
public static void main(String[] args) {
List<Franchisee> dataList = new ArrayList<>();
Franchisee franchisee1 = new Franchisee();
franchisee1.setId(1);
franchisee1.setName("Franchisee1");
dataList.add(franchisee1);
Franchisee franchisee2 = new Franchisee();
franchisee2.setId(2);
franchisee2.setName("Franchisee2");
dataList.add(franchisee2);
Franchisee franchisee3 = new Franchisee();
franchisee3.setName("Franchisee3");
dataList.add(franchisee3);
Franchisee franchisee4 = new Franchisee();
franchisee4.setName("Franchisee4");
dataList.add(franchisee4);
//
Map<Integer, Franchisee> dataMap = CollectorHelper.toMap(dataList, "id");
System.out.println("dataMap = " + dataMap);
Map<Integer, Franchisee> dataMap2 = CollectorHelper.extractMap(dataList, "id");
System.out.println("dataMap2 = " + dataMap2);
//
List<Integer> idList = CollectorHelper.toList(dataList, "id");
System.out.println("idList = " + idList);
idList.forEach(id -> {
System.out.println("id = " + id);
});
List<Integer> idList2 = CollectorHelper.extractList(dataList, "id");
System.out.println("idList2 = " + idList2);
idList2.forEach(id -> {
System.out.println("id = " + id);
});
}
}