简体   繁体   中英

Java creating a queue

I need to create a queue, the information fields of which are: the computer(I suppose it meant the name of the computer) and the amount of its RAM.

The first thing I thought was to create a computer class with the corresponding variables - name and ram .

class Computer{
    String name;
    int ram;
    public Computer(String name, int ram){
    . . .

}
. . .
}

And then create something like this :

Queue<Computer> q= new LinkedList<>();

And everything seems to be working well. But I would like to know if it is possible to do without creating a class Computer ?Like create information fields already when declaring a queue?

While this is not a good way to use Map.Entry interface, you could do the following:

Queue<Map.Entry<String, Integer>> queue = new LinkedList<>();
Map.Entry<String, Integer> entry = Map.entry("computer name", 16);
queue.add(entry);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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