0

Have a Map as HashMap<ItemNo, List<Price>> in my application. Each itemNo having many price intervals. Each price interval is having FROM_DTIME, TO_DTIME. Need to find the all price objects whose are having overlapping interval by writing java logic. Example overlapping interval is as below, 2nd interval should start after completion of 1st interval i.e 01-SEP-19. but it started at 08-AUG-19.

i tried as below ,

    for (int i = 0; i < vGroupedByItemNoMap.get(itemNo).size(); i++) {
              Price vCurrent = vGroupedByItemNoMap.get(itemNo).get(i);
              Price vNext = (i == 0 ? null : vGroupedByItemNoMap.get(itemNo).get(i + 1));
DateTime vCurrentTodate = vCurrent.getToDate().plusSeconds(1);
          if (vNext != null) {
            DateTime vNextFromdate = vNext.getFromDate();
            if (vCurrentTodate != vNextFromdate) {
                  vOverlappedItemNoList.addAll(vGroupedByItemNoMap.get(itemNo));

                  log.info(
                      "Prices having Overlapping and [ItemNos ={}] are  not processed.",vOverlappedItemNoList);
                  break;
                }
              }
            }

enter image description here

Can any one help me here how find such overlapping intervals.

Gangs165700
  • 111
  • 8
  • Can you describe what you have tried and show your code? – andrewJames Apr 02 '20 at 16:53
  • I tried as below code, – Gangs165700 Apr 02 '20 at 16:55
  • Using indexed for loop getting the current iteration and next iteration price objects.compared them as if (vCurrent.getFromDate().plusSeconds(1) != vNext.getToDate()) then these 2 prices having overlapping. – Gangs165700 Apr 02 '20 at 17:01
  • A couple of pointers which may help you to improve your solution: [How to iterate over a map](https://stackoverflow.com/questions/46898/how-do-i-efficiently-iterate-over-each-entry-in-a-java-map); [How to compare two dates](https://stackoverflow.com/questions/2592501/how-to-compare-dates-in-java). – andrewJames Apr 02 '20 at 17:14

0 Answers0