欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【List排除集合中的某些属性】  

程序员文章站 2022-03-25 15:25:54
...

Set<String> exclusionSet = new HashSet<>();

exclusionSet.add(Constants.CLASS);

exclusionSet.add("taskJson");

List<TaskInstance> taskInstanceList = taskInstanceIPage.getRecords();

// 排除集合中的某些属性

CollectionUtils.getListByExclusion(taskInstanceList,exclusionSet);

 

 

/**

 * Removes certain attributes of each object in the list

 * @param originList origin list

 * @param exclusionSet exclusion set

 * @param <T> T

 * @return removes certain attributes of each object in the list

 */

public static <T extends Object> List<Map<String, Object>> getListByExclusion(List<T> originList, Set<String> exclusionSet) {

List<Map<String, Object>> instanceList = new ArrayList<>();

if (exclusionSet == null) {

exclusionSet = new HashSet<>();

}

if (originList == null) {

return instanceList;

}

Map<String, Object> instanceMap;

for (T instance : originList) {

Map<String, Object> dataMap = new BeanMap(instance);

instanceMap = new LinkedHashMap<>(16,0.75f,true);

for (Map.Entry<String, Object> entry: dataMap.entrySet()) {

if (exclusionSet.contains(entry.getKey())) {

continue;

}

instanceMap.put(entry.getKey(), entry.getValue());

}

instanceList.add(instanceMap);

}

return instanceList;

}

 

代码来自dolphinscheduler的CollectionUtils