I currently have a three dimensional list in my Java project. I am using it to store the following. I have a list of Pairs. A Pair has a gender (male, female or mixed) and a food preference (meat, veggie or vegan).
In my code I have to access sometimes only the pairs which have veggie as food preference and are male or other combinations. I first thought that a three dimensional list would be the best to do this. My 3D list looks like the following:
ArrayList<ArrayList<ArrayList<Pair>>> pairsSplitUp;
The list is filled with values such that if we access it we get the following:
- Index 0: Index 0: all Pairs that are male and have meat as food Preference
- Index 0: Index 1: all Pairs that are female and have meat as food Preference
- Index 0: Index 2: all Pairs that are mixed and have meat as food Preference
- Index 1: Index 0: all Pairs that are male and have veggie as food Preference
- ...
- Index 2: Index 0: all Pairs that are male and have vegan as food Preference
- ...
I first thought that this might be the best way to store this data, but then I read this question, where it is stated that if you are implementing a 3D list you are probably not handling your data right. In the mentioned question there is also an answer where a Map is used to store the data, but I am not really understanding this abstraction and I don't know how it could be used for my case. Because of this I wanted to ask, if there is a better, cleaner way for storing my data such that I can still access only the Pairs which are male and vegan etc.
In the code there exists a ArrayList<Pair> pairList
which holds all the Pairs. The Pair class looks like the following:
public class Pair {
private Person person1;
private Person person2;
private String foodPreference;
private String gender;
public Pair(Person person1, Person person2) {
this.person1 = person1;
this.person2 = person2;
this.foodPreference = decideFoodPreference();
this.gender = decideGender();
}
/*
contains Getter for the fields and methods to decide and set the foodPreference
and the gender.
*/
}
The Person class contains information about a specific Person like the name, age, gender, location and so on.
Is there a better, cleaner way to do the storing of the Pairs, where I can still get only the Pairs which have foodPreference x and gender y or is my way with the three dimensional list the best way for my specific data?