contains
有一个方便的/**
* Returns {@code true} if this queue contains the specified element.
* More formally, returns {@code true} if and only if this queue contains
* at least one element {@code e} such that {@code o.equals(e)}.
*
* @param o object to be checked for containment in this queue
* @return {@code true} if this queue contains the specified element
* @throws ClassCastException if the class of the specified element
* is incompatible with this queue
* (<a href="../Collection.html#optional-restrictions">optional</a>)
* @throws NullPointerException if the specified element is null
* (<a href="../Collection.html#optional-restrictions">optional</a>)
*/
public boolean contains(Object o);
方法:
predicate
我需要更具体一点:应用 Using a ArrayBlockQueue
Invoking toArray
/搜索条件。
{{1}}
那会有效..但如果队列很大怎么办?这可能会导致内存分配问题。
答案 0 :(得分:0)
我不知道你应该这样做,但似乎你可以这样做:
Checking item 1 against predicate QueueEquals$Matcher$$Lambda$1/531885035@816f27d
Checking item 3 against predicate QueueEquals$Matcher$$Lambda$1/531885035@816f27d
Checking item 5 against predicate QueueEquals$Matcher$$Lambda$1/531885035@816f27d
Checking item 7 against predicate QueueEquals$Matcher$$Lambda$1/531885035@816f27d
Checking item 9 against predicate QueueEquals$Matcher$$Lambda$1/531885035@816f27d
Checking item 11 against predicate QueueEquals$Matcher$$Lambda$1/531885035@816f27d
Checking item 13 against predicate QueueEquals$Matcher$$Lambda$1/531885035@816f27d
false
Checking item 1 against predicate QueueEquals$Matcher$$Lambda$1/531885035@816f27d
Checking item 2 against predicate QueueEquals$Matcher$$Lambda$1/531885035@816f27d
true
输出:
hashCode()
它确实让我想要洗澡......另外,正如pvg正确指出的那样 - 这不能保证工作。在调用equals()
之前,BlockingQueue实现可能会调用BlockingQueue
,然后我们就会被搞砸。
一个更健全的选择是迭代阻塞队列,并针对每个元素运行谓词。 iterator
接口没有ArrayBlockingQueue
,但有许多阻塞队列实现,例如LinkedBlockingQueue
和<packageCredentials>
。
答案 1 :(得分:0)
您可以直接在Queue
上流式传输(如果您愿意,可以并行)并使用您的谓词过滤掉您的元素。 BlockingQueue
是Queue
,Collection
,因此定义stream()
和parallelStream()
。以下内容:
public class SampleApplication {
private static final int ELEMENT_COUNT = 100000;
public static void main(String[] args) {
Queue<Element> blockingQueue = new ArrayBlockingQueue<>(ELEMENT_COUNT);
List<Element> elements = Stream.generate(SampleApplication::createElement).limit(ELEMENT_COUNT).collect(Collectors.toList());
blockingQueue.addAll(elements);
blockingQueue.stream().filter(getFilterPredicate(5)).findAny().ifPresent(System.out::println);
blockingQueue.stream().filter(getFilterPredicate(105)).findAny().ifPresent(System.out::println);
blockingQueue.stream().filter(getFilterPredicate(-1)).findAny().ifPresent(System.out::println);
}
private static Predicate<Element> getFilterPredicate(int value) {
return e -> e.getId() == value;
}
private static int i = 0;
private static Element createElement() {
return new Element(++i);
}
static class Element {
private int id;
private String message;
public Element(int id) {
this.id = id;
this.message = "Default msg";
}
//Getters, Setters & toString omitted
}
}
输出以下值:
Element{id=5, message=Default msg}
Element{id=105, message=Default msg}