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

org.metacsp.utility.PowerSet Maven / Gradle / Ivy

There is a newer version: 1.3.5
Show newest version
package org.metacsp.utility;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class PowerSet {
	
	public static  Set> powerSet(Set originalSet) {
	    Set> sets = new HashSet>();
	    if (originalSet.isEmpty()) {
	        sets.add(new HashSet());
	        return sets;
	    }
	    List list = new ArrayList(originalSet);
	    T head = list.get(0);
	    Set rest = new HashSet(list.subList(1, list.size())); 
	    for (Set set : powerSet(rest)) {
	    	Set newSet = new HashSet();
	        newSet.add(head);
	        newSet.addAll(set);
	        sets.add(newSet);
	        sets.add(set);
	    }           
	    return sets;
	}


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy