private synchronized void sortAndTrim() {
            if (!trimmed) {
                // We won't group, sort or trim lists with only a single item obviously.
                if (list.size() > 1) {
                    if (groupingFunction != null || sortingFunction != null) {
                        Stream<ItemImpl> stream = list
                                .parallelStream();

                        // Group items.
                        if (groupingFunction != null) {
                            stream = groupingFunction.apply(stream);
                        }

                        // Sort the list before trimming if we have a comparator.
                        if (sortingFunction != null) {
                            stream = sortingFunction.apply(stream);
                        }

                        list = stream
                                .collect(Collectors.toList());
                    }

                    if (list.size() > trimmedSize) {
                        logTruncation();
                        while (list.size() > trimmedSize) {
                            final ItemImpl lastItem = list.removeLast();

                            // Tell the remove handler that we have removed an item.
                            removeHandler.accept(lastItem.getKey());
                        }
                    }
                }
                trimmed = true;
            }
        }
Set to false if we don't want to keep looking through the store.
stroom-query/stroom-query-common/src/main/java/stroom/query/common/v2/MapDataStore.java