matri1395
matri1395

Reputation: 1

Check if in the List occurs object with boolean = true

I have the following List:

ArrayList<ConsumerWarehouse> consumerWarehouse = new ArrayList<ConsumerWarehouse>();

The ConsumerWarehouse constructor:

public ConsumerWarehouse(double length, double width, double height)
    {
        this.length = length;
        this.width = width;
        this.height = height;
        calculateRoomSpace();
        SPACE_ID = countSpace.incrementAndGet();
        this.booked = false;
        consumerWarehouse.add(this);
    }

Created object and called method:

ConsumerWarehouse warehouseNo1 = new ConsumerWarehouse(10,10,3);
warehouseNo1.setBooked(true);

The setBooked method sets the 'booked' variable to true. I would like to write a method that would show me all objects that are in consumerWarehouse list and have the value 'booked' set to false, an example of method name:

public void displayFreeConsumerWarehouse()
    {
        for(int i = 0; i < consumerWarehouse.size(); i++)
        {
            if() // TODO
        }
    }

I'm not sure what condition I should set here, could you please suggest some?

Upvotes: 0

Views: 721

Answers (2)

Most Noble Rabbit
Most Noble Rabbit

Reputation: 2776

Java stream solution:

public void displayFreeConsumerWarehouse(){
    consumerWarehouse.stream()
            .filter(warehouse -> !warehouse.isBooked())
            .forEach(warehouse -> System.out.println(warehouse));
}

Upvotes: 0

Eduardo Briguenti Vieira
Eduardo Briguenti Vieira

Reputation: 4599

You need to check if you have a isBooked() or getBooked() method exposing that property.
If you have a isBooked() method, it would be:

public void displayFreeConsumerWarehouse()
{
    for(int i = 0; i < consumerWarehouse.size(); i++)
    {
        if(!consumerWarehouse.get(i).isBooked())
    }
}

Upvotes: 1

Related Questions