Java blocking queue containing only unique elements

后端 未结 5 1185
长发绾君心
长发绾君心 2020-12-11 15:45

sort of like a \"blocking set\". How can I implement a blocking queue where adding a member that is already in the set is ignored?

5条回答
  •  自闭症患者
    2020-12-11 15:55

    You can override add and put methods of any implementation of BlockingQueue to check first if the element is already within the queue, e.g.

    @Override
    public boolean add(T elem) {
        if (contains(elem))
            return true;
        return super.add(elem);
    }
    

提交回复
热议问题