Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>EDIT:</strong> As FearUs pointed out, a better solution is to use Guava's <a href="https://google.github.io/guava/releases/19.0/api/docs/com/google/common/collect/Sets.html#powerSet(java.util.Set)" rel="noreferrer">Sets.powerset(Set set)</a>.</p> <p><strong>EDIT 2:</strong> Updated links.</p> <hr> <p>Quick and dirty translation of <a href="http://web.archive.org/web/20100206003803/http://davidtran.doublegifts.com/blog/?p=144" rel="noreferrer">this solution</a>:</p> <pre><code>public static void main(String[] args) { List&lt;List&lt;String&gt;&gt; powerSet = new LinkedList&lt;List&lt;String&gt;&gt;(); for (int i = 1; i &lt;= args.length; i++) powerSet.addAll(combination(Arrays.asList(args), i)); System.out.println(powerSet); } public static &lt;T&gt; List&lt;List&lt;T&gt;&gt; combination(List&lt;T&gt; values, int size) { if (0 == size) { return Collections.singletonList(Collections.&lt;T&gt; emptyList()); } if (values.isEmpty()) { return Collections.emptyList(); } List&lt;List&lt;T&gt;&gt; combination = new LinkedList&lt;List&lt;T&gt;&gt;(); T actual = values.iterator().next(); List&lt;T&gt; subSet = new LinkedList&lt;T&gt;(values); subSet.remove(actual); List&lt;List&lt;T&gt;&gt; subSetCombination = combination(subSet, size - 1); for (List&lt;T&gt; set : subSetCombination) { List&lt;T&gt; newSet = new LinkedList&lt;T&gt;(set); newSet.add(0, actual); combination.add(newSet); } combination.addAll(combination(subSet, size)); return combination; } </code></pre> <p>Test:</p> <pre><code>$ java PowerSet ted williams golden [[ted], [williams], [golden], [ted, williams], [ted, golden], [williams, golden], [ted, williams, golden]] $ </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload