【List排除集合中的某些属性】
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
推荐阅读
-
Java 中初始化 List 集合的 6 种方式!
-
Java8利用stream的distinct()方法对list集合中的对象去重和抽取属性去重
-
【转载】C#中List集合使用RemoveRange方法移除指定索引开始的一段元素
-
C#中List集合使用Max()方法查找到最大值的实例
-
【转载】C#中ArrayList集合类和List集合类的比较
-
【转载】C#中List集合中Last和LastOrDefault方法的差别
-
【转载】C#中List集合使用LastOrDefault方法查找出最后一个符合条件的元素
-
【转载】C#中ToArray方法将List集合转换为对应的数组
-
【转载】C#中List集合使用RemoveAt方法移除指定索引位置的元素
-
【转载】C#中List集合使用Reverse方法对集合中的元素进行倒序反转