Uploaded image for project: 'Commons Collections'
  1. Commons Collections
  2. COLLECTIONS-858

CartesianProductIterator

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Resolved
    • Trivial
    • Resolution: Fixed
    • None
    • 4.5.0-M3
    • Iterator
    • None

    Description

      The CartesianProductIterator is suitable to enhance a deeply-nested for-loops over a set of collections.

      Given

      var a = new Arrays.asList('a1', 'a2', 'a3');
      var b = new Arrays.asList('b1', 'b2', 'b3');
      var c = new Arrays.asList('c1', 'c2', 'c3');
      var d = new Arrays.asList('d1', 'd2', 'd3');
      var e = new Arrays.asList('e1', 'e2', 'e3');
      var f = new Arrays.asList('f1', 'f2', 'f3');
      

       
      instead of

      for (var aElement : a) {
          for (var bElement : b) {
              for (var cElement : c) {
                  for (var dElement : d) {
                      for (var eElement : e) {
                          for (var fElement : f) {
                              // ...
                          }
                      }
                  }
              }
          }
      }
      

      it would look like:

      var it = new CartesianProductIterator<>(a, b, c, d, e, f);
      while (it.hasNext()) {
          var elements = it.next();
      }
      

       
      Extremely useful for nesting levels 10+.

      https://github.com/apache/commons-collections/pull/509

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              alexey-pelykh Alexey Pelykh
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: