Skip to content

Instantly share code, notes, and snippets.

@hatakawas
Last active February 23, 2017 10:42
Show Gist options
  • Select an option

  • Save hatakawas/3f4a6f6b0668ead2bf6c0260ba2009a7 to your computer and use it in GitHub Desktop.

Select an option

Save hatakawas/3f4a6f6b0668ead2bf6c0260ba2009a7 to your computer and use it in GitHub Desktop.

Revisions

  1. hatakawas revised this gist Feb 23, 2017. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion Subset.java
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    public class Subset {

    @SafeVarargs
    private final <T> List<List<T>> subsets(T... items) {
    public static <T> List<List<T>> subsets(T... items) {
    List<T> elements = new ArrayList<>(new HashSet<>(Arrays.asList(items)));
    List<List<T>> results = new ArrayList<>();
    for (T e : elements) {
    @@ -15,4 +16,11 @@ private final <T> List<List<T>> subsets(T... items) {
    }
    return results;
    }

    public static void main(String[] args) {
    List<List<String>> results = subsets("a", "b", "c");
    for (List<String> result : results) {
    System.out.println(result);
    }
    }
    }
  2. hatakawas revised this gist Feb 23, 2017. 1 changed file with 15 additions and 13 deletions.
    28 changes: 15 additions & 13 deletions Subset.java
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,18 @@
    @SafeVarargs
    private final <T> List<List<T>> subsets(T... items) {
    List<T> elements = new ArrayList<>(new HashSet<>(Arrays.asList(items)));
    List<List<T>> results = new ArrayList<>();
    for (T e : elements) {
    // Record size as the list will change
    int size = results.size();
    for (int i = 0; i < size; i++) {
    List<T> subset = new ArrayList<>(results.get(i));
    subset.add(e);
    results.add(subset);
    public class Subset {
    @SafeVarargs
    private final <T> List<List<T>> subsets(T... items) {
    List<T> elements = new ArrayList<>(new HashSet<>(Arrays.asList(items)));
    List<List<T>> results = new ArrayList<>();
    for (T e : elements) {
    // Record size as the list will change
    int size = results.size();
    for (int i = 0; i < size; i++) {
    List<T> subset = new ArrayList<>(results.get(i));
    subset.add(e);
    results.add(subset);
    }
    results.add(Collections.singletonList(e));
    }
    results.add(Collections.singletonList(e));
    return results;
    }
    return results;
    }
  3. hatakawas created this gist Feb 23, 2017.
    16 changes: 16 additions & 0 deletions Subset.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    @SafeVarargs
    private final <T> List<List<T>> subsets(T... items) {
    List<T> elements = new ArrayList<>(new HashSet<>(Arrays.asList(items)));
    List<List<T>> results = new ArrayList<>();
    for (T e : elements) {
    // Record size as the list will change
    int size = results.size();
    for (int i = 0; i < size; i++) {
    List<T> subset = new ArrayList<>(results.get(i));
    subset.add(e);
    results.add(subset);
    }
    results.add(Collections.singletonList(e));
    }
    return results;
    }