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

de.invation.code.toval.misc.SetUtils Maven / Gradle / Ivy

Go to download

TOVAL comprises a set of java classes for common programming issues. It includes utils for arrays, lists, sets and collections for convenient handling and modification, but also support for mathematic definitions concerning logic (clauses + resolution) together with some algorithms for permutations, powersets and resolution. Additionally it contains a number of types for multisets, matrices with object keys and much more.

The newest version!
package de.invation.code.toval.misc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

import de.invation.code.toval.types.HashList;
import de.invation.code.toval.validate.Validate;


public class SetUtils {
	
	private static java.util.Random rand = new Random(); 
	
	/**
	 * Generates a subset of set, that contains a random number of elements.
	 * @param  Type of set elements
	 * @param set Basic set for operation
	 * @return A subset with a random number of elements
	 */
	public static  Set getRandomSubset(Set set){
		return getRandomSubsetMax(set, set.size());
	}
	
	/**
	 * Generates a random subset of set, that contains at most maxCount elements.
	 * @param  Type of set elements
	 * @param set Basic set for operation
	 * @param maxCount Maximum number of items
	 * @return A subset with at most maxCount elements
	 */
	public static  Set getRandomSubsetMax(Set set, int maxCount){
		int count = rand.nextInt(maxCount)+1;
		return getRandomSubset(set, count);	
	}
	
	/**
	 * Generates a random subset of set, that contains at least maxCount elements.
	 * @param  Type of set elements
	 * @param set Basic set for operation
	 * @param minCount Minimum number of items
	 * @return A subset with at least minCount elements
	 */
	public static  Set getRandomSubsetMin(Set set, int minCount){
		int count = RandomUtils.randomIntBetween(minCount, set.size());
		return getRandomSubset(set, count);	
	}
	
	/**
	 * Generates a random subset of set, that contains exactly maxCount elements.
	 * @param  Type of set elements
	 * @param set Basic set for operation
	 * @param count Number of items
	 * @return A subset with exactly count elements
	 */
	public static  Set getRandomSubset(Set set, int count){
		Set result = new HashSet();
		HashList opSet = new HashList(set);
		while(result.size() Type of set elements
	 * @param hashSet Underlying set of elements
	 * @return Powerset of set
	 */
	public static  PowerSet getPowerSet(Set hashSet) {
		Validate.notNull(hashSet);
		if(hashSet.size() == 0)
			throw new IllegalArgumentException("set size 0");
		HashList hashList = new HashList(hashSet);
		PowerSet result = new PowerSet(hashList.size());
		for (int i = 0; i < Math.pow(2, hashList.size()); i++) {
			int setSize = Integer.bitCount(i);
			HashSet newList = new HashSet(setSize);
			result.get(setSize).add(newList);
			for (int j = 0; j < hashList.size(); j++) {
				if ((i & (1 << j)) != 0) {
					newList.add(hashList.get(j));
				}
			}
		}
		return result;
	}
	
	public static synchronized  List> getKElementarySets(Set set, int k) {
		return ListUtils.getKElementaryLists(new ArrayList(set), k);
	}
	
	/**
	 * Basic class for representing a powerset (the set of all subsets of a set).
* Subsets are kept in a map grouped by their size which acts as map-key. * map-values are lists of {@link HashSet}s representing the subsets. * @param Type of set elements * * @author Thomas Stocker */ @SuppressWarnings("serial") public static class PowerSet extends HashMap>>{ /** * Creates a new PowerSet and prepares the subset-map. * @param setSize The size (number of elements) of the underlying set */ public PowerSet(int setSize){ for(int i=0; i<=setSize ; i++) { put(i, new ArrayList>()); } } /** * Returns a String representation of the powerset. * @return String representation of the powerset */ @Override public String toString() { StringBuilder builder = new StringBuilder(); String format = "%s: %s\n"; for(Integer i: keySet()) { builder.append(String.format(format, i, Arrays.toString(get(i).toArray()))); } return builder.toString(); } } /** * Determines the intersection of a collection of sets. * @param sets Basic collection of sets. * @return The set of common elements of all given sets. */ public static Set intersection(Collection> sets){ Set result = new HashSet(); if(sets.isEmpty()){ return result; } Iterator> iter = sets.iterator(); result.addAll(iter.next()); while(iter.hasNext()){ result.retainAll(iter.next()); } return result; } /** * Determines the intersection of a collection of sets. * @param sets Basic collection of sets. * @return The set of common elements of all given sets. */ public static Set intersection(Set... sets){ return intersection(Arrays.asList(sets)); } /** * Determines the union of a collection of sets. * @param sets Basic collection of sets. * @return The set of distinct elements of all given sets. */ public static Set union(Collection> sets){ Set result = new HashSet(); if(sets.isEmpty()){ return result; } Iterator> iter = sets.iterator(); result.addAll(iter.next()); if(sets.size() == 1){ return result; } while(iter.hasNext()){ result.addAll(iter.next()); } return result; } public static Set union(Set... sets){ List> list = new ArrayList>(sets.length); for(Set set: sets){ list.add(set); } return union(list); } public static boolean containSameElements(Collection> sets){ if(sets.isEmpty() || sets.size()<2) return false; Iterator> iterator = sets.iterator(); Set basicSet = iterator.next(); Set actualSet; while(iterator.hasNext()){ actualSet = iterator.next(); if(actualSet.size() != basicSet.size()) return false; if(!actualSet.containsAll(basicSet)) return false; } return true; } public static boolean containSameElements(Set... sets){ if(sets.length == 0 || sets.length<2) return false; Set basicSet = sets[0]; for(int i=1; i T getRandomElement(Set set){ ArrayList list = new ArrayList(); list.addAll(set); return list.get(rand.nextInt(list.size())); } /** * Checks if any two of the given sets intersect. * @param sets Basic collection of sets. * @return true if there is an intersection between at least two sets;
* false otherswise. */ public static boolean existPairwiseIntersections(Collection> sets){ //Determine all possible pairs of sets List>> setPairs = ListUtils.getKElementaryLists(new ArrayList>(sets), 2); for(Iterator>> iter = setPairs.iterator(); iter.hasNext();){ Set intersection = SetUtils.intersection(iter.next()); if(!intersection.isEmpty()){ return true; } } return false; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy