0

I have a class of which objects I want to get instantiated at runtime and stored in a collection of such objects. However, the number of such objects can be dynamic in nature.

Let say,

class Car{
    private Color color;
    private Brand brand;
    //AllArgsConstructor
}

enum Color{
   RED, BLUE
}

enum Brand{
   VW, SUZUKI, HONDA
}

Now I want objects with all UNIQUE possible permutations from 2 enums(in this case 2*3 = 6) to be created at runtime and added to a collection.

If I were to do this manually, I would do:

Car carRV = new Car(Color.RED, Brand.VW);
Car carRS = new Car(Color.RED, Brand.SUZUKI);
Car carRH = new Car(Color.RED, Brand.HONDA);
Car carBV = new Car(Color.BLUE, Brand.VW);
Car carBS = new Car(Color.BLUE, Brand.SUZUKI);
Car carBH = new Car(Color.BLUE, Brand.HONDA);

//addToCollection

But the problem with the above is too much coding and very manual to keep on adding as a new enum value is added. Is there a better way to do this?

Thanks in advance :)

0

4 Answers 4

1

Yop can use a nested for loop to create car objects

private Car createCar() {
    List<Car> cars = new ArrayList<>();
    for(Color color : Color.values()) {
        for(Brand brand : Brand.values()) {
            cars.add(new Car(color, brand));
        }
    }
    return cars;
}
Sign up to request clarification or add additional context in comments.

Comments

1

I personally think, that generating objects this way, is not the best thing to do, as it does not exhibit dynamic nature: your code will be dependent on a few other factors and breaking those factors will break your code.

However, if you're sure, that this is what you want, you might consider something like:

List<Car> cars = new ArrayList<>();

for (int i = 0; i < Color.values().length; i++) {
    for (int j = 0; j < Brand.values().length; j++) {
        cars.add(new Car(Color.values()[i], Brand.values()[j]));
    }
}

//or, even simpler

for (Color color : Color.values()) {
    for (Brand brand : Brand.values()) {
        cars.add(new Car(color, brand));
    }
}

and then,

for (Car car : cars) {
    System.out.println(car);
}

would print:

Car{color=RED, brand=HONDA}
Car{color=RED, brand=SUZUKI}
Car{color=RED, brand=VW}
Car{color=BLUE, brand=HONDA}
Car{color=BLUE, brand=SUZUKI}
Car{color=BLUE, brand=VW}

assuming that .toString() method is overriden.

Comments

0

I am finally doing it like this:

EnumSet.allOf(Color.class).forEach(color -> EnumSet.allOf(Brand.class).forEach(brand -> carList.add(new Car(color, brand))));

2 Comments

Prefer using normal for-loop in this case. Readability is dead in this code.. streams are not always better than ordinary loops. :)
He doesn't use stream.
0

Alternative design:

Demo class:

public class Demo {

    public static void main(String[] args) {
        List<Car> allCarCombinations = CarProducer.newInstance().getAllCarCombinations();
        System.out.println("All car combinations: ");
        allCarCombinations.forEach(System.out::println);
        System.out.println("Total number of cars: " + allCarCombinations.size());
    }

}

Car class:

public class Car {

    private final Color color;
    private final Brand brand;

    public Car(Color color, Brand brand) {
        this.color = color;
        this.brand = brand;
    }

    public Color getColor() {
        return color;
    }

    public Brand getBrand() {
        return brand;
    }

    @Override
    public String toString() {
        return "Car{" +
                "color='" + color + '\'' +
                ", brand='" + brand + '\'' +
                '}';
    }
}

Color enum:

public enum Color {
    RED, BLUE;
}

Brand enum:

public enum Brand {
    VW, SUZUKI, HONDA;
}

CarProducer class:

public final class CarProducer {

    private List<Car> allCarCombinations;

    private CarProducer() {
        allCarCombinations = new ArrayList<>();
    }

    public static CarProducer newInstance() {
        return new CarProducer();
    }

    public List<Car> getAllCarCombinations() {
        if(allCarCombinations.isEmpty()) {
            addAllCars();
        }
       return allCarCombinations;
    }

    private void addAllCars() {
        for (Brand brand : Brand.values()) {
            for (Color color : Color.values()) {
                allCarCombinations.add(new Car(color, brand));
            }
        }
    }

}

Output:

All car combinations: 
Car{color='RED', brand='VW'}
Car{color='BLUE', brand='VW'}
Car{color='RED', brand='SUZUKI'}
Car{color='BLUE', brand='SUZUKI'}
Car{color='RED', brand='HONDA'}
Car{color='BLUE', brand='HONDA'}
Total number of cars: 6

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.