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

com.penglecode.codeforce.common.util.CollectionUtils Maven / Gradle / Ivy

The newest version!
package com.penglecode.codeforce.common.util;

import org.springframework.util.Assert;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author pengpeng
 * @version 1.0
 */
@SuppressWarnings("ConstantConditions")
public class CollectionUtils extends org.springframework.util.CollectionUtils {

    /**
     * 

如果collection为null/empty则返回defaultValue否则原值返回

* @param collection * @param defaultValue * @return */ public static Collection defaultIfEmpty(Collection collection, Collection defaultValue) { return isEmpty(collection) ? defaultValue : collection; } /** *

如果collection为null/empty则返回defaultValue否则原值返回

* @param collection * @param defaultValue * @return */ public static List defaultIfEmpty(List collection, List defaultValue) { return isEmpty(collection) ? defaultValue : collection; } /** *

如果collection为null/empty则返回defaultValue否则原值返回

* @param collection * @param defaultValue * @return */ public static Set defaultIfEmpty(Set collection, Set defaultValue) { return isEmpty(collection) ? defaultValue : collection; } /** *

如果map为null/empty则返回defaultValue否则原值返回

* @param map * @param defaultValue * @return */ public static Map defaultIfEmpty(Map map, Map defaultValue) { return isEmpty(map) ? defaultValue : map; } /** * Adapts an {@code Iterator} to the {@code Enumeration} interface. * * @param iterator * @param * @return */ public static Enumeration asEnumeration(final Iterator iterator) { Objects.requireNonNull(iterator); return new Enumeration() { @Override public boolean hasMoreElements() { return iterator.hasNext(); } @Override public T nextElement() { return iterator.next(); } }; } /** * 求笛卡尔积 * * @param groupedLists * @param * @return */ public static List> cartesianProduct(List> groupedLists) { if(isEmpty(groupedLists)) { return Collections.emptyList(); } Assert.noNullElements(groupedLists, "The element of 'groupedLists' must be not empty!"); //利用LinkedList的poll()特性 LinkedList> groupedElements = new LinkedList<>(groupedLists); //为递归中的flatMap创造条件,假设groupedElements.poll() => ["1","2","3"], 则startElements => [["1"],["2"],["3"]] List> startElements = groupedElements.poll().stream() .map(Collections::singletonList) .collect(Collectors.toList()); List> resultElements = new ArrayList<>(); recursiveCartesianProduct(startElements, groupedElements, resultElements); return resultElements; } /** * 递归求取笛卡尔积 * @param startElements * @param remainingElements * @param resultElements * @param */ private static void recursiveCartesianProduct(List> startElements, LinkedList> remainingElements, List> resultElements) { if(!isEmpty(remainingElements)) { List remainingFirst = remainingElements.poll(); List> newStartElements = startElements.stream() //利用flatMap将诸如:[["1"],"红"] ==flatMap()==> ["1","红"] //利用flatMap将诸如:[["1","红"],"A"] ==flatMap()==> ["1","红","A"] .flatMap(startElement -> remainingFirst.stream().map(element -> { List subCartesian = new ArrayList<>(startElement); subCartesian.add(element); return subCartesian; })).collect(Collectors.toList()); recursiveCartesianProduct(newStartElements, remainingElements, resultElements); } else { resultElements.addAll(startElements); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy