public T getAndRemove(Predicate<T> preCondition) {
        Iterator<T> iterator = deque.iterator();
        for (int i = 0; iterator.hasNext(); i++) {
            T next = iterator.next();
            if (preCondition.test(next)) {
                if (i < numPriorityElements) {
                    numPriorityElements--;
                }
                iterator.remove();
                return next;
            }
        }
        throw new NoSuchElementException();
    }
@Test
    void testGetAndRemove() {
        final PrioritizedDeque<Integer> deque = new PrioritizedDeque<>();

        deque.add(0);
        deque.add(1);
        deque.add(2);
        deque.add(1);
        deque.add(3);

        assertThat(deque.getAndRemove(v -> v == 1).intValue()).isOne();
        assertThat(deque.asUnmodifiableCollection()).containsExactly(0, 2, 1, 3);
        assertThat(deque.getAndRemove(v -> v == 1).intValue()).isOne();
        assertThat(deque.asUnmodifiableCollection()).containsExactly(0, 2, 3);
        try {
            int removed = deque.getAndRemove(v -> v == 1);
            fail(
                    String.format(
                            "This should not happen. Item [%s] was removed, but it shouldn't be found",
                            removed));
        } catch (NoSuchElementException ex) {
            // expected
        }
    }