0

I need to create 3 datastructures.

The first is a collection of Persons (PersonRegister):

public Person {
    private final int uniquePersonId; // Unique identifier
    private long personalNumber;
    private String name;
    // additional code
}

The second is a collection of Insurances (InsuranceRegister):

public Insurance {
    private final int uniqueInsuranceId; // Unique identifier
    private int uniquePersonId; // Is used as a link between the insurance and person
    private Date date;
    private boolean active;
    // additional code
}

The third is a collection of Claims (ClaimsRegister):

public Insurance {
    private final int uniqueClaimId; // Unique identifer
    private int uniquePersonId; // Is used as a link between the claim and person
    private Date date;
    // additional code
}

Each Objects have overriden equals() and hashCode() methods.

Non of the datastructures will need any removal, as old data will be used for statistics etc.

It is also important that the datastructures contains no duplicates.

Methods that will be used on these datastructures are for example:

  • Find the insurance/claim of a specific personId.
  • Find all claims/insurances submitted at a given timeframe.
  • Find a person based on personId or personalNumber
  • Find all persons with a specific lastName
  • Find all active insurances to a specific person.
  • Find all insurances/claims of a specific subclass type (e.g. CarInsurance or HomeInsurance, both are sublcasses to Insurance)

The list goes on till you named it. Any kind of data that fits a comprehensive statistics- and search functionality.

A lot of these methods will be using an advanced for loop with an iterator.

As it stands now, the only way to link a Person to its insurances is by comparing the uniquePersonId variables to each other. Would it be better to also have each Person own a List with object references to its Insurances and Claims?

Additionally have each Insurance/Claim have an object reference to its parent/owner? Or is this considered bad practice, by the "seperation of concerns"?

It would make methods such as determining boolean totalCustomer(True if you have more than 3 active insurances) much easier to place inside the Person class. Any suggestions?

Anyhow, to the main question. What Collections would be best suited for each datastructure? (Limited to Java Collections)

Currently/temporarily I have 3 ArrayLists with a if(!list.contains(newElement)) { add(newElement) }; to prevent duplicates.

Isn't ArrayList faster than HashSet or HashMap when iterating through a for loop? But is it substantially fast enough to be of value?

I've been thinking that HashSet would be a better pracitice considering no duplicates are allowed, or is my "solution to duplicates" good enough? HashMap could make sense for Persons considering uniquePersonId will be used everytime an Insurance or Claim is searched for.

However I still want the functionality of searching for personalNumber or any other member of person for that matter. Will iterating like this Iterate through a HashMap still be effective enough, and make for good programming design?

I have spent countless hours on stackoverflow and google trying to figure this out. Any suggestions and help would be very much appreciated. Thanks.

edit: Difference between HashSet and HashMap? Was linked as possible duplicate question, however that thread only explains the difference between HashSet and HashMap, with a link to Oracles Collection tutorial. I'm aware of most of the differences. I have also read the Collection tutorial. But because of a wide usage of the structures, I'm still having some difficulties finding the most fitting Collection. My main goal is regarding what would make for the best data-structure design. Sorry if I was being unclear.

Community
  • 1
  • 1
sevenup
  • 1
  • 3
  • possible duplicate of [Difference between HashSet and HashMap?](http://stackoverflow.com/questions/2773824/difference-between-hashset-and-hashmap) – steve cook May 06 '15 at 11:31

1 Answers1

0

I suggest using a HashSet to store a list of your various objects. Also make your objects (Person, etc) implement Comparable, that way you can override the compareTo method and use it to for sorting your HashSet. When it comes to sorting you could simply make use of the Collections.sort() method, which can also take a Comparator for custom sorting.