1

From several lists I need to create a single result list with the values of all of the others, by choosing the data with a round robin algorithm.

list1 = val1_1,val1_2 .. 
list2 = val2_1,val2_2 .. 
list3 = val3_1,val3_2 ..
//rr choosing
result = val1_1,val2_1,val3_1,val1_2,val2_2,val3_2,val1_3...

The number of values in each list may be different. How can I do this?

cigien
  • 57,834
  • 11
  • 73
  • 112
SWeC
  • 165
  • 2
  • 15

3 Answers3

3

You could use a Queue of Iterators derived from the individual lists. Get the next iterator from the queue, get the next element from the iterator, and add it back to the queue if it is not empty.

List<String> lst1 = new ArrayList<>(Arrays.asList("a", "b", "c"));
List<String> lst2 = new ArrayList<>(Arrays.asList("A", "B"));
List<String> lst3 = new ArrayList<>(Arrays.asList("1", "2", "3", "4"));

Queue<Iterator<String>> iters = new LinkedList<>(Stream.of(lst1, lst2, lst3)
        .map(List::iterator).collect(Collectors.toList()));

List<String> result = new LinkedList<>();
while (! iters.isEmpty()) {
    Iterator<String> iter = iters.poll();
    if (iter.hasNext()) {
        result.add(iter.next());
        iters.add(iter);
    }
}
System.out.println(result); // [a, A, 1, b, B, 2, c, 3, 4]
tobias_k
  • 81,265
  • 12
  • 120
  • 179
3

If you want to create a 1d list filled with data from the columns of a jagged 2d list, you can use two nested for-loops: first by columns, and then by rows. If you don't know the length of the maximum row in advance, i.e. the number of columns, you can iterate until the columns are still present at least in one row.

// input data
List<List<String>> lists = Arrays.asList(
        Arrays.asList("a1", "b1", "c1"),
        Arrays.asList("a2", "b2"),
        Arrays.asList("a3", "b3", "c3", "d3"));

// result list
List<String> listRobin = new ArrayList<>();

// iteration over the columns of a 2d list until
// the columns are still present at least in one row
for (int col = 0; ; col++) {
    // whether the columns are still present
    boolean max = true;
    // iteration over the rows, i.e. lists
    for (List<String> list : lists) {
        // if column is present
        if (col < list.size()) {
            // take value from this column
            listRobin.add(list.get(col));
            // columns are still present
            max = false;
        }
    }
    // if there are no more columns
    if (max) break;
}

// output
System.out.println(listRobin);
// [a1, a2, a3, b1, b2, b3, c1, c3, d3]

See also:
Join lists in java in round robin fashion
How do you rotate a 2D array 90 degrees without using a storage array?

2

List Having Same Lengths

public static void main(String[] args) {
    String[][] values = new String[][] {
        { "1_1", "1_2", "1_3" },
        { "2_1", "2_2", "2_3" },
        { "3_1", "3_2", "3_3" }
    };

    for (int count = 0; count < values.length * values[0].length; count++) {
        System.out.println(values[count % values.length][count / values[0].length]);
    }
}

The expression:

count % values.length

does rotate between all rows, while the expression:

count / values[0].length

increases by one after row many iterations.

List Having Different Lengths

public static void main(String[] args) {
    String[][] values = new String[][] {
        { "1_1", "1_2", "1_3" },
        { "2_1", "2_2" },
        { "3_1", "3_2", "3_3", "3_4" }
    };

    for (int count = 0, maxLen = 0;; count++) {
        int row = count % values.length;
        int col = count / values[0].length;
        maxLen = Math.max(values[row].length, maxLen);
        if (values[row].length > col) {
            System.out.println(values[row][col]);
        } else if (row + 1 == values.length && col >= maxLen) break;
    }
}

The differences to the solution provided for list having same lengths are:

  • fetch a value only if the current list defines the computed column.
  • collect the maximum length of all lists while iterating over the values.
  • stop if no list exists having defining currently computed column.
Harmlezz
  • 7,972
  • 27
  • 35
  • "List Having Different Lengths" code is buggy you can see output if 2nd array row to be first row. like { "2_1", "2_2" }, >> { "1_1", "1_2", "1_3" }, https://www.mycompiler.io/view/97paDZbDJPU – Rikin Patel Nov 22 '22 at 17:09