All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.opslab.util.collection.CollectionUtil Maven / Gradle / Ivy

package com.opslab.util.collection;

import com.opslab.util.valid;

import java.util.*;

/**
 * 封装一些集合相关度工具类
 * 

* Collection<--List<--Vector * Collection<--List<--ArrayList * Collection<--List<--LinkedList * Collection<--Set<--HashSet * Collection<--Set<--HashSet<--LinkedHashSet * Collection<--Set<--SortedSet<--TreeSet * Map<--SortedMap<--TreeMap * Map<--HashMap */ public class CollectionUtil { /** * 去重 */ public static List removeDuplicate(List list) { Set set = new HashSet(); List newList = new ArrayList(); for (Iterator iter = list.iterator(); iter.hasNext(); ) { Object element = iter.next(); if (set.add(element)) newList.add(element); } return newList; } /** * 使用指定的Filter过滤集合 */ public static List Filter(List list, ListFilter filter) { List result = new ArrayList(); if (valid.valid(list)) { for (T t : list) { if (filter.filter(t)) { result.add(t); } } } return result; } public static Set Filter(Set set, SetFilter filter) { Set result = new HashSet(); if (valid.valid(set)) { for (T t : set) { if (filter.filter(t)) { result.add(t); } } } return result; } public static Queue Filter(Queue queue, QueueFilter filter) { Queue result = new LinkedList(); if (valid.valid(queue)) { for (T t : queue) { if (filter.filter(t)) { result.add(t); } } } return result; } public static Map Filter(Map map, MapFilter filter) { Map result = new HashMap(); if (valid.valid(map)) { for (Map.Entry entry : map.entrySet()) { if (filter.filter(entry)) { result.put(entry.getKey(), entry.getValue()); } } } return result; } /** * 求俩个集合的交集 */ public static List intersection(List list1, List list2) { if (valid.valid(list1, list2)) { Set set = new HashSet<>(list1); set.retainAll(list2); return new ArrayList<>(set); } return new ArrayList(); } public static Set intersection(Set set1, Set set2) { if (valid.valid(set1, set2)) { List list = new ArrayList(set1); list.retainAll(set2); return new HashSet<>(list); } return new HashSet(); } public static Queue intersection(Queue queue1, Queue queue2) { if (valid.valid(queue1, queue2)) { Set set = new HashSet(queue1); set.retainAll(queue2); return new LinkedList(set); } return new LinkedList(); } /** * Map集合的交集只提供键的交集 * * @param map1 * @param map2 * @param * @param * @return */ public static Map intersection(Map map1, Map map2) { Map map = new HashMap<>(); if (valid.valid(map1, map2)) { Set setkey1 = new HashSet<>(map1.keySet()); Set setkey2 = new HashSet<>(map2.keySet()); setkey1.retainAll(setkey2); for (K k : setkey1) { map.put(k, map1.get(k)); } } return map; } /////////////////////////////////////////////////////////////////////////////// /** * 求俩个集合的并集 */ public static List unicon(List list1, List list2) { List list = new ArrayList(); list.addAll(list1); list.addAll(list2); return list; } public static Set unicon(Set set1, Set set2) { Set set = new HashSet<>(); set = set1; set.addAll(set2); return set; } public static Queue unicon(Queue queue1, Queue queue2) { Queue queue = new LinkedList(); queue.addAll(queue1); queue.addAll(queue2); return queue; } public static Map unicon(Map map1, Map map2) { Map map = new HashMap<>(); map.putAll(map1); map.putAll(map2); return map; } /////////////////////////////////////////////////////////////////////////////// /** * 求俩个集合的差集 */ public static List subtract(List list1, List list2) { List list = new ArrayList<>(); if (valid.valid(list1)) { list.addAll(list1); list.removeAll(list2); } return list; } public static Set subtract(Set set1, Set set2) { Set set = new HashSet<>(); if (valid.valid(set1)) { set.addAll(set1); set.removeAll(set2); } return set; } public static Queue subtract(Queue queue1, Queue queue2) { Queue queue = new LinkedList<>(); if (valid.valid(queue1)) { queue.addAll(queue1); queue.removeAll(queue2); } return queue; } public static Map subtract(Map map1, Map map2) { Map map = new HashMap<>(); if (valid.valid(map1, map2)) { Set setkey1 = new HashSet<>(map1.keySet()); Set setkey2 = new HashSet<>(map2.keySet()); for (K k : setkey2) { setkey1.remove(k); } for (K k : setkey1) { map.put(k, map1.get(k)); } } return map; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy