• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

desgin problem: how to categorize items in new list?

 
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

say I have a list of Cars with the following properties:

long agencyId;
long carId;
String type;

There are 10,000 items in my arrayList named carList. and there are ~ 50 agencies (ids). I would like to create X new array list such that every list will contain only cars from the same agencyId. example, the list of carList_A will contain cars object from agencyId=14593.

Every time I tackle this type of problems, I managed to solve it but with so many 'if statements', 'exceptions scenarios' and pointers that points to the id and figures if its a new id or not.

I wonder if there's such design pattern to address this problem or just a simple way to do that.

thanks for any thoughts
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How will you use those lists, then?

If you need to access them by id, I'd consider using a MultiValueMap: http://jakarta.apache.org/commons/collections/apidocs/org/apache/commons/collections/map/MultiValueMap.html
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
YES, this does it.
thank you Ilja
 
reply
    Bookmark Topic Watch Topic
  • New Topic