0

Given: 2 Lists of String. Required: List<Pair<String, String>> countryStatus.

Following the answer from @geco17 with two for loops I am getting into OutOfMemoryError: Java Heap Space due to the amount of data.

Is there any other way to do so and avoid OutOfMemory Error?

Pairs two different list into a single map

        for (String country : countrylist) {
            for (String status : statusList) {
                nodesCountryStatus.add(new Pair(country, status));
            }
        }

UPDATE: As a possible way to avoid this problem but to solve the task I had not straightforward I did the following - concatinating two lists by each element in sequence Zipping streams using JDK8 with lambda (java.util.stream.Streams.zip). I was to have the output in csv thus commas appropriate:

if (countries.size() == statuses.size()) { countryStatusAsExtracted = Streams.zip(countries.stream(), statuses.stream(), (a, b) -> (a + "," + b)) .collect(Collectors.toList());}

JuliaN
  • 1
  • 1
  • Well, the result of creating every single pair of 2 lists with length N and M will result in N*M `Pair` objects. This will be problematic for big numbers of N an M, what numbers are we talking about here? You can assign more maximum memory to your JVM, but after all it's limited by your machine's hardware. – f1sh May 30 '22 at 08:48
  • @f1sh, 7200 for N and 7200 for M – JuliaN May 30 '22 at 09:36

1 Answers1

0

Since you already know how long your resulting list will be, you can initialize the ArrayList with the size. It should be country.size() * statusList.size. That way you avoid reallocating the list constantly.