title: Algorithm for combination
body: <p>I have designed an algorithm in java I generate all combinations of elements of a list . For example whereas the elements <code>[A, B , C]</code> it generates combinations , <code>[A]</code> , <code>[B]</code> , <code>[C]</code> , <code>[AB]</code> , <code>[BC]</code> , <code>[ABC]</code> . Or based on the elements <code>[A, A, B]</code> generates combinations <code>[A]</code> , <code>[B]</code> , <code>[AA]</code> , <code>[AB]</code> , <code>[AAB]</code> .
  This is my code to generate combinations .</p>
  
  <pre><code>private List&lt;Elemento&gt; combinazioneMassima = new ArrayList&lt;&gt;();
      private Log logger = LogFactory.getLog(Combinazioni3.class);
  
      public Combinazioni3(List&lt;Elemento&gt; generaCombinazioneMassima) {
          this.combinazioneMassima = generaCombinazioneMassima;
      }
  
      public void combine() {
          this.findAllCombinations(combinazioneMassima);
      }
  
      private static class Node{
          int lastIndex = 0;
      List&lt;Elemento&gt; currentList;
      public Node(int lastIndex, List&lt;Elemento&gt; list) {
              this.lastIndex = lastIndex;
              this.currentList = list;
      }
      public Node(Node n) {
              this.lastIndex = n.lastIndex;
              this.currentList = new ArrayList&lt;Elemento&gt;(n.currentList);
      }
      }
  
      public void findAllCombinations(List&lt;Elemento&gt; combinazioni) {
          Date dataInizio = new Date();
          List&lt;List&lt;Elemento&gt;&gt; resultList = new ArrayList&lt;List&lt;Elemento&gt;&gt;();
          LinkedList&lt;Node&gt; queue = new LinkedList&lt;Node&gt;();
          int n = combinazioni.size();
          ArrayList&lt;Elemento&gt; temp = new ArrayList&lt;Elemento&gt;();
          temp.add(combinazioni.get(0));
          queue.add(new Node(0, temp));
          // add all different integers to the queue once.
          for(int i=1;i&lt;n;++i) {
                  if(combinazioni.get(i-1) == combinazioni.get(i)) continue;
                  temp = new ArrayList&lt;Elemento&gt;();
                  temp.add(combinazioni.get(i));
                  queue.add(new Node(i, temp));
          }
          // do bfs until we have no elements
          while(!queue.isEmpty()) {
                  Node node = queue.remove();
                  if(node.lastIndex+1 &lt; n) {
                          Node newNode = new Node(node);
                          newNode.lastIndex = node.lastIndex+1;
                          newNode.currentList.add(combinazioni.get(node.lastIndex+1));
                          queue.add(newNode);
                  }
                  for(int i=node.lastIndex+2;i&lt;n;++i) {
                          if(combinazioni.get(i-1) == combinazioni.get(i)) continue;
                          // create a copy and add extra integer
                          Node newNode = new Node(node);
                          newNode.lastIndex = i;
                          newNode.currentList.add(combinazioni.get(i));
                          queue.add(newNode);
                  }
                  GestoreRegole gestoreRegole = new GestoreRegole();
                  gestoreRegole.esegui(node.currentList);
          }
  
      }
  </code></pre>
  
  <p>But however for <code>input &gt; 250</code> the program stops and takes too long to make all combinations .
  How can I improve this solution ? Or is there a better solution ?</p>