List containing another list

I have an object in my Customer application that has a list of customers.

public class CustomerList { private List<Customer> } 

In the class of customers, in turn, there is a list of all the goods that they bought in the store on a specific day of the week.

  public class Customer { private List<String> itemsOnMonday; private List<String> itemsOnTuesday; private List<String> itemsOnWednesday; private List<String> itemsOnThursday; private List<String> itemsOnFriday; } 

Now I want to get a list of all the items that the customer bought in a week. What is the best way to do this? My colleague suggests creating another list and adding items to this list. I am not sure if this is a good approach. I have more than 1000 clients, and each client sells more than 500 items per week. He offers something like this -

 for(Customer customer:customerList) { List<String> items = new ArrayList<String>(); items.addAll(itemsOnMonday); //So on until Friday. } 

This is crazy because I will create more than 1000 objects inside the for loop. Any thoughts on a better way to do this? For some time we stormed the brain and cannot come up with an effective implementation for this. Any help would be greatly appreciated.

+4
source share
2 answers

How about using guava Multimap and enum for Customer elements:

 public static enum Weekday { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY } public class Customer { private Multimap<WeekDay, String> items = ArrayListMultimap.create(); // getters, setters etc } 

then

 // monday items: customer.getItems().get(Weekday.MONDAY); // week items: customer.getItems().values(); 
+5
source

Present the days of the week as an enumeration and use the EnumMap class to store lists of purchased items:

 EnumMap<Weeekday, List<String>> boughtItems; 
0
source

Source: https://habr.com/ru/post/1493791/


All Articles