0

Is there any solution available to reduce the number of lines of code for the following?

TreeMap<Integer,List<SomeObject>> myMap = new TreeMap<Integer,List<SomeObject>>();

List<SomeObject> list1 = new ArrayList<SomeObject>();
list.add(someObjectInstance1);
list.add(someObjectInstance2);

List<SomeObject> list2 = new ArrayList<SomeObject>();
list.add(someObjectInstance1);
list.add(someObjectInstance2);

List<SomeObject> list3 = new ArrayList<SomeObject>();
list.add(someObjectInstance1);
list.add(someObjectInstance2);

List<SomeObject> list4 = new ArrayList<SomeObject>();
list.add(someObjectInstance1);
list.add(someObjectInstance2);

myMap.put(1, list1);
myMap.put(2, list2);
myMap.put(3, list3);
myMap.put(4, list3);

Assume that SomeObject represents a custom Date type and List<SomeObject> currentWeek will represent a list of dates. In this list I will store only starting and ending date of a week. I have to store previous weeks (by subtracting 7 days from the current Sunday and current Saturday) in a map.

The above code will looks good if the loop size is small. What if my loop size is more than 20? Then I tried the following solution.

    private Map<Integer, List<SomeObject>> getPreviousWeeks(List<SomeObject> currentWeek) {
    TreeMap<Integer,List<SomeObject>> treeMap = new TreeMap<Integer,List<SomeObject>>();
            for(int n=0; n<20; n++) {
                List<SomeObject> nthWeek = currentWeek;
                SomeObject nthWeekStartDate = nthWeek.get(0);
                SomeObject nthWeekEndDate = nthWeek.get(1);
                nthWeek.clear();
                nthWeek.add(nthWeekStartDate);
                nthWeek.add(nthWeekEndDate);
                treeMap.put(n,nthWeek)
                nthWeek.clear();
                nthWeekStartDate = nthWeekStartDate.subtractDays(7);
                nthWeekEndDate = nthWeekEndDate.subtractDays(7);
                nthWeek.add(nthWeekStartDate);
                nthWeek.add(nthWeekEndDate);
                currentWeek = nthWeek;
             }
             return treeMap;
        }

The problem here is, it is returning 20 days of same day. When I was debugging it is working fine only for the first iteration.

Can any one help me in this?

Thanks.

2 Answers2

0

Try this for the code you mentioned first. After declaring your lists :

int index = 1;
List<SomeObject>[] lists = new List[]{list1; list 2; list 3; list4}
TreeMap<Integer,List<SomeObject>> myMap = new TreeMap<Integer,List<SomeObject>>();
for (List<SomeObject> list : lists) {
    list.add(someObjectInstance1);
    list.add(someObjectInstance2);
    myMap.put(index, list);
    index++;
}
Gabriel
  • 464
  • 4
  • 17
0

After careful observation to my code, I identified my mistake. Then, I tried the following and it is working as expected. Simply we have to replace the value of n in for-loop to get list of weeks in a map.

public class WeekTest {

    public static void main(String args[]) {
        LocalDate start = LocalDate.of(2020, Month.MARCH, 15);
        LocalDate end = LocalDate.of(2020, Month.MARCH, 21);
        List<LocalDate> dates = new ArrayList<>();
        dates.add(start);
        dates.add(end);
        Map<Integer, List<LocalDate>> map = getPreviousWeeks(dates);
        System.out.print(map);
    }

    private static Map<Integer, List<LocalDate>> getPreviousWeeks(List<LocalDate> currentWeek) {
        TreeMap<Integer, List<LocalDate>> treeMap = new TreeMap<Integer, List<LocalDate>>();
        int i=0;
        for (int n = 0; n < 4; n++) {
            treeMap.put(n,Arrays.asList(currentWeek.get(i+n),currentWeek.get(i+n+1)));
            LocalDate nthWeekStartDate = currentWeek.get(i+n).minusDays(7);
            LocalDate nthWeekEndDate = nthWeekStartDate.plusDays(6);
            currentWeek.add(nthWeekStartDate);
            currentWeek.add(nthWeekEndDate);
            i++;
        }
        currentWeek.clear();
        return treeMap;
    }
}

and the out put is,

{0=[2020-03-15, 2020-03-21], 1=[2020-03-08, 2020-03-14], 2=[2020-03-01, 2020-03-07], 3=[2020-02-23, 2020-02-29]}