Time out while accessing huge list/collection

what is the best way to traverse huge list/collection in kotlin? is there any fast way available which reduce the time of traversing the list? I have tried parallel stream but it did not work …

I think we’d need to know more about your particular problem. Which list class are you using, what does it contain, and what are you doing with each element in your traversal?

Maybe using another data structure might help? Map for example.

Without a full image of the problem it’s hard to make a diagnosis. Parallel streams don’t automatically speed-up traversals.

We’re going to make our own Contacts application! The application must perform two types of operations:

  1. add name , where name is a string denoting a contact name. This must store as a new contact in the application.
  2. find partial , where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting with partial and print the count on a new line.

Given n sequential add and find operations, perform each operation in order.

sample input is in 2d array```
add Raj
add Rajxyz
find Raj
find Rak
output would be:

My solution is working if add and find operation little in count ,test case with too many add/find operations producing timeout

fun contacts(queries: Array<Array>): Array {

      val contact = ArrayList<String>()
    val find = ArrayList<Int>()
    queries.forEachIndexed { index, strings ->
       if (strings.get(0)=="add") {
      var count= contact.parallelStream().filter { it.startsWith(queries[index][1]) }


    return find.toTypedArray();


In a case like the above, I’d expect the overhead of starting threads, distributing work to them, and collecting their results is likely to be far, far greater than any time saved from running single startsWith() tests in parallel.

How many contacts will be in your list? If it’s just a personal app with only hundreds or thousands of contacts, performance really shouldn’t be a problem (unless there’s some dodgy coding somewhere).

How often will you be searching, compared to adding new elements? If searching will be much more frequent, you might consider using a data structure optimised for it, such as a trie.