0

Best way to describe this is explain the situation.

Imagine I have a factory that produces chairs. Now the factory is split into 5 sections. A chair can be made fully in one area or over a number of areas. The makers of the chairs add attributes of the chair to a chair object. At the end of the day these objects are collected by my imaginary program and added into X datatype(ArrayList etc).

When a chair is added it must check if the chair already exists and if so not replace the existing chair but append this chairs attributes to it(Dont worry about this part, Ive got this covered)

So basically I want a structure than I can easily check if an object exists if not just straight up insert it, else perform the append. So I need to find the chair matching a certain unique ID. Kind of like a set. Except its not matching the same object, if a chair is made in three areas it will be three distinct objects - in real life they all reperesent the same object though - yet I only want one object that will hold the entire attribute contents of all the chairs.

Once its collected and performed the update on all areas of the factory it needs iterate over each object and add its contents to a DB. Again dont worrk about adding to the DB etc thats covered.

I just want to know what the best data structure in Java would be to match this spec.

Thank you in advance.

chepseskaf
  • 664
  • 2
  • 12
  • 41
Julio
  • 1,815
  • 7
  • 20
  • 22
  • How do you determine whether two chairs are the same chair, if they have different attributes? – StriplingWarrior May 03 '11 at 15:24
  • It'd be easier to imagine if you posted some code. – Brian Roach May 03 '11 at 15:25
  • @StriplingWarrior needs to be matched on the unique id of the chair object in the datatype. The attributes are stored as distinct objects in an arraylist in each chair object. The attribute objects are simply copied into the existing chairs attribute arraylist if a match is made on the unique id, else no match it adds the whole object to the datatype. – Julio May 03 '11 at 15:29

3 Answers3

1

I'd say use ArrayList. Override the hashcode/equals() method on your Chair object to use the unique ID. That way you can just use list.contains(chair) to check if it exists.

Kal
  • 24,724
  • 7
  • 65
  • 65
  • 3
    If you already re-defined hashcode and euqals why not use a HashSet instead of a List. I don't see the necessity of preserving the list order. – Robert May 03 '11 at 15:39
1

I'd say a HashMap: it lets you quickly check whether an object exists with a given unique ID, and retrieve that object if it does exist in the collection. Then it's simply a matter of performing your merge function to add attributes to the object that is already in the collection.

Unlike most other collections (ArrayList, e.g.), HashMaps are actually optimized for looking something up by a unique ID, and it will be just as fast at doing this regardless of how many objects you have in your collection.

This answer originally made reference to the Hashtable class, but after further research (and some good comments), I discovered that you're always better off using a HashMap. If you need synchronization, you can call Collections.synchronizedMap() on it. See here for more information.

Community
  • 1
  • 1
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • cheers, from what i gathered prior to writing this, hashtables are optimized vs arraylist.. nice to see some confirmation. – Julio May 03 '11 at 15:38
  • thanks for the thread heads up, for this project thread safety shouldnt be an issue – Julio May 03 '11 at 15:45
  • 1
    Just to clarify: A hash table is a general CS term, a `Hashtable` is a specific Java class that you should not use!!! It's ancient and badly designed, and `HashMap` is it's much more efficient successor – Sean Patrick Floyd May 03 '11 at 15:46
  • @Sean Patrick Floyd: Thanks for the comment. I updated the answer. – StriplingWarrior May 03 '11 at 16:08
0

I'd say use an EnumMap. Define an enum of all possible part categories, so you can query the EnumMap for which part is missing

public enum Category {
    SEAT,REST,LEGS,CUSHION
}
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • this is a sensible option however the way the system works over many factories that implement a similar routine, the parts names and variations change drastically. i also ahd to water down the whole concept because its not only chairs but potentially any type of furniture. – Julio May 03 '11 at 15:43