2

I have an ArrayList of ArrayList<String>

ArrayList<String> will be having the same size

I want to create new ArrayList<ArrayList<String>>

For example:-

I have this list:-

[
    ["a", "1"],
    ["b", "1"],
    ["c", "1"]
]

and I want output as below :-

[
    ["a", "b", "c"],
    ["1", "1", "1"]
]
1
  • In matrix terminology, what you want is the transpose. (Also, as a matter of style, it's usually much better to refer the interfaces instead of concrete implementations: List<List<String>>.) Commented Mar 27, 2020 at 22:32

4 Answers 4

2

This will handle lists of arbitrary and uneven size.

Strategy: Remove the inner lists while adding original indices to each item. Group them by their original indices. Finally, strip the indices.

val pivoted = lists.flatMap { it.withIndex() }
    .groupBy { it.index }
    .values.map { list -> list.map { it.value } }

Console:

[[a, 1], [b, 2, β], [c, 3, γ]] // Input
[[a, b, c], [1, 2, 3], [β, γ]] // Output
Sign up to request clarification or add additional context in comments.

Comments

1

More generic way that can handle longer inner lists.


    @Test
    fun `Test groupByIndexed`() {
        val mapOfLists = arrayListOf(
                arrayListOf("a", "1"),
                arrayListOf("b", "2"),
                arrayListOf("c", "3")
            )
            .asSequence()
            .flatMap { it.asSequence() }
            .groupByIndexed { idx, _ ->
                idx % 2 // Use how many elements have each inner list
            }

        // Convert Map<List<String>> to desired ArrayList<ArrayList<String>>
        val arrayListOfArrayLists = ArrayList<ArrayList<String>>()
        mapOfLists
            .asSequence()
            .map {
                ArrayList<String>().apply {
                    addAll(it.component2())
                }
            }
            .toCollection(arrayListOfArrayLists)



        Assertions.assertEquals(2, arrayListOfArrayLists.size)
        Assertions.assertEquals(arrayListOf("a", "b", "c"), arrayListOfArrayLists[0])
        Assertions.assertEquals(arrayListOf("1", "2", "3"), arrayListOfArrayLists[1])
    }

    private fun <T, K> Sequence<T>.groupByIndexed(keySelector: (Int, T) -> K): Map<K, List<T>> {
        val data = LinkedHashMap<K, MutableList<T>>()

        return foldIndexed(data) { idx, acc, str ->
            val key = keySelector(idx, str)
            val list = acc.getOrPut(key) { mutableListOf() }
            list.add(str)
            acc
        }
    }

Comments

1
@Test
fun rotate2dList() {
    val old = listOf(
            listOf("a", "1"),
            listOf("b", "1"),
            listOf("c", "1")
    )

    val N = old[0].size // determine size of new list

    val new = old
            .asSequence()
            .flatMap { it.asSequence() }
            .withIndex()
            .groupBy { it.index % N }
            .values
            .map { it.map { it.value } }

    assertEquals(N, new.size)
    assertEquals(listOf("a", "b", "c"), new[0])
    assertEquals(listOf("1", "1", "1"), new[1])
}

Comments

0

For example in that way:

val lista = arrayListOf(
            arrayListOf("a","1"),
            arrayListOf("b", "1"),
            arrayListOf("c","1")
    )


    val newList = ArrayList<ArrayList<String>>()
    newList.add(
            lista.map { it[0] }.toMutableList() as ArrayList<String>
    )
    newList.add(lista.map { it[1] }.toMutableList() as ArrayList<String>)

    newList.forEach {
        println(it)
    }

Output:

[a, b, c]
[1, 1, 1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.