Obtaining a powerset of a set in Java

后端 未结 26 1739
青春惊慌失措
青春惊慌失措 2020-11-22 11:33

The powerset of {1, 2, 3} is:

{{}, {2}, {3}, {2, 3}, {1, 2}, {1, 3}, {1, 2, 3}, {1}}

Let\'s say I have a Set in Java:<

26条回答
  •  忘了有多久
    2020-11-22 12:00

    If you're using Eclipse Collections (formerly GS Collections), you can use the powerSet() method on all SetIterables.

    MutableSet set = UnifiedSet.newSetWith(1, 2, 3);
    System.out.println("powerSet = " + set.powerSet());
    // prints: powerSet = [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
    

    Note: I am a committer for Eclipse Collections.

提交回复
热议问题