简体   繁体   English

Java偷看哈希图中的下一个元素

[英]Java peek next element in hashmap

I have a hashmap with a queue object and I want to peek different objects in the different queue in the order they are located in queue each time i peek an object, typically consumer-producer problem; 我有一个带有队列对象的hashmap ,并且我希望每次查看一个对象时都可以按它们在队列中的顺序来查看不同队列中的不同对象,通常是消费者-生产者问题;

public class MainQueue {

    public static Map<Integer, SingleQueue> list = new ConcurrentHashMap<>();

    public MainQueue() {

    }

    public Map getQueue() {
        return this.list;
    }

    public void addToMainQueue(SendObject sfo) {
        int ohashcode = sfo.hashCode();
        if (!list.containsKey(ohashcode)) {
            list.put(ohashcode, new SingleQueue());
            list.get(ohashcode).addQueue(sfo);
        } else {
            list.get(ohashcode).addQueue(sfo);
        }

    }

    public SendObject getFromQueue() {

        ???
        return TempFax;
    }
}

---------HashMap-------------

423532,queue1:{1,2,3,4,5,6,7,8,9}

564898,queue2:{a,b,c,d,e}

039894,queue3:{x,y,z}

When I call getFromQueue function, it should return '1' after I call, return 'a' after I call, return 'x' 当我调用getFromQueue函数时,它应在调用后返回“ 1”,在调用后返回“ a”,返回“ x”

finally= 1,a,x,2,b,y,3,c,z,4,d,5,e,6,7,8,9

How can I do this? 我怎样才能做到这一点?

Based on your question you are interested in retrieving those elements in a FIFO order and you need some way to group them into hashcodes. 根据您的问题,您有兴趣按FIFO顺序检索这些元素,并且需要某种方式将它们分组为哈希码。

I suggest adding those SendObject into a single queue like LinkedList<SendObject>, this way you can poll() those elements one by one. 我建议将那些SendObject添加到像LinkedList <SendObject>这样的单个队列中,这样您就可以一个一地轮询()这些元素。

If you need to have some way to group different items into categories (or hashcodes in your case), you can add a Category field for example in SendObject so you can keep track of those. 如果您需要某种方式将不同的项目分组为类别(或您的案例中的哈希码),则可以在SendObject中添加一个Category字段,以便跟踪这些内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM