Retrieving an object just from a specific and unique instance variable - Java -
i have class (cars) has int id instance variable.
the id not have unique - more of category id.
if id exists add object array list within category if id not exist create new category id
before create instance of class want check there isn't instance variable id.
the way can think of doing right having static arraylist of cars within class cars. search through array list through each car , compare ids see if there id matches. if not - create the new car instance new id.
surely there better way of doing this?
using map, suggested lutz horn, better way of holding ids, , hashmap in runtime complexity of o(1), rather searching elements of container such list.
if cars might concurrently created using several threads, should, of course, make sure not enter race condition.
Comments
Post a Comment