Reputation: 55
I wanted to optimize the below code. Will streams optimize the below nested foreach loops? If so am new to streams can someone please help me? In the below I have replaced the names for project confidentiality purpose. Shall be using the tList for further processing in the code. Can some one please help on this?
List<Tea> tea = requestBody.getTea();
for (Tea tea1 : teas) {
List<String> teaValues = tea1.getTeaValues();
for (String t : teaValues) {
if ((t).contains("tMapping") || ((t).contains("tdata"))) {
int subStrng = t.indexOf(".") + 1;
int subStrngSpace = t.indexOf(" ");
String tStrng = t.substring(subStrng, subStrngSpace);
tList.add(tStrng);
} else {
String[] tStrng = t.split("\'");
String t1 = tStrng[1];
tList.add(t1);
}
}
}
Upvotes: 1
Views: 427
Reputation: 2817
Will streams optimize the below nested foreach loops?
No, not really. Unless you have a really big input and you're using a server with multiple processes/cores, then you can get a speed up from using parallel streams.
Otherwise, these streams will just be converted into loops "under the hood" ...
This is to answer your question.
Now let's look at the question behind the question ... why do you want to optimise it ? How will you know that your optimisation level is good enough ?
EDIT:
Upvotes: 3