我正在尝试将迭代 ArrayList 的普通 for 循环转换为并行循环。我遇到了parallelStream.forEach() 和parallelStream().collect(Collectors.toList()) 的概念。但不清楚如何转换。下面是普通 for 循环的示例代码:
public List<FinPlan> getFinPlanList() {
List<FinPlan> newList = new ArrayList<>();
// get list of string
List<String> finPlanIdsList = finPlanSearchRequest.getFinPlanIds();
// iterate list and add element to new list
for(String planId: finPlanIdsList) {
newList.add(retrieveFinPlan(planId));
}
return newList;
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下代码,但它给了我错误“newList 应该是最终的”。
finPlanIdsList.parallelStream().forEach( planId -> {
newList.add(retrieveFinPlan(planId));
});
Run Code Online (Sandbox Code Playgroud)
那么,主要问题是如何将上面的普通循环转换为parallelStream.forEach()和parallelStream().collect()?
我正在尝试将 java 转换String date为java.sql.Timestamp. 我可以通过使用SimpleDateFormatwith String datevalue as来转换它"2021-01-07 02:02:16.172",但是当尝试使用 value as "2021-08-04T00:00:00.000"with seperator时'T',它会给我错误。下面是java代码:
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest {
public static void main(String[] args) throws ParseException {
//String date = "2021-08-04T00:00:00.000Z";// How to convert this?
String date = "2021-01-07 02:02:16.172";// conversion successful
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
Date parsedDate = dateFormat.parse(date);
Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
System.out.println(timestamp);
}
}
Run Code Online (Sandbox Code Playgroud)