@Override
    public void returnAllStates(Map states) {
        for (Iterator<Map.Entry<String, Map<String, State>>> statesIterator =
             ((Set<Map.Entry<String, Map<String, State>>>) states.entrySet()).iterator(); statesIterator.hasNext(); ) {
            Map.Entry<String, Map<String, State>> statesEntry = statesIterator.next();
            for (Iterator<Map.Entry<String, State>> stateIterator = statesEntry.getValue().entrySet().iterator();
                 stateIterator.hasNext(); ) {
                Map.Entry<String, State> stateEntry = stateIterator.next();
                State state = stateEntry.getValue();
                if (state.activeUseCount == 0) {
                    try {
                        if (state.canDestroy()) {
                            stateIterator.remove();
                        }
                    } catch (Throwable t) {
                        log.error("Dropping partition state for partition key '" + statesEntry.getKey() +
                                "' and the group by key '" + stateEntry.getKey() + "', due to error! " +
                                t.getMessage(), t);
                        stateIterator.remove();
                    }
                } else if (state.activeUseCount < 0) {
                    throw new SiddhiAppRuntimeException("State active count has reached less then zero for " +
                            "partition key '" + statesEntry.getKey() + "' and the group by key '" +
                            stateEntry.getKey() + "', current value is " +
                            state.activeUseCount);
                }
            }
            if (statesEntry.getValue().isEmpty()) {
                statesIterator.remove();
            }
        }
    }
State holder for  partitioned use case
modules/siddhi-core/src/main/java/io/siddhi/core/util/snapshot/state/PartitionStateHolder.java