AlexAlba
AlexAlba

Reputation: 31

[java][Calendar] - Filter my records from a Arraylist<Bean> Weekely

i'm new in java programming, and i need help to filter my records from an array list weekly(first day of every week [Monday]) in the current year.

i have showed all my records from an ArrayList in a simple table result:

+----+-------+-----------+-------+-------------------+
| ID | Name | LastName   | Email | Registration Date |
+----+-------+-----------+-------+-------------------+
| 1  | Name1 | LastName1 | Email | 01-01-2017        |
+----+-------+-----------+-------+-------------------+
| 2  | Name2 | LastName2 | Email | 05-02-2017        |
+----+-------+-----------+-------+-------------------+
| 3  | Name3 | LastName3 | Email | 15-02-2017        |
+----+-------+-----------+-------+-------------------+
| 4  | Name4 | LastName4 | Email | 18-03-2017        |
+----+-------+-----------+-------+-------------------+
| 5  | Name5 | LastName5 | Email | 22-04-2017        |
+----+-------+-----------+-------+-------------------+
| 6  | Name6 | LastName6 | Email | 15-05-2017        |
+----+-------+-----------+-------+-------------------+
| 7  | Name7 | LastName7 | Email | 26-06-2017        |
+----+-------+-----------+-------+-------------------+
| 8  | Name8 | LastName8 | Email | 26-06-2017        |
+----+-------+-----------+-------+-------------------+

this the result i need to show using Registration date like a filter to filter all my records weekly:

+--------------------+
|Week from 02-01-2017|
+----+-------+-------+---+-------+-------------------+
| ID | Name | LastName   | Email | Registration Date |
+----+-------+-----------+-------+-------------------+
| 1  | Name1 | LastName1 | Email | 02-01-2017        |
+----+-------+-----------+-------+-------------------+
| 2  | Name2 | LastName2 | Email | 05-01-2017        |
+----+-------+-----------+-------+-------------------+

+--------------------+
|Week from 13-02-2017|
+----+-------+-------+---+-------+-------------------+
| ID | Name | LastName   | Email | Registration Date |
+----+-------+-----------+-------+-------------------+
| 3  | Name3 | LastName3 | Email | 15-02-2017        |
+----+-------+-----------+-------+-------------------+

+--------------------+
|Week from 13-03-2017|
+----+-------+-------+---+-------+-------------------+
| 4  | Name4 | LastName4 | Email | 18-03-2017        |
+----+-------+-----------+-------+-------------------+

+--------------------+
|Week from 17-03-2017|
+----+-------+-------+---+-------+-------------------+
| 5  | Name5 | LastName5 | Email | 22-04-2017        |
+----+-------+-----------+-------+-------------------+

+--------------------+
|Week from 15-05-2017|
+----+-------+-------+---+-------+-------------------+
| 6  | Name6 | LastName6 | Email | 15-05-2017        |
+----+-------+-----------+-------+-------------------+
| 7  | Name6 | LastName6 | Email | 19-05-2017        |
+----+-------+-----------+-------+-------------------+

+--------------------+
|Week from 26-06-2017|
+----+-------+-------+---+-------+-------------------+
| 8  | Name7 | LastName7 | Email | 26-06-2017        |
+----+-------+-----------+-------+-------------------+
| 9  | Name8 | LastName8 | Email | 29-06-2017        |
+----+-------+-----------+-------+-------------------+

The date in "week from dd-MM-yyyy" mean all Mondays in the current year when my program is executed can be executed in 2026 and filter the records weekly.

this is the code i developed:

//from an Arraylist instantiated:
List<CertificatMajCrBean> lines = new ArrayList<CertificatMajCrBean>();

with the elements i want to add:

lines.add(records);

//i show the result like that:

System.out.println("+---------+----------------+---------------------+------------------+-------------------+");
System.out.println("| ID      | Name         | LastName        | Email            | Registration Date        |");
System.out.println("+---------+----------------+---------------------+------------------+-------------------+");
for (CertificatMajCrBean line: lignes){

System.out.println("| "+numRec+"  | "+line.getName()+"  | "+line.getLastName()+"   | "+line.getEmail() +"  | "+line.getRegistrationDate()+" |");
System.out.println("+---------+----------------+---------------------+------------------+---------------------------+");
numRec  ++;

}

into this iteration i want to add my filter.

Any help to make this algorithm real, many thanks !

Upvotes: 1

Views: 87

Answers (1)

Anonymous
Anonymous

Reputation: 86359

I suggest that in the name of good style and of breaking a problem into smaller pieces first we separate the computation of your result from the printing of it. To that end we need a data structure that can hold a number of lists of beans, one list per week. I suggest:

    Map<LocalDate, List<CertificatMajCrBean>> byWeek = new TreeMap<>();

Most often one would use a HashMap rather than a TreeMap. You may, but the advantage of a TreeMap is that it will keep the map sorted by date, which will help us when we want to print the weekly lists in chronological order.

For the computation, iterate through your ArrayList the way you are probably already doing. For each CertificatMajCrBean from the list, get the registration date. I am assuming this is a LocalDate as I suggested in a comment. Find the Monday where the week begins from bean.getRegistrationDate().with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)). I know it looks complicated, but it’s lovely terse. As the method name suggests, it finds the previous Monday of the registration date or the same date if it is already a Monday. Next, check if this Monday is already a key in the map. It’s easiest to use byWeek.get(mondayOfRegWeek). This will return null the first time because there isn’t already a map entry. In this case, create a new ArrayList, store your bean into it and put in into the map: byWeek.put(mondayOfRegWeek, weeklyBeans); where weeklyBeans is your newly created list. If byWeek.get(mondayOfRegWeek) returned a list, just add the bean to it and you’re done. After you’ve processed all your beans from your list, your map is finished.

For printing the result use a for loop over the entry set of the map:

    for (Map.Entry<LocalDate, List<CertificatMajCrBean>> entry : byWeek.entrySet()) {
        // print beans from one week here
    }

Inside the for loop use entry.getKey() to get the Monday where the week begins. If you just print the date, it will come out as 2017-06-26. To format it in some other way, you may use a DateTimeFormatter. To get the list of beans for the week, use entry.getValue(). Again you may use a loop inside the loop to print each bean.

If some of it calls for clarification, you may want to check the API documentation first, or else revert in a comment here.

If you had been advanced, I would have told you to use a Stream and Collectors.groupingBy() for the computation. If you are new to programming you probably don’t want to try that just yet.

Upvotes: 1

Related Questions