user1896796
user1896796

Reputation: 749

Improving code design to enhance performance

I am trying to do something like below. I don't like the design of this as I am using 4 for loops to achieve this. Can I further enhance the design to achieve this?

  1. Creating a map with dates as keys.

  2. Sort the list values inside the map on dates(dates have hours and minutes here)

  3. Giving an incremental id to each dto.

     int serialNumber = 1;
     if (hList != null && !hList.isEmpty()) {
         // create a Map with dates as keys
         HashMap<String, ArrayList<BookDTO>> mapObj = new HashMap<>();
         for (int count = 0; count < hList.size(); count++) {
             BookDTO bookDTO = (BookDTO) hList.get(count);
    
             ArrayList<BookDTO> list = new ArrayList<>();
             list.add(bookDTO);
    
             Calendar depDate = bookDTO.getDepartureDate();
             SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
             if (depDate != null) {
                 String formattedDate = format.format(depDate.getTime());
                 if (mapObj.containsKey(formattedDate)) {
                     mapObj.get(formattedDate).add(bookDTO);
                 } else {
                     mapObj.put(formattedDate, list);
                 }
             }
    
         }
    
    
     // Sort the values inside the map based on dates
     for (Entry<String, ArrayList<BookingDTO>> entry : mapObj.entrySet()) {
         Collections.sort(entry.getValue(), new BookDTOComparator(DATES));
     }
    
     for (Entry<String, ArrayList<BookDTO>> entry : mapObj.entrySet()) {
         serialNumber = setItinerarySerialNumber(entry.getValue(), serialNumber);
     }
    

Upvotes: 0

Views: 70

Answers (1)

Timur Efimov
Timur Efimov

Reputation: 358

  1. I believe you can merge two last loops. So, we will see only two loops. (for now I can see three loops)
  2. You can also try Arrays.parallelSort(entry.getValue()) if lists are too large and it is applicable

Also, if it applicable, see code below:

int serialNumber = 1;
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
ArrayList<BookDTO> hListCopy = new ArrayList<>(hList);
Collections.sort(hListCopy, new NewBookDTOComparator());               // 2. sorting
HashMap<String, ArrayList<BookDTO>> mapObj = new HashMap<>();
for (BookDTO bookDTO : hListCopy) {
    serialNumber = setItinerarySerialNumber(bookDTO, serialNumber);    // 3. serialNumber

    Calendar depDate = bookDTO.getDepartureDate();
    if (depDate != null) {
        String formattedDate = format.format(depDate.getTime());
        if (mapObj.containsKey(formattedDate)) {
            mapObj.get(formattedDate).add(bookDTO);
        } else {
            ArrayList<BookDTO> list = new ArrayList<>();
            list.add(bookDTO);
            mapObj.put(formattedDate, list);
        }
    }
}

So, only one loop (and one sorting algorithm). For list cope-constructor used System.arraycopy internally, you can google performance of.

  1. You can sort hList instead without creating new 'hListCopy' if applicable.
  2. Beware of NewBookDTOComparator, you should sort not only by minutes and hours, but also by 'DepartureDate'
  3. I think SimpleDateFormat format should be static field or class field.
  4. You can also try Arrays.parallelSort(hListCopy) if lists are too large and it is applicable

Upvotes: 1

Related Questions