天天看点

Java CookBook--Using a Buffer--by Tim O'Brien 整理by博主

数据结构Buffer的使用

  下面是一个最简单的Buffer实现UnboundedFifoBuffer的使用实例,UnboundedFifoBuffer是一个先进先出无大小限制的数据结构,如果无限制的添加,可能会耗尽内存。

import org.apache.commons.collections.Buffer;
import org.apache.commons.collections.buffer.UnboundedFifoBuffer;

public class BufferTest {
	public static void main(String[] args) {
		
		/*
		 * A buffer is an object that is defined by the algorithm used for element
		 * removal.Buffer objects can be priority queues, staging areas,message queues,
		 * or buffers for I/O. One of the simplest Buffer implementations is the 
		 * UnboundFifoBuffer, a first-in, first-out data structure with no size limit.
		 */
		//create an Unbounded FIFO
		Buffer buffer =new UnboundedFifoBuffer();
		//add element to the Buffer
		buffer.add("A");
		buffer.add("B");
		buffer.add("D");
		//remove element from the buffer
		String value=(String)buffer.remove();
		buffer.add("E");
		value=(String)buffer.remove();
		System.out.println(buffer.toString());
       
		/*
		 * Conclusion:
		 * 1. This example creates an UnboundedFifoBuffer, adding three elements: 
		 * "A," "B," and "D." When remove() is invoked,the buffer selects 
		 * the item that was placed into the buffer first—the first-in is the 
		 * first-out. The first call to remove( )returns "A," and the second call 
		 * returns "B." so the result is [D, E]
		 * 2. BufferUnderflowException will be thrown if you try to remove() or get() 
		 * an element from an empty Buffer.
		 * 3.This data sturcture is userful if you need a temporary holding are between
		 * stages in a pipeline--a queue to hold objects as they wait to be processed
		 * by the next stage. A unbounded FIFO buffer has no size limit, and, as such, 
		 * it could grow to a limitless size. In fact,if an application continued to fill
		 * an unbounded buffer, it would exhaust availabe memory.
		 */
	}
}
           

  结果如上述:

[D,E]
           

  再简单简绍一个先进先出的有容量限制的Buffer, 在超出容量删除或增加时,会抛出异常。如下:

import org.apache.commons.collections.Buffer;
import org.apache.commons.collections.BufferOverflowException;
import org.apache.commons.collections.BufferUnderflowException;
import org.apache.commons.collections.buffer.BoundedFifoBuffer;

public class BufferTest {
	public static void main(String[] args) {
		//Create a Bounded FIFO with a max size of two
		Buffer buffer=new BoundedFifoBuffer(2);
		buffer.add( "One" );
		buffer.add( "Two" );
		// Adding a third element to this buffer will cause an exception
		try {
			buffer.add( "Three" );
			} catch(BufferOverflowException bue){
				System.out.println("Buffer is Full!");
			}
		// Remove an object... Buffer now contains one element.
		Object removed = buffer.remove( );
		System.out.println("removed:"+removed);
		// Add another object
		buffer.add( "Three" );
		Object remove1 = buffer.remove( );
		System.out.println("remove1:"+remove1);
		Object remove2 = buffer.remove( );
		System.out.println("remove2:"+remove2);
		// This next remove( ) should cause a BufferUnderflowException
		try {
			Object remove3 = buffer.remove( );
			} catch(BufferUnderflowException bue){
				System.out.println( "Buffer is Empty!" );
			}	
		/*
		 * A buffer in Commons Collections is analogous to a Queue in Java5.The Queue 
		 * interface is accompanied by a number of interfaces that provide almost
		 * identical behavior to the Buffer interface as defined in Commons Collections 3.0. 
		 * Queue contains some new methods, offer( ) and peek( ), which perform the same 
		 * functions as add( ) and remove( ), with one difference: these new methods return 
		 * false if there is any problem adding an object to a Queue.
		 */
	}
}
           

结果如下:

Buffer is Full!
removed:One
remove1:Two
remove2:Three
Buffer is Empty!
           

如结论所言,我们也可以用java自带的Queue替代Buffer。

继续阅读