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 6373641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:23:14+00:00 2026-05-25T01:23:14+00:00

I have an arraylist of objects that I want to create all possible combinations

  • 0

I have an arraylist of objects that I want to create all possible combinations (according to a simple set of rules). Each object that is stored in the list holds a squadNumber and a string. Here is an example of a typical list I am storing:

0: 1, A
1: 1, B
2: 2, A
3: 2, B
4: 3, C
5: 3, D
6: 4, C
7: 4, D

I want to get all the combinations where each squadNumber can only be present once, for example: (1,A),(2,A),(3,C),(4,C) then the next combination would be (1,A),(2,A),(3,C),(4,D).
How would I go about this in java? Usually I would use a nested loop, but the fact that it’s all being stored in one list complicates things for me.

Thanks,
paintstripper

  • 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-05-25T01:23:15+00:00Added an answer on May 25, 2026 at 1:23 am

    EDITED

    Algorithm is following:

    1. Split all squads by numbers. So we have list with squads for 1,
      another list for squads 2, etc.
    2. Run dfs. At nth step we add squads with nth number.

    Code

    // Split squads by numbers, so we can iterate through each number independently.
    private Map<Integer, List<Squad>> splitSquadsByNumbers(List<Squad> squads) {
        Map<Integer, List<Squad>> res = new HashMap<Integer, List<Squad>>();
        for (Squad squad : squads) {
            if (res.get(squad.getNumber()) == null) {
                res.put(squad.getNumber(), new ArrayList<Squad>());
            }
            res.get(squad.getNumber()).add(squad);
        }
        return res;
    }
    
    List<Integer> squadNumbers;
    Map<Integer, List<Squad>> squadsByNumbers;
    Stack<Squad> stack;
    
    // Iterating through each squad with number squadNumbers[position] and try to add to stack, at the end pop it from stack.
    
    private void dfs(int position) {
        if (position == squadNumber.size()) {
            System.out.println(stack.toString());
        } else {
            for (Squad squad : squadsByNumbers.get(squadNumber.get(position))) {
                stack.push(squad);
                dfs(position + 1);
                stack.pop();
            }
        }
    }
    
    private void main(List<Squad> squads) {
        squadsByNumbers = splitSquadsByNumbers(squads);
        squadNumber = new ArrayList(squadsByNumber.keySet());
        Collections.sort(squadNumbers);
        stack = new Stack<Squad>();
        dfs(0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ArrayList that I want to use to hold RaceCar objects that
I have an ArrayList containing Objects that have a date value. Now I want
I have an arraylist that contains items called Room. Each Room has a roomtype
I am wanting to create a Spinner from an ArrayList of Objects I have
I want to convert a List to a List so that each object on
Say I have an ArrayList of USBDevice Objects. Each USBDevice has ProductID and VendorID
i have an arraylist of objects, each containing a year/month/day/hour property. what i'm ultimately
I have an object that contains a few unserializable fields that I want to
I have an application that uses a service to create an ArrayList of custom
Ok, so I have an ArrayList (arrBok) , which is full of book objects

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.