0

I want to use queue FIFO (first-in first-out) functionality. How can I do this?

1
  • I would encourage you to show the research you've done, and what you've tried (and where you're stuck). If you don't do those things, you'll be more likely to get downvotes and have questions closed. Also, it looks like you haven't been accepting answers on most of your questions. Accepting answers isn't required, but it is a nice gesture (gives both you and the person who wrote the accepted answer internet points) and helps signal that your problem has been solved (and that the information in the answer is ostensibly valid and should help other people with similar problems).
    – Derek F
    Jun 18, 2020 at 18:35

1 Answer 1

7

There is no inbuilt functionality for that. You need to write the custom class for that.

public class Queue {
    
    private List<Object> fifoList = new List<Object>();
    
    public Object pop(){
        if(fifoList.size() > 0){
            return fifoList.remove(0);
        }
        return null;
    }
    
    public void push(Object obj){
        fifoList.add(obj);
    }
    
    public void clearQueue(){
        fifoList.clear();
    }
    
    public Integer size(){
        return fifoList.size();
    }
}

Example:

Queue q = new Queue();
q.push(1);
q.push(2);
q.push(3);

System.debug(q.pop());
System.debug(q.pop());
q.push(10);
System.debug(q.pop());
System.debug(q.pop());

Output:

enter image description here

Not the answer you're looking for? Browse other questions tagged .