tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Tools and Libs > Google Guava > Collections > Power Set

Power Set 

Google Guava is a java library with lot of utilities and reusable components. This requires the library guava-10.0.jar to be in classpath. The following example shows using Sets.powerSet() API. It returns the set of all possible subsets of the given set.

File Name  :  
com/bethecoder/tutorials/guava/collection_tests/PowerSetTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.guava.collection_tests;

import java.util.Set;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;

public class PowerSetTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    
    ImmutableSet<Integer> intSet = ImmutableSet.of(123);
    System.out.println(intSet);
    
    System.out.println("\nPower Set : ");
    Set<Set<Integer>> intPowerSet = Sets.powerSet(intSet);
    for (Set<Integer> subSet : intPowerSet) {
      System.out.println(subSet);
    }
  }

}
   

It gives the following output,
[1, 2, 3]

Power Set : 
[]
[1]
[2]
[1, 2]
[3]
[1, 3]
[2, 3]
[1, 2, 3]



 
  


  
bl  br