org.apache.commons.collections4.queue.CircularFifoQueue Example

Q

What is org.apache.commons.collections4.queue.CircularFifoQueue class? How to use org.apache.commons.collections4.queue.CircularFifoQueue class?

✍: FYIcenter.com

A

org.apache.commons.collections4.queue.CircularFifoQueue class is a Java class offered in commons-collections4.jar that represents a first-in first-out queue with a fixed size that replaces its oldest element if full.

Here is a simple example of using org.apache.commons.collections4.queue.CircularFifoQueue class:

// Copyright (c) 2016 FYIcenter.com
import org.apache.commons.collections4.queue.CircularFifoQueue;

// Example of using the CircularFifoQueue class
public class CircularFifoQueueExample {
   public static void main(String[] args) throws Exception {
       
      // Create a CircularFifoQueue object
      CircularFifoQueue<String> week = new CircularFifoQueue<String>(7);
      
      // Load the CircularFifoQueue object to the full size
      week.add("Sunday");
      week.add("Monday");
      week.add("Tuesday");
      week.add("Wednesday");
      week.add("Thursday");
      week.add("Friday");
      week.add("Saturday");
      
      // Load more elements
      week.add("One");
      week.add("Two");
      week.add("Three");

      // Poll elements out of the CircularFifoQueue object
      for (int i=0; i<10; i++) {
         System.out.println(i+ ": "+week.poll());
      }
   }
}

You can compile and run the above example in a command window as shown below:

C:\fyicenter>\local\jdk-1.7.0\bin\javac 
   -cp C:\local\commons-collections4-4.1\commons-collections4-4.1.jar 
   CircularFifoQueueExample.java

C:\fyicenter>\local\jdk-1.7.0\bin\java
   -cp .;C:\local\commons-collections4-4.1\commons-collections4-4.1.jar 
   CircularFifoQueueExample

0: Wednesday
1: Thursday
2: Friday
3: Saturday
4: One
5: Two
6: Three
7: null
8: null
9: null

 

Using commons-collections.jar in Java Programs

⇒⇒FAQ for Apache commons-collections.jar

2017-05-20, 528👍, 0💬