Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8119415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:47:48+00:00 2026-06-06T04:47:48+00:00

Using guava 12 Collections2.permutations() , I’m wondering if it’s possible to limit the size

  • 0

Using guava 12 Collections2.permutations(), I’m wondering if it’s possible to limit the size of the permutations ?

More precisely, I would like to get a list of k-sized permutations within a list of n elements, instead of getting a list of all n-sized permutations.

Currently, if I pass a list of 4 fruits, permutations() will currently return a list of 24 4-sized permutations although I’m only interested in retrieving, say, the 4 unique size 3 permutations.

Say I have a list of 4 fruits :

["Banana", "Apple", "Orange", "Peach"]

If I’m only interested in size 3 permutations, I’d like the following returned :

["Banana", "Apple", "Orange"]
["Banana", "Apple", "Peach"]
["Banana", "Orange", "Peach"]
["Apple", "Orange", "Peach"]

Could anyone please provide any hints to a solution ? Thanks !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:47:50+00:00Added an answer on June 6, 2026 at 4:47 am

    This code works out the variations, then runs the permutations on each unique set of 3.

    i.e. for “A”, “B”, “C”, “D” the possibilities are [[A, B, C], [A, B, D], [A, C, D], [B, C, D]]. We then calculate the permutations on each threesome (or n-some) and append the possibilities to a list.

    PermutationsOfN.processSubsets( List set, int k ) returns:
    [[A, B, C], [A, B, D], [A, C, D], [B, C, D]]

    Taking it a bit further PermutationsOfN.permutations( List list, int size ) returns:
    [[A, B, C], [A, C, B], [C, A, B], [C, B, A], [B, C, A], [B, A, C], [A, B, D], [A, D, B], [D, A, B], [D, B, A], [B, D, A], [B, A, D], [A, C, D], [A, D, C], [D, A, C], [D, C, A], [C, D, A], [C, A, D], [B, C, D], [B, D, C], [D, B, C], [D, C, B], [C, D, B], [C, B, D]]

    import java.util.Collection;
    import java.util.List;
    
    import com.google.common.collect.Collections2;
    import com.google.common.collect.ImmutableList;
    import com.google.common.collect.Lists;
    
    public class PermutationsOfN<T> {
      public static void main( String[] args ) {
        List<String> f = Lists.newArrayList( "A", "B", "C", "D" );
        PermutationsOfN<String> g = new PermutationsOfN<String>();
        System.out.println( String.format( "n=1 subsets %s", g.processSubsets( f, 1 ) ) );
        System.out.println( String.format( "n=1 permutations %s", g.permutations( f, 1 ) ) );
        System.out.println( String.format( "n=2 subsets %s", g.processSubsets( f, 2 ) ) );
        System.out.println( String.format( "n=2 permutations %s", g.permutations( f, 2 ) ) );
        System.out.println( String.format( "n=3 subsets %s", g.processSubsets( f, 3 ) ) );
        System.out.println( String.format( "n=3 permutations %s", g.permutations( f, 3 ) ) );
        System.out.println( String.format( "n=4 subsets %s", g.processSubsets( f, 4 ) ) );
        System.out.println( String.format( "n=4 permutations %s", g.permutations( f, 4 ) ) );
        System.out.println( String.format( "n=5 subsets %s", g.processSubsets( f, 5 ) ) );
        System.out.println( String.format( "n=5 permutations %s", g.permutations( f, 5 ) ) );
      }
    
      public List<List<T>> processSubsets( List<T> set, int k ) {
        if ( k > set.size() ) {
          k = set.size();
        }
        List<List<T>> result = Lists.newArrayList();
        List<T> subset = Lists.newArrayListWithCapacity( k );
        for ( int i = 0; i < k; i++ ) {
          subset.add( null );
        }
        return processLargerSubsets( result, set, subset, 0, 0 );
      }
    
      private List<List<T>> processLargerSubsets( List<List<T>> result, List<T> set, List<T> subset, int subsetSize, int nextIndex ) {
        if ( subsetSize == subset.size() ) {
          result.add( ImmutableList.copyOf( subset ) );
        } else {
          for ( int j = nextIndex; j < set.size(); j++ ) {
            subset.set( subsetSize, set.get( j ) );
            processLargerSubsets( result, set, subset, subsetSize + 1, j + 1 );
          }
        }
        return result;
      }
    
      public Collection<List<T>> permutations( List<T> list, int size ) {
        Collection<List<T>> all = Lists.newArrayList();
        if ( list.size() < size ) {
          size = list.size();
        }
        if ( list.size() == size ) {
          all.addAll( Collections2.permutations( list ) );
        } else {
          for ( List<T> p : processSubsets( list, size ) ) {
            all.addAll( Collections2.permutations( p ) );
          }
        }
        return all;
      }
    }
    

    A special mention goes to meriton whose answer here helped me work it out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Like the title says, i would like to get a thread-safe HashSet using Guava
Using Guava, I would like to have a map that has the following properties:
Using Jenkins or Hudson I would like to create a pipeline of builds with
Can I easily convert InputStream to BufferedReader using Guava? I'm looking for something like:
I am trying to get a date range using Guava's new Range functionality, via
what is the simple way to do lexicographical ordering of string list using guava.
I have a project which would like to utilize the Google Guava libraries (on
We have a List of items: List<X> from this List, we would like to
I would like to know if there is currently a way with Guava MapMaker
I was using BloomFilter in guava v.11.0.1 and it seems like I am getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.