0

I have a class (cars) that has an int ID instance variable.

The ID does not necessarily have to be unique - it is more of a category ID.

If the ID exists I add the object to an array list within that category if the ID does not exist I create a new category with that ID

Before I create an instance of this class I want to check that there isn't already an instance variable with that ID.

The only way I can think of doing this right now is having a static arraylist of all the cars within the class cars. I then search through the array list through each car and compare IDs to see if there is an ID that already matches. if not - I create the the new car instance with the new ID.

Surely there is a better way of doing this?

Silk13
  • 47
  • 2
  • 9
  • You could use a `Map` that keeps track of existing Car IDs. –  Nov 17 '16 at 13:33
  • Possible duplicate of [Elegant way to assign object id in Java](http://stackoverflow.com/questions/4009570/elegant-way-to-assign-object-id-in-java) – Julien Lopez Nov 17 '16 at 13:33
  • `Set`, especially `HashSet` would work. It may not belong in the `Car` class though. – bradimus Nov 17 '16 at 13:34
  • @JulienLopez - It doesn't necessarily have to be a unique ID, IF the ID is not unique I add it to another arraylist. – Silk13 Nov 17 '16 at 13:36
  • problem with a static variable is that its only unique per classloader and not per jvm – salyh Nov 17 '16 at 14:47

3 Answers3

0

Using Map, as suggested by Lutz Horn, is a better way of holding all the IDs, and HashMap will do that usually in runtime complexity of O(1), rather than searching all the elements of a container such as a list.

If cars might be concurrently created using several threads, you should, of course, make sure that you do not enter a race condition.

Mike
  • 1,215
  • 7
  • 12
0

In the first place if you want your id to be unique, your class design should at least have some form of mechanism to prevent duplicates and not by checking all existing id every time you create a new object.

Below shows one example you may consider:

public class Car{

    private static int counter = 1;
    private int id;

    public Car(){
        id = counter ++;    //auto-generate a new id when car is created
    }
}

Example:

Car c1 = new Car();    //id is 1
Car c2 = new Car();    //id is 2
Car c3 = new Car();    //id is 3
user3437460
  • 17,253
  • 15
  • 58
  • 106
  • Just updated the question to say it does not have to be a unique ID - Sorry for the confusion – Silk13 Nov 17 '16 at 15:00
  • @Silk13 Your requirement is kinda odd, you may want to tell us why do you need to add different category into different list. This seems like a [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) to me. Further more, how many possible different categories will there be? Thousands? – user3437460 Nov 17 '16 at 15:28
0

Using a Map would be a good option for your problem. But It will not make a good sens semantically speaking to have that map inside your Car class. Maybe more likely in a CarFactory or a CarManager class

Let assume that you are using a CarFactory for that:

public class Car {
    public Car(String ID) { 
       // construct the instance 
    }
   // car's behaviour here
}

public class CarFactory {
   private Map<String, Car> existingCars;
   private CarFactory() {
      existingCars = new HashMap<>();
   }
   private static CarFactory factory;
   public static CarFactory getFactory() {
      if (factory == null) {
         factory = new CarFactory();
      }
      return factory;
   }

   // Now our method
   public Car getBrandNewCar(String ID) {
      if (existingCars.containsKey(ID) {
         throw new IllegalArgumentException("The car already exists...");
      } else {
         Car car = new Car(ID);
         existingCars.put(ID, car);
         return car;
      }   
   }      
}

As an alternative to this approach you can use an approach that generates unique ID everytime it is invoked so you don't need to check it before using it. For example via the link shared by @Julien Lopez in comments

alainlompo
  • 4,414
  • 4
  • 32
  • 41