0

I want to optimise my code, and for that, I want to iterate parallely through Maps.

My current code is:

for (String orderKey: insertMap.keySet()) {
    if(orderKey.contains('#New')) {
        //Do big stuff: (500 lines)
    }
}

for (String orderKey: updateMap.keySet()) {
    if(orderKey.contains('#New')) {
        //Do same big stuff as above: (500 lines)
    }
}

EDIT: Adding the types of Map

Map<String,CustomObject> insertMap,updateMap = new Map<String,CustomObject>();

Now, as you can see, then inner IF condition is same, only the values of the orderKeyare coming from different maps.

The values of the orderKey will be different from each Map. They won't ever be the same.

Is there a way to combine these FOR Loops in one? Maybe like :

for((String orderKey : insertMap.keySet()) OR (String orderKey : udpateMap.keySet()))
{
    if(orderKey.contains('#New')) {
        //Do big stuff
    }
}

I know the above code is not possible, I just wanted to show what I am looking for.

The number of lines that I have masked by the //Do Big Stuff are almost 500. And hence I want to reduce the lines of code.

Thanks a lot !

3
  • 1
    move //Do big stuff: (500 lines) in a method and call it there. if there are some over lapping step move them in one method and keep different steps in different methods and call both of them in the if's Commented Dec 3, 2015 at 19:36
  • What are the types of the values here? Please update your code and your question to include 1) the type declaration of both insertMap and updateMap 2) clarify whether the two maps will contain the same keys 3) if both maps contain the same key, should Do big stuff run once or twice for the values in each of those maps? Commented Dec 3, 2015 at 19:49
  • @whaley Hi, I have added the required editing. Two maps will not contain the same keys. Commented Dec 3, 2015 at 19:56

2 Answers 2

2

In Java 8 you can use Stream.concat:

Stream.concat(insertMap.keySet().stream(), updateMap.keySet().stream())
    .filter(orderKey -> orderKey.contains("#New"))
    .forEach(orderKey -> {
        ...
    });

Google's Guava library has Iterables.concat:

for (String orderKey: Iterables.concat(insertMap.keySet(), updateMap.keySet())) {
    ...
}

And of course, the most "low tech" way would be to simply extract a method:

for (String orderKey: insertMap.keySet()) {
    if (orderKey.contains("#New")) {
        doBigStuff(orderKey);
    }
}

for (String orderKey: updateMap.keySet()) {
    if (orderKey.contains("#New")) {
        doBigStuff(orderKey);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I am actually using this code in Salesforce Apex, hence I can't use custom libraries!
Thanks. I will have to check whether Apex supports Java 8 features. If it does, then this will work.
1

If you want parallelism, first extract you doStuff(key) function to a class that implements Runnable interface.

Then define a thread pool executor with as many threads as you need. Your for loops may remain, but instead of calling your function, create an instance of you new class and submit to your executor.

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.