A summary of java common collection tools

The company's code emphasizes neatness and standardization. Before the judgment of some sets, it was handwritten by itself. Now it uses the methods encapsulated by java itself. Now it summarizes some methods that are not commonly used.

Judge whether the set is empty

CollectionUtils.isEmpty(list)

public static boolean isEmpty(Collection coll) {
    return coll == null || coll.isEmpty();
}

Judge whether the set is not empty

CollectionUtils.isNotEmpty(list)

public static boolean isNotEmpty(Collection coll) {
    return !CollectionUtils.isEmpty(coll);
}

Union of two sets

CollectionUtils.union(listA, listB)

public static Collection union(final Collection a, final Collection b) {
    ArrayList list = new ArrayList();
    Map mapa = getCardinalityMap(a);
    Map mapb = getCardinalityMap(b);
    Set elts = new HashSet(a);
    elts.addAll(b);
    Iterator it = elts.iterator();
    while(it.hasNext()) {
        Object obj = it.next();
        for(int i=0,m=Math.max(getFreq(obj,mapa),getFreq(obj,mapb));i<m;i++) {
            list.add(obj);
        }
    }
    return list;
}

Intersection of two sets

CollectionUtils.intersection(listA, listB)

public static Collection intersection(final Collection a, final Collection b) {
    ArrayList list = new ArrayList();
    Map mapa = getCardinalityMap(a);
    Map mapb = getCardinalityMap(b);
    Set elts = new HashSet(a);
    elts.addAll(b);
    Iterator it = elts.iterator();
    while(it.hasNext()) {
        Object obj = it.next();
        for(int i=0,m=Math.min(getFreq(obj,mapa),getFreq(obj,mapb));i<m;i++) {
            list.add(obj);
        }
    }
    return list;
}

Difference set of two sets

CollectionUtils.subtract(listA, listB)

public static Collection subtract(final Collection a, final Collection b) {
    ArrayList list = new ArrayList( a );
    for (Iterator it = b.iterator(); it.hasNext();) {
        list.remove(it.next());
    }
    return list;
}

Complement of set (disjunction)

CollectionUtils.disjunction(listA, listB)

public static Collection disjunction(final Collection a, final Collection b) {
    ArrayList list = new ArrayList();
    Map mapa = getCardinalityMap(a);
    Map mapb = getCardinalityMap(b);
    Set elts = new HashSet(a);
    elts.addAll(b);
    Iterator it = elts.iterator();
    while(it.hasNext()) {
        Object obj = it.next();
        for(int i=0,m=((Math.max(getFreq(obj,mapa),getFreq(obj,mapb)))-(Math.min(getFreq(obj,mapa),getFreq(obj,mapb))));i<m;i++) {
            list.add(obj);
        }
    }
    return list;
}

Whether two sets are equal

CollectionUtils.isEqualCollection(listA,listB)

public static boolean isEqualCollection(final Collection a, final Collection b) {
    if(a.size() != b.size()) {
        return false;
    } else {
        Map mapa = getCardinalityMap(a);
        Map mapb = getCardinalityMap(b);
        if(mapa.size() != mapb.size()) {
            return false;
        } else {
            Iterator it = mapa.keySet().iterator();
            while(it.hasNext()) {
                Object obj = it.next();
                if(getFreq(obj,mapa) != getFreq(obj,mapb)) {
                    return false;
                }
            }
            return true;
        }
    }
}

Add element when not empty

CollectionUtils.addIgnoreNull(personList,o);

public static boolean addIgnoreNull(Collection collection, Object object) {
    return (object == null ? false : collection.add(object));
}

Judge whether there is intersection between two sets

CollectionUtils.containsAny(a,b)

public static boolean containsAny(final Collection coll1, final Collection coll2) {
    if (coll1.size() < coll2.size()) {
        for (Iterator it = coll1.iterator(); it.hasNext();) {
            if (coll2.contains(it.next())) {
                return true;
            }
        }
    } else {
        for (Iterator it = coll2.iterator(); it.hasNext();) {
            if (coll1.contains(it.next())) {
                return true;
            }
        }
    }
    return false;
}

Is it a subset

CollectionUtils.isSubCollection(a,b)

public static boolean isSubCollection(final Collection a, final Collection b) {
    Map mapa = getCardinalityMap(a);
    Map mapb = getCardinalityMap(b);
    Iterator it = a.iterator();
    while (it.hasNext()) {
        Object obj = it.next();
        if (getFreq(obj, mapa) > getFreq(obj, mapb)) {
            return false;
        }
    }
    return true;
}

