Store values of multiple keys while collecting into a map stream | بلاگ

Store values of multiple keys while collecting into a map stream

تعرفه تبلیغات در سایت

آخرین مطالب

امکانات وب

Vote count: 0

Map x = ArrayListMultimap.create();
Map results1 = Maps.newHashMap();
Map results2 = Maps.newHashMap();

I have a multimap which I need to traverse and make some expensive calls. To save time, I want to do a parallel stream. The results I get can have null values which has to be stored in a map. I know how to do this in a non-parallel way, however I'm not able to do this in a parallel stream without getting into concurrency issues. I realize I need to somehow convert this into a map and collect the results, but I don't know how I can return multiple keys and values. I was thinking of having a temporary single map, but that too will have multiple keys.

x.keySet().paralleStream().forEach(req -> {
   try {
      Response response = getResponseForRequest(req);
      if(response.getTitle() != null) {
        x.get(req).forEach(y -> results1.put(y, response.getTitle());
      if(response.getDetails() != null) {
        x.get(req).forEach(y -> results2.put(y, response.getDetails()));
      value.put(req, response.getTitle());
   } catch(TimeoutException e) {
      x.get(req).forEach(y -> {
        results1.put(y, null);
        results2.put(y, null);
   } catch(Exception e) {
    //log & do nothing
asked 52 secs ago

نویسنده : استخدام کار بازدید : 6 تاريخ : چهارشنبه 22 فروردين 1397 ساعت: 5:29