Is it a true subset

CollectionUtils.isProperSubCollection(a,b)

public static boolean isProperSubCollection(final Collection a, final Collection b) {
    return (a.size() < b.size()) && CollectionUtils.isSubCollection(a,b);
}

The number of times an object appears in the collection

CollectionUtils.cardinality(a,b)

public static int cardinality(Object obj, final Collection coll) {
    if (coll instanceof Set) {
        return (coll.contains(obj) ? 1 : 0);
    }
    if (coll instanceof Bag) {
        return ((Bag) coll).getCount(obj);
    }
    int count = 0;
    if (obj == null) {
        for (Iterator it = coll.iterator();it.hasNext();) {
            if (it.next() == null) {
                count++;
            }
        }
    } else {
        for (Iterator it = coll.iterator();it.hasNext();) {
            if (obj.equals(it.next())) {
                count++;
            }
        }
    }
    return count;
}

Find the first element in the collection that meets the criteria

CollectionUtils.find(a,predicate)

public static Object find(Collection collection, Predicate predicate) {
    if (collection != null && predicate != null) {
        for (Iterator iter = collection.iterator(); iter.hasNext();) {
            Object item = iter.next();
            if (predicate.evaluate(item)) {
                return item;
            }
        }
    }
    return null;
}

Performs the specified closure on each element in the collection

CollectionUtils.forAllDo(a,closure)

public static void forAllDo(Collection collection, Closure closure) {
    if (collection != null && closure != null) {
        for (Iterator it = collection.iterator(); it.hasNext();) {
            closure.execute(it.next());
        }
    }
}

Filter out some elements that do not meet the specified conditions

CollectionUtils.filter(a,predicate)

public static void filter(Collection collection, Predicate predicate) {
    if (collection != null && predicate != null) {
        for (Iterator it = collection.iterator(); it.hasNext();) {
            if (predicate.evaluate(it.next()) == false) {
                it.remove();
            }
        }
    }
}

Perform the specified operation on each element in the collection, which will affect the incoming collection

CollectionUtils.transform(a,transformer)

public static void transform(Collection collection, Transformer transformer) {
    if (collection != null && transformer != null) {
        if (collection instanceof List) {
            List list = (List) collection;
            for (ListIterator it = list.listIterator(); it.hasNext();) {
                it.set(transformer.transform(it.next()));
            }
        } else {
            Collection resultCollection = collect(collection, transformer);
            collection.clear();
            collection.addAll(resultCollection);
        }
    }
}

Returns the number of elements that meet the specified criteria

CollectionUtils.countMatches(a,predicate)

public static int countMatches(Collection inputCollection, Predicate predicate) {
    int count = 0;
    if (inputCollection != null && predicate != null) {
        for (Iterator it = inputCollection.iterator(); it.hasNext();) {
            if (predicate.evaluate(it.next())) {
                count++;
            }
        }
    }
    return count;
}

Determine whether there is an element in the set that meets a specified condition

CollectionUtils.exists(a,predicate)

public static boolean exists(Collection collection, Predicate predicate) {
    if (collection != null && predicate != null) {
        for (Iterator it = collection.iterator(); it.hasNext();) {
            if (predicate.evaluate(it.next())) {
                return true;
            }
        }
    }
    return false;
}

Find all elements in the collection that meet the specified criteria

CollectionUtils.select(a,predicate)

public static Collection select(Collection inputCollection, Predicate predicate) {
    ArrayList answer = new ArrayList(inputCollection.size());
    select(inputCollection, predicate, answer);
    return answer;
}

Find all elements in the collection that do not meet the conditions

CollectionUtils.selectRejected(a,predicate)

public static Collection selectRejected(Collection inputCollection, Predicate predicate) {
    ArrayList answer = new ArrayList(inputCollection.size());
    selectRejected(inputCollection, predicate, answer);
    return answer;
}

Reverses the order of a given array

CollectionUtils.reverseArray(Object[] array)

public static void reverseArray(Object[] array) {
    int i = 0;
    int j = array.length - 1;
    Object tmp;

    while (j > i) {
        tmp = array[j];
        array[j] = array[i];
        array[i] = tmp;
        j--;
        i++;
    }
}
Published 5 original articles, praised 0, visited 47
Private letter follow

Tags: Java

Posted on Sun, 15 Mar 2020 01:33:44 -0700 by selsdon